-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fiter items through `Result`. Fix #390 * Fix coding style * remove backslash * Advertise filtering ability
- Loading branch information
1 parent
f5f06ea
commit 9b726b1
Showing
7 changed files
with
191 additions
and
6 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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FeedIo\Filter; | ||
|
||
use FeedIo\FeedInterface; | ||
|
||
class Chain | ||
{ | ||
private array $filters; | ||
|
||
public function add(FilterInterface $filter): void | ||
{ | ||
$this->filters[] = $filter; | ||
} | ||
|
||
public function filter(FeedInterface $feed): iterable | ||
{ | ||
foreach ($feed as $item) { | ||
foreach ($this->filters as $filter) { | ||
if (!$filter->filter($item)) { | ||
continue 2; | ||
} | ||
} | ||
|
||
yield $item; | ||
} | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FeedIo\Filter; | ||
|
||
use FeedIo\Feed\ItemInterface; | ||
|
||
interface FilterInterface | ||
{ | ||
/** | ||
* Returns `true` if the item is to be returned. | ||
* | ||
* @param ItemInterface $item | ||
* @return bool | ||
*/ | ||
public function filter(ItemInterface $item): bool; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FeedIo\Filter; | ||
|
||
use DateTime; | ||
use FeedIo\Feed\ItemInterface; | ||
|
||
class Since implements FilterInterface | ||
{ | ||
private DateTime $date; | ||
|
||
public function __construct(DateTime $date) | ||
{ | ||
$this->date = $date; | ||
} | ||
|
||
public function filter(ItemInterface $item): bool | ||
{ | ||
return $item->getLastModified() >= $this->date; | ||
} | ||
} |
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,49 @@ | ||
<?php | ||
|
||
namespace FeedIo\Filter; | ||
|
||
use FeedIo\Feed; | ||
use FeedIo\Feed\Item; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ChainTest extends TestCase | ||
{ | ||
public function testFilter() | ||
{ | ||
$chain = new Chain(); | ||
$chain->add(new Since(new \DateTime('-1 day'))); | ||
|
||
$feed = new Feed(); | ||
$feed->add((new Item())->setLastModified(new \DateTime('-1 hour'))); | ||
$feed->add((new Item())->setLastModified(new \DateTime('-1 day'))); | ||
$feed->add((new Item())->setLastModified(new \DateTime('-1 month'))); | ||
|
||
$filtered = $chain->filter($feed); | ||
$this->assertEquals(2, iterator_count($filtered)); | ||
} | ||
|
||
public function testFancyFilter() | ||
{ | ||
$chain = new Chain(); | ||
$fancyFilter = $this->getMockForAbstractClass('\FeedIo\Filter\FilterInterface'); | ||
$fancyFilter->expects($this->exactly(2))->method('filter')->will($this->returnCallback(function (Item $item) { | ||
return $item->getTitle() === '1 day ago'; | ||
})); | ||
|
||
$chain->add(new Since(new \DateTime('-1 day'))); | ||
$chain->add($fancyFilter); | ||
|
||
$feed = new Feed(); | ||
$feed->add((new Item())->setTitle('1 hour ago')->setLastModified(new \DateTime('-1 hour'))); | ||
$feed->add((new Item())->setTitle('1 day ago')->setLastModified(new \DateTime('-1 day'))); | ||
$feed->add((new Item())->setTitle('1 month ago')->setLastModified(new \DateTime('-1 month'))); | ||
|
||
$filtered = $chain->filter($feed); | ||
$count = 0; | ||
foreach ($filtered as $item) { | ||
$count++; | ||
} | ||
$this->assertEquals(1, $count); | ||
$this->assertEquals('1 day ago', $item->getTitle()); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace FeedIo\Filter; | ||
|
||
use FeedIo\Feed\Item; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FilterTest extends TestCase | ||
{ | ||
public function testFilter() | ||
{ | ||
$filter = new Since(new \DateTime('-1 day')); | ||
$this->assertTrue($filter->filter((new Item())->setLastModified(new \DateTime('-1 hour')))); | ||
$this->assertTrue($filter->filter((new Item())->setLastModified(new \DateTime('-1 day')))); | ||
$this->assertFalse($filter->filter((new Item())->setLastModified(new \DateTime('-2 day')))); | ||
} | ||
} |