Skip to content

Commit 4a246c5

Browse files
committed
Support multiple operators as defined in spec v0.4
1 parent 6959037 commit 4a246c5

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/QueryExpressionFilter.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,25 @@ private function matchValue($data, $column, $expectation)
124124
throw new DomainException('Unknown combinator "' . $column . '"');
125125
}
126126

127-
$expectedValue = $expectation;
128-
$comparator = '$is';
129-
130-
if ($this->isVector($expectedValue)) {
131-
// list of possible values
132-
$comparator = '$in';
133-
} elseif ($this->isObject($expectedValue)) {
134-
// custom comparator ('>', '<', ...)
135-
$comparator = key($expectedValue);
136-
$expectedValue = reset($expectedValue);
127+
if ($this->isVector($expectation)) {
128+
// L2 simple list matching
129+
$expectation = array('$in' => $expectation);
130+
} elseif (!$this->isObject($expectation)) {
131+
// L2 simple scalar matching
132+
$expectation = array('$is' => $expectation);
137133
}
138134

139135
$actualValue = $this->fetchValue($data, $column);
140136

141-
return $this->matchComparator($actualValue, $comparator, $expectedValue);
137+
foreach ($expectation as $comparator => $expectedValue) {
138+
$ret = $this->matchComparator($actualValue, $comparator, $expectedValue);
139+
140+
if (!$ret) {
141+
return false;
142+
}
143+
}
144+
145+
return true;
142146
}
143147

144148
private function fetchValue($data, $column)

tests/QueryExpressionFilterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,21 @@ public function testAttributeContainsObject()
431431
)));
432432
}
433433

434+
public function testMultipleOperators()
435+
{
436+
$filter = new QueryExpressionFilter(array(
437+
'id' => array(
438+
'$gte' => 100,
439+
'$lt' => 200
440+
)
441+
));
442+
443+
$this->assertFalse($filter->doesMatch(array('id' => 99)));
444+
$this->assertTrue($filter->doesMatch(array('id' => 100)));
445+
$this->assertTrue($filter->doesMatch(array('id' => 199)));
446+
$this->assertFalse($filter->doesMatch(array('id' => 200)));
447+
}
448+
434449
/**
435450
* @expectedException DomainException
436451
*/

0 commit comments

Comments
 (0)