-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
tests/Ddeboer/DataImport/Tests/Filter/DateTimeFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |