Skip to content

Commit 0f4c1f7

Browse files
committed
Allow to create filters from operators
1 parent b7aeb2e commit 0f4c1f7

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ The example above convert all Record objects into an array.
101101
## Finding multiple records into a collection
102102

103103
```php
104-
$storage->users->findAll(array('name' => 'Henrique Moody')); // Return an Collection object with the partial result (if any)
104+
$storage->users->findAll(array('status !=' => false)); // Return an Collection object with the partial result (if any)
105105
```
106106

107107
## Finding single record into a collection
108108

109109
```php
110-
$storage->users->find(array('name' => 'Henrique Moody')); // Return an Record object with the first matched result (if any) or NULL
110+
$storage->users->find(array('priority >=' => 4)); // Return an Record object with the first matched result (if any) or NULL
111111
```
112112

113113
## Using Criteria object

src/Factory.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@
1111

1212
class Factory
1313
{
14+
protected $operatorsToFilters = array(
15+
'=' => 'equalTo',
16+
'!=' => 'notEqualTo',
17+
'<' => 'lessThan',
18+
'<=' => 'lessThanOrEqualTo',
19+
'>' => 'greaterThan',
20+
'>=' => 'greaterThanOrEqualTo',
21+
'BETWEEN' => 'between',
22+
'NOT BETWEEN' => 'notBetween',
23+
'ILIKE' => 'iLike',
24+
'NOT ILIKE' => 'notILike',
25+
'IN' => 'in',
26+
'NOT IN' => 'notIn',
27+
'LIKE' => 'like',
28+
'NOT LIKE' => 'notLike',
29+
'REGEX' => 'regex',
30+
'NOT REGEX' => 'notRegex',
31+
);
32+
1433
public function collection($collection = null)
1534
{
1635
if (! $collection instanceof Collection) {
@@ -41,7 +60,14 @@ public function criteria($criteria = null)
4160
continue;
4261
}
4362

44-
$criteria->addFilter($key, new EqualTo($value));
63+
$index = strstr($key, ' ', true) ?: $key;
64+
$operator = trim(strstr($key, ' ') ?: '=');
65+
if (false === strpos($operator, 'BETWEEN')) {
66+
$value = array($value);
67+
}
68+
$filter = $this->filter($operator, $value);
69+
70+
$criteria->addFilter($index, $filter);
4571
}
4672
}
4773

@@ -51,6 +77,11 @@ public function criteria($criteria = null)
5177
public function filter($filter, array $arguments = array())
5278
{
5379
if (! $filter instanceof Filter) {
80+
81+
if (isset($this->operatorsToFilters[$filter])) {
82+
return $this->filter($this->operatorsToFilters[$filter], $arguments);
83+
}
84+
5485
if (0 === strpos($filter, 'not')) {
5586
$filterName = substr($filter, 3);
5687
$filter = $this->filter($filterName, $arguments);

tests/FactoryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ public function testShouldCreateACriteriaFromAKeyValueArrayOfNonFilters()
9898
$this->assertEquals($expectedFilter, $actualFilters[0][1]);
9999
}
100100

101+
public function operatorsProvider()
102+
{
103+
return array(
104+
array('=', 42, new Filter\EqualTo(42)),
105+
array('!=', 42, new Filter\Not(new Filter\EqualTo(42))),
106+
array('<', 42, new Filter\LessThan(42)),
107+
array('<=', 42, new Filter\OneOf(array(new Filter\LessThan(42), new Filter\EqualTo(42)))),
108+
array('>', 42, new Filter\GreaterThan(42)),
109+
array('>=', 42, new Filter\OneOf(array(new Filter\GreaterThan(42), new Filter\EqualTo(42)))),
110+
array('BETWEEN', array(1, 3), new Filter\Between(1, 3)),
111+
array('NOT BETWEEN', array(1, 3), new Filter\Not(new Filter\Between(1, 3))),
112+
array('ILIKE', 'String%', new Filter\ILike('String%')),
113+
array('NOT ILIKE', 'String%', new Filter\Not(new Filter\ILike('String%'))),
114+
array('IN', array(1, 2, 3), new Filter\In(array(1, 2, 3))),
115+
array('NOT IN', array(1, 2, 3), new Filter\Not(new Filter\In(array(1, 2, 3)))),
116+
array('LIKE', 'String%', new Filter\Like('String%')),
117+
array('NOT LIKE', 'String%', new Filter\Not(new Filter\Like('String%'))),
118+
array('REGEX', '/[a-z]/', new Filter\Regex('/[a-z]/')),
119+
array('NOT REGEX', '/[a-z]/', new Filter\Not(new Filter\Regex('/[a-z]/'))),
120+
);
121+
}
122+
123+
/**
124+
* @dataProvider operatorsProvider
125+
*/
126+
public function testShouldCreateACriteriaFromAKeyValueArrayOfNonFiltersUsingOperator($operator, $value, $expectedFilter)
127+
{
128+
$inputFilters = array(
129+
'name ' . $operator => $value,
130+
);
131+
$factory = new Factory();
132+
$criteria = $factory->criteria($inputFilters);
133+
$actualFilters = $criteria->getFilters();
134+
135+
$this->assertEquals($expectedFilter, $actualFilters[0][1]);
136+
}
137+
101138
public function testShouldReturnInputIfInputIsAlreadyAFilter()
102139
{
103140
$filter = new Filter\EqualTo(42, 'identical');

0 commit comments

Comments
 (0)