Beispiel #1
0
 private function buildSheet(FileInterface $file) : SheetInterface
 {
     $csv = tmpfile();
     $stream = $file->content()->rewind();
     while (!$stream->isEof()) {
         fwrite($csv, $stream->read(8192));
     }
     rewind($csv);
     $sheet = new Sheet(basename((string) $file->name(), '.csv'));
     $position = 1;
     $firstLine = fgetcsv($csv, 0, $this->delimiter);
     if (!$this->useFirstLineAsColumnIdentifier) {
         rewind($csv);
     }
     while (($line = fgetcsv($csv, 0, $this->delimiter)) !== false) {
         foreach ($line as $key => $value) {
             $sheet = $sheet->add(new Cell(new Position($this->useFirstLineAsColumnIdentifier ? $firstLine[$key] : $key, $position), $value));
         }
         ++$position;
     }
     fclose($csv);
     return $sheet;
 }
 /**
  * Create the wished file at the given absolute path
  *
  * @param string $path
  * @param FileInterface $file
  *
  * @return void
  */
 private function createFileAt(string $path, FileInterface $file)
 {
     if ($file instanceof DirectoryInterface) {
         $folder = $path . '/' . (string) $file->name();
         if ($this->files->contains($folder) && $this->files->get($folder) === $file) {
             return;
         }
         $this->filesystem->mkdir($folder);
         $file->recordedEvents()->foreach(function ($event) use($folder) {
             if ($this->handledEvents->contains($event)) {
                 return;
             }
             switch (true) {
                 case $event instanceof FileWasRemoved:
                     $this->filesystem->remove($folder . '/' . $event->file());
                     break;
                 case $event instanceof FileWasAdded:
                     $this->createFileAt($folder, $event->file());
                     break;
             }
             $this->handledEvents = $this->handledEvents->add($event);
         });
         $this->files = $this->files->put($folder, $file);
         return;
     }
     $path .= '/' . (string) $file->name();
     if ($this->files->contains($path) && $this->files->get($path) === $file) {
         return;
     }
     $stream = $file->content();
     $stream->rewind();
     $handle = fopen($path, 'w');
     while (!$stream->isEof()) {
         fwrite($handle, $stream->read(8192));
     }
     $this->files = $this->files->put($path, $file);
 }