示例#1
0
 /**
  * @param ReadInterface $file
  * @param int $websiteId
  * @param string $conditionShortName
  * @param string $conditionFullName
  * @param int $bunchSize
  * @return \Generator
  * @throws LocalizedException
  */
 public function getData(ReadInterface $file, $websiteId, $conditionShortName, $conditionFullName, $bunchSize = 5000)
 {
     $this->errors = [];
     $headers = $this->getHeaders($file);
     /** @var ColumnResolver $columnResolver */
     $columnResolver = $this->columnResolverFactory->create(['headers' => $headers]);
     $rowNumber = 1;
     $items = [];
     while (false !== ($csvLine = $file->readCsv())) {
         try {
             $rowNumber++;
             if (empty($csvLine)) {
                 continue;
             }
             $rowData = $this->rowParser->parse($csvLine, $rowNumber, $websiteId, $conditionShortName, $conditionFullName, $columnResolver);
             // protect from duplicate
             $hash = $this->dataHashGenerator->getHash($rowData);
             if (array_key_exists($hash, $this->uniqueHash)) {
                 throw new RowException(__('Duplicate Row #%1 (duplicates row #%2)', $rowNumber, $this->uniqueHash[$hash]));
             }
             $this->uniqueHash[$hash] = $rowNumber;
             $items[] = $rowData;
             if (count($items) === $bunchSize) {
                 (yield $items);
                 $items = [];
             }
         } catch (RowException $e) {
             $this->errors[] = $e->getMessage();
         }
     }
     if (count($items)) {
         (yield $items);
     }
 }