Skip to content

Commit

Permalink
Create OneOf filter
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Sep 3, 2014
1 parent 9a11c3b commit 21b9401
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ $criteria->foo->equalTo(2)
->corge->between(array(1, 42))
->grault->lessThan(1000)
->garply->greaterThan(0)
->waldo->notEqualTo(false);
->waldo->notEqualTo(false)
->fred->greaterThanOrEqualTo(13);

$storage->users->find($criteria);
```
7 changes: 7 additions & 0 deletions src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function __call($methodName, array $arguments = array())
if (0 === strpos($methodName, 'not')) {
$shortName = substr($methodName, 3);
$filter = new Not($this->newFilterInstance($shortName, $arguments));
} elseif (false !== strpos($methodName, 'Or')) {
$pieces = explode('Or', $methodName);
$filters = array();
foreach ($pieces as $shortName) {
$filters[] = $this->newFilterInstance($shortName, $arguments);
}
$filter = new OneOf($filters);
} else {
$filter = $this->newFilterInstance($methodName, $arguments);
}
Expand Down
38 changes: 38 additions & 0 deletions src/Filter/OneOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace PHPFluent\ArrayStorage\Filter;

use InvalidArgumentException;

class OneOf implements Filter
{
protected $filters = array();

public function __construct(array $filters)
{
foreach ($filters as $filter) {
if (! $filter instanceof Filter) {
throw new InvalidArgumentException('Filter is not valid');
}
$this->filters[] = $filter;
}
}

public function getFilters()
{
return $this->filters;
}

public function isValid($input)
{
foreach ($this->getFilters() as $filter) {
if (! $filter->isValid($input)) {
continue;
}

return true;
}

return false;
}
}
11 changes: 11 additions & 0 deletions tests/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,15 @@ public function testShouldUseNotFilterWhenUsingTheWordNotInTheFilterName()

$this->assertFalse($criteria->isValid($record));
}

public function testShouldUseOneOfFilterWhenUsingTheWordOrInTheFilterName()
{
$criteria = new Criteria();
$criteria->foo->greaterThanOrEqualTo(42);

$record = new Record();
$record->foo = 42;

$this->assertTrue($criteria->isValid($record));
}
}
85 changes: 85 additions & 0 deletions tests/Filter/OneOfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PHPFluent\ArrayStorage\Filter;

/**
* @covers PHPFluent\ArrayStorage\Filter\OneOf
*/
class OneOfTest extends \PHPUnit_Framework_TestCase
{
protected function filter($return = false)
{
return $this->getMock('PHPFluent\ArrayStorage\Filter\Filter');
}

public function testShouldAcceptFiltersOnConstructor()
{
$filters = array(
$this->filter(),
$this->filter(),
);
$filter = new OneOf($filters);

$this->assertSame($filters, $filter->getFilters());
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Filter is not valid
*/
public function testShouldThrowsExceptionWhenFilterIsNotValid()
{
$filters = array(
$this->filter(),
'foo',
);
$filter = new OneOf($filters);
}

public function testShouldReturnFalseIfAllFiltersAreInvalid()
{
$filter1 = $this->filter();
$filter1
->expects($this->once())
->method('isValid')
->will($this->returnValue(false));

$filter2 = $this->filter();
$filter2
->expects($this->once())
->method('isValid')
->will($this->returnValue(false));

$filters = array(
$filter1,
$filter2,
);

$filter = new OneOf($filters);

$this->assertFalse($filter->isValid('Value'));
}

public function testShouldReturnTrueIfAtLeastOneFilterIsValid()
{
$filter1 = $this->filter();
$filter1
->expects($this->once())
->method('isValid')
->will($this->returnValue(true));

$filter2 = $this->filter();
$filter2
->expects($this->never())
->method('isValid');

$filters = array(
$filter1,
$filter2,
);

$filter = new OneOf($filters);

$this->assertTrue($filter->isValid('Value'));
}
}

0 comments on commit 21b9401

Please sign in to comment.