Skip to content

Commit

Permalink
Remove addFilterClosure, addConverterClosure and addWriterClosure met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
ddeboer committed Jun 19, 2012
1 parent 128ef08 commit 6d06981
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 60 deletions.
40 changes: 25 additions & 15 deletions README.md
Expand Up @@ -55,7 +55,7 @@ Usage
object to retrieve this `splFileObject`: construct a source and add `source
filters`, if you like.
2. Construct a `reader` object and pass an `splFileObject` to it.
3. Construct a `workflow` object and pass the reader to it. Add at least one
3. Construct a `workflow` object and pass the reader to it. Add at least one
`writer` object to this workflow. You can also add `filters` and `converters`
to the workflow.
4. Process the workflow: this will read the data from the reader, filter and
Expand All @@ -64,10 +64,10 @@ Usage
An example:

```
use Ddeboer\DataImportBundle\Workflow;
use Ddeboer\DataImportBundle\Source\Http;
use Ddeboer\DataImportBundle\Source\Filter\Unzip;
use Ddeboer\DataImportBundle\Reader\CsvReader;
use Ddeboer\DataImportBundle\Workflow;
use Ddeboer\DataImportBundle\Converter\DateTimeConverter;
(...)
Expand All @@ -90,22 +90,32 @@ $workflow = new Workflow($csvReader);
$dateTimeConverter = new DateTimeConverter();
// Add converters to the workflow
$workflow->addConverter('twn_datumbeschikking', $dateTimeConverter)
->addConverter('twn_datumeind', $dateTimeConverter)
->addConverter('datummutatie', $dateTimeConverter)
$workflow
->addConverter('twn_datumbeschikking', $dateTimeConverter)
->addConverter('twn_datumeind', $dateTimeConverter)
->addConverter('datummutatie', $dateTimeConverter)
// You can also add closures as converters
->addConverterClosure('twn_nummertm', function($input) {
return str_replace('-', '', $input);
})
->addConverterClosure('twn_nummervan', function($input) {
return str_replace('-', '', $input);
})
// For now, no writers are supplied yet, so implement your own or use a closure
->addWriterClosure(function($csvLine) {
->addConverter('twn_nummertm',
new \Ddeboer\DataImportBundle\Converter\CallbackConverter(
function($input) {
return str_replace('-', '', $input);
}
)
->addConverter('twn_nummervan',
new \Ddeboer\DataImportBundle\Converter\CallbackConverter(
function($input) {
return str_replace('-', '', $input);
}
)
// Use one of the writers supplied with this bundle, implement your own, or use
// a closure:
->addWriter(new \Ddeboer\DataImportBundle\Writer\CallbackWriter(
function($csvLine) {
var_dump($csvLine);
});
}
);
// Process the workflow
$workflow->process();
Expand Down
41 changes: 41 additions & 0 deletions Tests/WorkflowTest.php
@@ -0,0 +1,41 @@
<?php

namespace Ddeboer\DataImportBundle\Tests;

use Ddeboer\DataImportBundle\Workflow;
use Ddeboer\DataImportBundle\Filter\CallbackFilter;
use Ddeboer\DataImportBundle\Converter\CallbackConverter;
use Ddeboer\DataImportBundle\Writer\CallbackWriter;

class WorkflowTest extends \PHPUnit_Framework_TestCase
{
public function testAddCallbackFilter()
{
$this->getWorkflow()->addFilter(new CallbackFilter(function($input) {
return true;
}));
}

public function testAddCallbackConverter()
{
$this->getWorkflow()->addConverter('someField', new CallbackConverter(function($input) {
return str_replace('-', '', $input);
}));
}

public function testAddCallbackWriter()
{
$this->getWorkflow()->addWriter(new CallbackWriter(function($item) {
var_dump($item);
}));
}

protected function getWorkflow()
{
$reader = $this->getMockBuilder('\Ddeboer\DataImportBundle\Reader\CsvReader')
->disableOriginalConstructor()
->getMock();

return new Workflow($reader);
}
}
45 changes: 0 additions & 45 deletions Workflow.php
Expand Up @@ -95,22 +95,6 @@ public function addFilterAfterConversion(Filter $filter)
return $this;
}

/**
* Add a filter closure to the workflow
*
* A filter decides whether an item is accepted into the import process.
*
* @param \Closure $closure
*
* @return $this
*/
public function addFilterClosure(\Closure $closure)
{
$this->filters[] = new CallbackFilter($closure);

return $this;
}

/**
* Add a writer to the workflow
*
Expand All @@ -128,20 +112,6 @@ public function addWriter(Writer $writer)
return $this;
}

/**
* Add a writer closure to the workflow
*
* @param \Closure $closure
*
* @return Workflow
*/
public function addWriterClosure(\Closure $closure)
{
$this->writers[] = new CallbackWriter($closure);

return $this;
}

/**
* Add a converter to the workflow
*
Expand All @@ -157,21 +127,6 @@ public function addConverter($field, Converter $converter)
return $this;
}

/**
* Add a converter closure to the workflow
*
* @param string $field Field
* @param \Closure $closure Closure
*
* @return Workflow
*/
public function addConverterClosure($field, \Closure $closure)
{
$this->converters[$field][] = new CallbackConverter($closure);

return $this;
}

/**
* Add a mapping to the workflow
*
Expand Down

0 comments on commit 6d06981

Please sign in to comment.