Skip to content

Commit 21b9401

Browse files
committed
Create OneOf filter
1 parent 9a11c3b commit 21b9401

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ $criteria->foo->equalTo(2)
122122
->corge->between(array(1, 42))
123123
->grault->lessThan(1000)
124124
->garply->greaterThan(0)
125-
->waldo->notEqualTo(false);
125+
->waldo->notEqualTo(false)
126+
->fred->greaterThanOrEqualTo(13);
126127

127128
$storage->users->find($criteria);
128129
```

src/Criteria.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function __call($methodName, array $arguments = array())
6969
if (0 === strpos($methodName, 'not')) {
7070
$shortName = substr($methodName, 3);
7171
$filter = new Not($this->newFilterInstance($shortName, $arguments));
72+
} elseif (false !== strpos($methodName, 'Or')) {
73+
$pieces = explode('Or', $methodName);
74+
$filters = array();
75+
foreach ($pieces as $shortName) {
76+
$filters[] = $this->newFilterInstance($shortName, $arguments);
77+
}
78+
$filter = new OneOf($filters);
7279
} else {
7380
$filter = $this->newFilterInstance($methodName, $arguments);
7481
}

src/Filter/OneOf.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PHPFluent\ArrayStorage\Filter;
4+
5+
use InvalidArgumentException;
6+
7+
class OneOf implements Filter
8+
{
9+
protected $filters = array();
10+
11+
public function __construct(array $filters)
12+
{
13+
foreach ($filters as $filter) {
14+
if (! $filter instanceof Filter) {
15+
throw new InvalidArgumentException('Filter is not valid');
16+
}
17+
$this->filters[] = $filter;
18+
}
19+
}
20+
21+
public function getFilters()
22+
{
23+
return $this->filters;
24+
}
25+
26+
public function isValid($input)
27+
{
28+
foreach ($this->getFilters() as $filter) {
29+
if (! $filter->isValid($input)) {
30+
continue;
31+
}
32+
33+
return true;
34+
}
35+
36+
return false;
37+
}
38+
}

tests/CriteriaTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,15 @@ public function testShouldUseNotFilterWhenUsingTheWordNotInTheFilterName()
120120

121121
$this->assertFalse($criteria->isValid($record));
122122
}
123+
124+
public function testShouldUseOneOfFilterWhenUsingTheWordOrInTheFilterName()
125+
{
126+
$criteria = new Criteria();
127+
$criteria->foo->greaterThanOrEqualTo(42);
128+
129+
$record = new Record();
130+
$record->foo = 42;
131+
132+
$this->assertTrue($criteria->isValid($record));
133+
}
123134
}

tests/Filter/OneOfTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace PHPFluent\ArrayStorage\Filter;
4+
5+
/**
6+
* @covers PHPFluent\ArrayStorage\Filter\OneOf
7+
*/
8+
class OneOfTest extends \PHPUnit_Framework_TestCase
9+
{
10+
protected function filter($return = false)
11+
{
12+
return $this->getMock('PHPFluent\ArrayStorage\Filter\Filter');
13+
}
14+
15+
public function testShouldAcceptFiltersOnConstructor()
16+
{
17+
$filters = array(
18+
$this->filter(),
19+
$this->filter(),
20+
);
21+
$filter = new OneOf($filters);
22+
23+
$this->assertSame($filters, $filter->getFilters());
24+
}
25+
26+
/**
27+
* @expectedException InvalidArgumentException
28+
* @expectedExceptionMessage Filter is not valid
29+
*/
30+
public function testShouldThrowsExceptionWhenFilterIsNotValid()
31+
{
32+
$filters = array(
33+
$this->filter(),
34+
'foo',
35+
);
36+
$filter = new OneOf($filters);
37+
}
38+
39+
public function testShouldReturnFalseIfAllFiltersAreInvalid()
40+
{
41+
$filter1 = $this->filter();
42+
$filter1
43+
->expects($this->once())
44+
->method('isValid')
45+
->will($this->returnValue(false));
46+
47+
$filter2 = $this->filter();
48+
$filter2
49+
->expects($this->once())
50+
->method('isValid')
51+
->will($this->returnValue(false));
52+
53+
$filters = array(
54+
$filter1,
55+
$filter2,
56+
);
57+
58+
$filter = new OneOf($filters);
59+
60+
$this->assertFalse($filter->isValid('Value'));
61+
}
62+
63+
public function testShouldReturnTrueIfAtLeastOneFilterIsValid()
64+
{
65+
$filter1 = $this->filter();
66+
$filter1
67+
->expects($this->once())
68+
->method('isValid')
69+
->will($this->returnValue(true));
70+
71+
$filter2 = $this->filter();
72+
$filter2
73+
->expects($this->never())
74+
->method('isValid');
75+
76+
$filters = array(
77+
$filter1,
78+
$filter2,
79+
);
80+
81+
$filter = new OneOf($filters);
82+
83+
$this->assertTrue($filter->isValid('Value'));
84+
}
85+
}

0 commit comments

Comments
 (0)