/**
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $inputFile = $input->getArgument('input');
     $outputFile = $input->getArgument('output');
     $inputFormat = substr($inputFile, strrpos($inputFile, '.') + 1);
     $outputFormat = $outputFile ? substr($outputFile, strrpos($outputFile, '.') + 1) : null;
     $output->writeln(sprintf('Convert <info>%s</info> into <info>%s</info>', $inputFormat, $outputFormat));
     $workflow = new Workflow();
     if ($inputFormat === 'csv') {
         $reader = new CsvReader($inputFile);
         $outputFormat = $outputFormat ? $outputFormat : 'xlsx';
         $outputFile = $outputFile ? $outputFile : str_replace('.csv', '.xlsx', $inputFile);
     } else {
         if ($inputFormat === 'xlsx' || $inputFormat === 'xls') {
             $reader = new ExcelReader(PHPExcel_IOFactory::load($inputFile));
             $outputFormat = $outputFormat ? $outputFormat : 'csv';
             $outputFile = $outputFile ? $outputFile : str_replace(['.xlsx', '.xls'], '.csv', $inputFile);
         } else {
             $output->writeln(sprintf('<error>Invalid input file format %s.</error>', $inputFormat));
             return;
         }
     }
     $workflow->addConverter(new HeaderConverter());
     $workflow->addFilter(new SkipFirstFilter(1));
     if ($outputFormat === 'csv') {
         $writer = new CsvWriter($outputFile);
         $writer->autoDetectHeader();
     } else {
         if ($outputFormat === 'xlsx' || $outputFormat == 'xls') {
             $writer = new ExcelWriter($outputFile);
             $writer->autoDetectHeader();
         } else {
             $output->writeln(sprintf('<error>Invalid output file format: %s</error>', $outputFormat));
             return;
         }
     }
     $workflow->addConverter(new NullConverter());
     $workflow->addWriter($writer);
     $workflow->addWriter(new ConsoleProgressWriter(new ProgressBar($output, $reader->count())));
     $result = $workflow->process($reader);
     $output->writeln('');
     $output->writeln(sprintf('Read rows:    %d', $result->getReadCount()));
     $output->writeln(sprintf('Written rows: %d', $result->getItemWriteCount()));
     $output->writeln(sprintf('Error rows:   %d', $result->getErrorCount()));
     if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
         foreach ($result->getExceptions() as $exception) {
             $output->writeln(sprintf('- <error>%s</error>', $exception->getMessage()));
             if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
                 $output->writeln(sprintf('<error>%s</error>', $exception->getTraceAsString()));
             }
         }
     }
 }
示例#2
0
 /**
  * @test
  * @covers Plum\PlumCsv\CsvWriter::__construct()
  * @covers Plum\PlumCsv\CsvWriter::writeItem()
  * @covers Plum\PlumCsv\CsvWriter::prepare()
  * @covers Plum\PlumCsv\CsvWriter::finish()
  * @covers Plum\PlumCsv\CsvWriter::verifyHandle()
  */
 public function writeItemWritesItemIntoFileInjectingWriter()
 {
     $csv = Writer::createFromFileObject(new SplFileObject(vfsStream::url('fixtures/foo.csv'), 'w'));
     $csv->setNullHandlingMode(Writer::NULL_AS_EMPTY);
     $writer = new CsvWriter($csv);
     $writer->prepare();
     $writer->writeItem(['col 1', 'col 2', 'col 3']);
     $writer->finish();
     $this->assertEquals("\"col 1\",\"col 2\",\"col 3\"\n", file_get_contents(vfsStream::url('fixtures/foo.csv')));
 }