Пример #1
0
 private function formatToReader(Format $format, \SplFileInfo $file)
 {
     if ($format instanceof CsvFormat) {
         $reader = new CsvReader($file->openFile(), $format->getDelimiter(), $format->getEnclosure(), $format->getEscape());
         $reader->setStrict(false);
         if ($format->isHeaderInFirstRow()) {
             $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE);
             $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders()));
             //TODO some header-collaborator?
         }
     } elseif ($format instanceof ExcelFormat) {
         $headerRowNumber = $format->isHeaderInFirstRow() ? 0 : null;
         $reader = new ExcelReader($file->openFile(), $headerRowNumber, $format->getActivesheet());
         if ($format->isHeaderInFirstRow()) {
             $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders()));
             //TODO some header-collaborator?
         }
     } elseif ($format instanceof JsonFormat) {
         $array = json_decode(file_get_contents($file), true);
         $reader = new ArrayReader($array);
     } elseif ($format instanceof XmlFormat) {
         $reader = new XmlReader($file->openFile(), $format->getXpath());
     } elseif ($format instanceof CompressedFormat && $format->getSubFormat()) {
         $reader = $this->formatToReader($format->getSubFormat(), $format->getInsideStream($file));
     } else {
         throw new InvalidConfigurationException('Cannot build reader. Unknown format: ' . $format);
     }
     return $reader;
 }