本文整理汇总了PHP中League\Csv\Reader::createFromString方法的典型用法代码示例。如果您正苦于以下问题:PHP Reader::createFromString方法的具体用法?PHP Reader::createFromString怎么用?PHP Reader::createFromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类League\Csv\Reader
的用法示例。
在下文中一共展示了Reader::createFromString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: import
/**
*
*/
public function import($data, Event $event)
{
$csv = Reader::createFromString(trim($data));
$csv->setDelimiter(';');
// get startgroups at first
$groups = new Map();
foreach ($csv->fetchColumn(0) as $name) {
if (!$groups->has($name)) {
$startgroup = $this->getStartgroup($name);
$startgroup->setEvent($event);
$groups->set($name, $startgroup);
}
}
$id = 0;
foreach ($csv as $row) {
$id++;
$routine = $this->getRoutine($id);
$group = $groups->get($row[0]);
$judges = (count($row) - 1) / 3;
for ($j = 1; $j <= $judges; $j++) {
$score = new PerformanceScore();
$score->setRoutine($routine);
$score->setJudge($group->getPerformanceJudge($j));
$score->setExecution($row[($j - 1) * 3 + 1]);
$score->setChoreography($row[($j - 1) * 3 + 2]);
$score->setMusicAndTiming($row[($j - 1) * 3 + 3]);
$score->setTotal($row[($j - 1) * 3 + 1] + $row[($j - 1) * 3 + 2] + $row[($j - 1) * 3 + 3]);
$routine->addPerformanceScore($score);
}
$group->addRoutine($routine);
$group->save();
}
$event->save();
}
示例2: readCSV
protected function readCSV()
{
$csvObj = Reader::createFromString($this->readFile(static::IMPORT_FILE));
$csvObj->setDelimiter(',');
$results = $csvObj->fetchAssoc();
return $results;
}
示例3: createImportEntries
/**
* Run the actual import
*
* @return Collection
*/
public function createImportEntries() : Collection
{
$config = $this->job->configuration;
$content = $this->job->uploadFileContents();
// create CSV reader.
$reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']);
$start = $config['has-headers'] ? 1 : 0;
$results = $reader->fetch();
Log::notice('Building importable objects from CSV file.');
foreach ($results as $index => $row) {
if ($index >= $start) {
$line = $index + 1;
Log::debug('----- import entry build start --');
Log::debug(sprintf('Now going to import row %d.', $index));
$importEntry = $this->importSingleRow($index, $row);
$this->collection->put($line, $importEntry);
/**
* 1. Build import entry.
* 2. Validate import entry.
* 3. Store journal.
* 4. Run rules.
*/
$this->job->addTotalSteps(4);
$this->job->addStepsDone(1);
}
}
Log::debug(sprintf('Import collection contains %d entries', $this->collection->count()));
Log::notice(sprintf('Built %d importable object(s) from your CSV file.', $this->collection->count()));
return $this->collection;
}
示例4: fetch
public function fetch(Connector $connector, EncapsulatedOptions $options = null)
{
$csv = $connector->fetch('http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv');
$reader = Reader::createFromString($csv);
foreach ($reader->fetchAssoc() as $row) {
(yield $row);
}
}
示例5: __construct
public function __construct($data)
{
if (is_string($data)) {
$this->csv = Reader::createFromString($data);
} else {
throw new InvalidArgumentException('CsvParser only accepts (string) [csv] for $data.');
}
}
示例6: getReader
/**
* @param string $data
* @return Reader
*/
protected function getReader($data)
{
$csv = Reader::createFromString($data);
$delimiters = $csv->fetchDelimitersOccurrence([' ', '|', ',', ';'], 10);
if (sizeof($delimiters) > 0) {
$csv->setDelimiter(array_keys($delimiters)[0]);
}
return $csv;
}
示例7: spreadsheetAsCsv
public function spreadsheetAsCsv()
{
$url = "https://docs.google.com/spreadsheets/d/{$this->fileId}/export?format=csv&gid={$this->gId}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$reader = Reader::createFromString(curl_exec($ch));
curl_close($ch);
return $reader->fetchAssoc(0);
}
示例8: parse
public function parse($content)
{
$reader = Reader::createFromString($content);
$reader->setDelimiter($this->delimiter);
$reader->setNewline($this->newline);
$reader->setEnclosure($this->enclosure);
$reader->setEscape($this->escapeChar);
$reader->setFlags($this->flags);
if ($this->hasHeader) {
return array_slice($reader->fetchAssoc($reader->fetchOne()), 1);
}
return $reader->fetchAll();
}
示例9: send
private function send()
{
try {
$response = $this->client->get(null, ['body' => $this->request]);
} catch (ClientException $e) {
throw new FirstDataException($e->getResponse()->getBody());
}
$inputCsv = Reader::createFromString($response->getBody());
$inputCsv->setDelimiter(',');
$inputCsv->setEncodingFrom('ISO-8859-15');
$inputCsv->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
return $inputCsv->fetchAssoc();
}
示例10: getRows
/**
* Extract user rows from CSV string
*
* @param $csv
* @return \Iterator
*/
private function getRows($csv)
{
$reader = Reader::createFromString($csv);
$reader->setDelimiter($this->delimiter);
// Find column positions
$columns = $this->findColumns($reader->fetchOne());
return $reader->setOffset(1)->fetch(function ($row) use($columns) {
$user = [];
foreach ($columns as $key => $name) {
$user[$name] = $row[$key];
}
return $user;
});
}
示例11: postUpload
public function postUpload()
{
$files = $this->flyLocal()->listContents();
foreach ($files as $file) {
if (preg_match('/.csv$/', $file['basename'])) {
$contents = $this->flyLocal()->readAndDelete($file['basename']);
$csv = Reader::createFromString($contents);
$rows = $csv->setOffset(1)->fetchAll();
foreach ($rows as $row) {
$item = new Item();
$item->date = strtotime($row[0]);
$item->category = $row[1];
$item->title = $row[2];
$item->location = $row[3];
$item->condition = $row[4];
$item->amount = $row[5];
$item->tax_name = $row[6];
$item->tax_amount = $row[7];
$item->save();
}
}
}
return redirect('admin');
}
示例12: processWorkerunitData
public function processWorkerunitData($csvresult, $preview = false)
{
set_time_limit(6000);
$csvLines = explode("\n", $csvresult['content']);
$csvLines = array_filter($csvLines, 'strlen');
// $csvLines = array_slice($csvLines, 0, 2);
$csv = Reader::createFromString(implode("\n", $csvLines));
$delimiter = $csv->detectDelimiter();
$csv->setDelimiter($delimiter);
$headers = $csv->fetchOne();
// dd($headers);
foreach ($headers as $columnIndex => $columnValue) {
if ($columnValue == "") {
$headers[$columnIndex] = "empty_" . $columnIndex;
}
}
$rowsMappedWithUnits = array();
$batch = array();
// dd($headers);
// dd($csv->fetchAll());
$workerunitCSVArray = $csv->fetchAssoc($headers);
unset($workerunitCSVArray[0]);
if (strpos($csvresult->title, 'relexcorrected') !== false || strpos($csvresult->title, 'reldir') !== false) {
$result = $this->processRel($csvresult, $workerunitCSVArray, $preview);
return $result;
}
// dd($workerunitCSVArray);
$inputCSVFiles = \MongoDB\Entity::where('documentType', 'csvresult')->where('title', 'like', '%input%')->get();
$processedInputCSVFiles = array();
foreach ($inputCSVFiles as $inputCSVFile) {
array_push($processedInputCSVFiles, $this->processInputData($inputCSVFile->toArray(), true));
}
$aggregatedProcessedInputCSVFiles = array();
foreach ($processedInputCSVFiles as $processedInputCSVFile) {
foreach ($processedInputCSVFile as $processedInputCSVFileMapped) {
array_push($aggregatedProcessedInputCSVFiles, $processedInputCSVFileMapped);
}
}
$mappedWorkerunitsWithUnits = array();
$batchOfUnits = array();
foreach ($workerunitCSVArray as $workerunitRow) {
foreach ($aggregatedProcessedInputCSVFiles as $mappedInputRow) {
if (strpos($csvresult->title, 'workerunit_output_cf_factorspan_merged_relex') !== false) {
if (stripos($workerunitRow['Sent_id'], '-')) {
$workerunitRowIndex = explode('-', $workerunitRow['Sent_id'])[0];
}
} else {
$workerunitRowIndex = $workerunitRow['index'];
}
if ($workerunitRowIndex == $mappedInputRow['index']) {
$mappedWorkerunitWithUnit = $workerunitRow;
$mappedWorkerunitWithUnit['unit'] = $mappedInputRow['unit'];
array_push($batchOfUnits, $mappedInputRow['unit']['_id']);
array_push($mappedWorkerunitsWithUnits, $mappedWorkerunitWithUnit);
}
}
}
$batchOfUnits = array_values(array_unique($batchOfUnits));
natsort($batchOfUnits);
$batchOfUnits = array_values($batchOfUnits);
if ($preview == true) {
return $mappedWorkerunitsWithUnits;
}
if (stripos($csvresult->title, "factorspan") && stripos($csvresult->title, "380140")) {
$status = $this->factorspan_308140($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
} elseif (stripos($csvresult->title, "factorspan") && stripos($csvresult->title, "380640")) {
$status = $this->factorspan_380640($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
} elseif (stripos($csvresult->title, "factorspan") && stripos($csvresult->title, "382004")) {
$status = $this->factorspan_382004($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
} elseif (strpos($csvresult->title, 'annotation_output_cf_factorspan_merged_relex') !== false) {
$status = $this->workerunit_output_cf_factorspan_merged_relex($mappedWorkerunitsWithUnits);
} elseif (stripos($csvresult->title, "relexu") && stripos($csvresult->title, "f387177")) {
$status = $this->relexu_f387177($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
} elseif (stripos($csvresult->title, "relexu") && stripos($csvresult->title, "f387719")) {
$status = $this->relexu_f387719($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
} elseif (stripos($csvresult->title, "relexu") && stripos($csvresult->title, "f388572")) {
$status = $this->relexu_f388572($csvresult, $batchOfUnits, $mappedWorkerunitsWithUnits);
}
return $status;
}
示例13: processRows
public function processRows()
{
$csv = Reader::createFromString($this->data);
$csv->setOffset(1);
// Add filter to skip empty rows
$csv->addFilter(function ($row) {
return strlen(trim(implode('', $row))) > 0;
});
$columnMap = $this->getModelInstance()->getColumnMap();
$columns = array_keys($columnMap);
$csv->each(function ($row, $index) use($columns, $columnMap) {
$this->refresh();
if ($this->abort) {
return false;
}
try {
$row = array_combine($columns, $row);
if (!$this->processRow($row, $columnMap, $index)) {
$this->error_count++;
}
} catch (\yii\base\Exception $e) {
Yii::error($e->getMessage());
$this->status_id = Import::STATUS_ERROR;
return false;
}
$this->processed_rows++;
$this->progress = $this->getPercent();
$this->update(false, ['processed_rows', 'progress', 'status_id', 'error_count']);
return true;
});
}
示例14: getDocumentData
/**
* Load content of a document (CSV) as a data matrix.
*
* @param $documentContent Raw content of CSV file.
* @param $delimiter CSV field delimiter.
* @param $separator CSV column separator.
* @param $ignoreHeader Boolean flag for ignoring headers.
* @param $nLines Maximum number of lines to read from file (-1 to read all)
* @return Data matrix representing the file contents.
*/
private function getDocumentData($documentContent, $delimiter, $separator, $ignoreHeader, $nLines)
{
$reader = Reader::createFromString($documentContent);
$reader->setDelimiter($separator);
$reader->setEnclosure($delimiter);
if ($ignoreHeader) {
$reader->setLimit($nLines);
} else {
$reader->setOffset(1);
$reader->setLimit($nLines + 1);
}
$dataTable = $reader->fetchAll();
return $dataTable;
}
示例15: getColumns
/**
* Get table columns
* @returns array
*/
public function getColumns($table)
{
$reader = \League\Csv\Reader::createFromString(implode("\n", $this->_getCSVArray($table, true)));
return $reader->fetchOne(0);
}