Skip to content

Commit

Permalink
add DateTime threshold filter
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire authored and Grégoire Paris committed Mar 23, 2015
1 parent afd71ba commit 94e5cb8
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Documentation
* [Filters](#filters)
- [CallbackFilter](#callbackfilter)
- [OffsetFilter](#offsetfilter)
- [DateTimeThresholdFilter](#datetimethresholdfilter)
- [ValidatorFilter](#offsetfilter)
* [Converters](#converters)
- [Item converters](#item-converters)
Expand Down Expand Up @@ -733,6 +734,23 @@ $filter = new OffsetFilter(0, 3);
$filter = new OffsetFilter(2, 5);
```

#### DateTimeThresholdFilter

This filter is useful if you want to do incremental imports. Specify a threshold
`DateTime` instance, a column name (defaults to `updated_at`), and a
`DateTimeValueConverter` that will be used to convert values read from the
filtered items. The item strictly older than the threshold will be discarded.

```php
use Ddeboer\DataImport\Filter\DateTimeThresholdFilter;
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

new DateTimeThresholdFilter(
new DateTimeValueConverter(),
new \DateTime('yesterday')
);
```

#### ValidatorFilter

It’s a common use case to validate the data before you save it to the database.
Expand Down
80 changes: 80 additions & 0 deletions src/Ddeboer/DataImport/Filter/DateTimeThresholdFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Ddeboer\DataImport\Filter;

use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

/**
* This filter can be used to filter out some items from a specific date. Useful
* to do incremental imports
*/
class DateTimeThresholdFilter implements FilterInterface
{
/**
* @var DateTime threshold dates strictly before this date will be filtered out.
* defaults to null
*/
protected $threshold;

/**
* @var DateTimeValueConverter used to convert the values in the time column
*/
protected $valueConverter;

/**
* @var string the name of the column that should contain the value the
* filter will compare the threshold with. Defaults to "updated_at"
*/
protected $timeColumnName;

/**
* @var int priority the filter priority. Defaults to 512.
*/
protected $priority;

public function __construct(
DateTimeValueConverter $valueConverter,
\DateTime $threshold = null,
$timeColumnName = 'updated_at',
$priority = 512
) {
$this->valueConverter = $valueConverter;
$this->threshold = $threshold;
$this->timeColumnName = $timeColumnName;
$this->priority = $priority;
}

/**
* {@inheritDoc}
*/
public function filter(array $item)
{
if ($this->threshold == null) {
throw new \LogicException('Make sure you set a threshold');
}

return
$this->valueConverter->convert($item[$this->timeColumnName])
>=
$this->threshold;
}

/**
* Useful if you build a filter service, and want to set the threshold
* dynamically afterwards.
*/
public function setThreshold(\DateTime $value)
{
$this->threshold = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
return $this->priority;
}
}
63 changes: 63 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Filter/DateTimeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Ddeboer\DataImport\Filter;

use Ddeboer\DataImport\Filter\DateTimeThresholdFilter;
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;

class DateTimeFilterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->items = array(
'a' => array('updated_at' => '-3 day'),
'b' => array('updated_at' => '-2 day'),
'c' => array('updated_at' => '-1 day'),
'd' => array('updated_at' => 'today'),
'e' => array('updated_at' => 'now'),
'f' => array('updated_at' => '+1 day'),
);
}

private function applyFilter(DateTimeThresholdFilter $filter, array $items)
{
return array_filter($items, array($filter, 'filter'));
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Make sure you set a threshold
*/
public function testDefaultFilter()
{
$resultItems = $this->applyFilter(
new DateTimeThresholdFilter(new DateTimeValueConverter()),
$this->items
);
}

public function testFilter()
{
$resultItems = $this->applyFilter(new DateTimeThresholdFilter(
new DateTimeValueConverter(),
new \DateTime('today')
), $this->items);
$this->assertEquals(
array('d', 'e', 'f'),
array_keys($resultItems)
);
}

public function testSetter()
{
$filter = new DateTimeThresholdFilter(new DateTimeValueConverter());
$filter->setThreshold(new \DateTime('today'));
$resultItems = $this->applyFilter($filter, $this->items);


$this->assertEquals(
array('d', 'e', 'f'),
array_keys($resultItems)
);
}
}

0 comments on commit 94e5cb8

Please sign in to comment.