addFilter() public method

public addFilter ( array | callable | Plum\Plum\Filter\FilterInterface $element ) : Workflow
$element array | callable | Plum\Plum\Filter\FilterInterface
return Workflow
 /**
  * @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()));
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @test
  * @covers Plum\Plum\Workflow::process()
  * @covers Plum\Plum\Workflow::processReader()
  * @covers Plum\Plum\Workflow::processItem()
  */
 public function processShouldApplyValueFilterToReadItems()
 {
     $iterator = m::mock('\\Iterator');
     $iterator->shouldReceive('rewind');
     $iterator->shouldReceive('valid')->andReturn(true)->twice();
     $iterator->shouldReceive('current')->andReturn(['foo' => 'foobar'])->once();
     $iterator->shouldReceive('current')->andReturn(['foo' => 'foo'])->once();
     $iterator->shouldReceive('next')->twice();
     $iterator->shouldReceive('valid')->andReturn(false)->once();
     $reader = $this->getMockReader();
     $reader->shouldReceive('getIterator')->andReturn($iterator);
     $filter = $this->getMockFilter();
     $filter->shouldReceive('filter')->with('foobar')->once()->andReturn(false);
     $filter->shouldReceive('filter')->with('foo')->once()->andReturn(true);
     $this->workflow->addFilter(['filter' => $filter, 'field' => ['foo']]);
     $writer = $this->getMockWriter();
     $writer->shouldReceive('prepare');
     $writer->shouldReceive('finish');
     $writer->shouldReceive('writeItem')->with(['foo' => 'foo'])->once();
     $this->workflow->addWriter($writer);
     $result = $this->workflow->process($reader);
     $this->assertEquals(2, $result->getReadCount());
     $this->assertEquals(1, $result->getWriteCount());
 }
Exemplo n.º 3
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Plum\Plum\Writer\ArrayWriter;
use Plum\Plum\Workflow;
use Plum\Plum\Reader\ArrayReader;
use Plum\Plum\Converter\CallbackConverter;
use Plum\Plum\Filter\CallbackFilter;
$reader = new ArrayReader(['foobar', 'qoobar', 'bazbaz']);
$writer1 = new ArrayWriter();
$writer2 = new ArrayWriter();
$workflow = new Workflow();
$workflow->addConverter(new CallbackConverter(function ($item) {
    return strtoupper($item);
}));
$workflow->addWriter($writer1);
$workflow->addFilter(new CallbackFilter(function ($item) {
    return $item != 'BAZBAZ';
}));
$workflow->addWriter($writer2);
$workflow->process($reader);
print_r($writer1->getData());
print_r($writer2->getData());