/**
  * @inheritdoc
  */
 public function needsTransforming(ResourceInterface $resource)
 {
     $file = $resource->getFile()->getPathname();
     $handle = fopen($file, 'r');
     $result = (bool) preg_match($this->xmlDeclarationRegEx, fread($handle, 96));
     fclose($handle);
     return $result;
 }
Example #2
0
 /**
  * Creates a reader for a resource.
  *
  * @param ResourceInterface $resource
  */
 protected function createReader(ResourceInterface $resource)
 {
     try {
         $jsonFile = $resource->getFile()->getPathname();
         $this->fileObject = new \SplFileObject($jsonFile);
     } catch (EmptyResponseException $e) {
         // getting an empty response is not a problem for jsonlines files, this
         // just means that there are no records.
         $this->fileObject = new \ArrayIterator();
     }
 }
 /**
  * @inheritdoc
  */
 public function needsTransforming(ResourceInterface $resource)
 {
     $file = $resource->getFile();
     while (!$file->eof()) {
         $line = $file->fgets();
         if (!mb_check_encoding($line, $this->toEncoding)) {
             return true;
         }
     }
     return false;
 }
 /**
  * @inheritdoc
  */
 public function transform(ResourceInterface $resource, ResourceCollection $collection)
 {
     $file = $resource->getFile()->getPathname();
     $tmpFile = tempnam(sys_get_temp_dir(), $file);
     // remove control characters
     $old = fopen($file, 'r');
     $new = fopen($tmpFile, 'w');
     // list control characters, but leave out \t\r\n
     $chars = array_map('chr', range(0, 31));
     $chars[] = chr(127);
     unset($chars[9], $chars[10], $chars[13]);
     while (!feof($old)) {
         fwrite($new, str_replace($chars, '', fread($old, $this->length)));
     }
     fclose($old);
     fclose($new);
     // atomic write
     $this->rename($tmpFile, $file);
     $transport = FileTransport::create($file);
     if ($resource->getTransport()) {
         $transport->setDestinationDir($resource->getTransport()->getDestinationDir());
     }
     return new FileResource($transport);
 }
Example #5
0
 /**
  * @inheritdoc
  */
 protected function createReader(ResourceInterface $resource)
 {
     $this->fileObject = new SplFileObject($resource->getFile()->getPathname());
     $this->fileObject->setFlags(SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
     $this->fileObject->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
     if ($this->useFirstRow) {
         /** @var array $mapping */
         $mapping = $this->fileObject->current();
         $this->setFieldMapping($mapping);
         $this->fileObject->next();
     }
 }
Example #6
0
 /**
  * @inheritdoc
  */
 protected function createReader(ResourceInterface $resource)
 {
     $this->reader = new \XmlReader();
     $this->open($resource->getFile()->getPathname());
     $this->key = -1;
     $this->next();
 }
 /**
  * @param ResourceInterface $resource
  *
  * @return array
  */
 protected function breakup(ResourceInterface $resource)
 {
     $originalFile = $resource->getFile();
     $baseFile = $originalFile->getPathname();
     $this->reader->setResources(new ResourceCollection([$resource]));
     $partCount = 0;
     $started = false;
     while ($this->reader->valid()) {
         if ($this->reader->key() % $this->size === 0) {
             if ($this->reader->key() > 0) {
                 $this->endPart();
             }
             $file = sprintf('%s.part%s', $baseFile, ++$partCount);
             $this->startPart($file);
             $started = true;
         }
         $this->writer->write($this->reader->current());
         $this->writer->flush();
         $this->reader->next();
     }
     if ($started) {
         $this->endPart();
     }
     return $this->getPartFiles($resource);
 }
 /**
  * @param ResourceInterface $resource
  *
  * @return string
  */
 protected function getTargetDir(ResourceInterface $resource)
 {
     return $this->target ?: $resource->getFile()->getPath();
 }