Skip to content

Commit 37f1647

Browse files
committed
Merge pull request #10 from clue-labs/contains
Add new $contains comparator from spec v0.4
2 parents da0fac3 + bfd205e commit 37f1647

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/QueryExpressionFilter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ private function matchValue($data, $column, $expectation)
111111
return ($actualValue === $expectedValue);
112112
} elseif ($comparator === '$in') {
113113
return in_array($actualValue, $expectedValue, true);
114+
} elseif ($comparator === '$contains') {
115+
if ($this->isObject($actualValue)) {
116+
if (is_object($actualValue)) {
117+
return property_exists($actualValue, $expectedValue);
118+
} else {
119+
return array_key_exists($expectedValue, $actualValue);
120+
}
121+
} elseif ($this->isVector($actualValue)) {
122+
return in_array($expectedValue, $actualValue, true);
123+
} else {
124+
return (strpos($actualValue, $expectedValue) !== false);
125+
}
114126
} elseif ($comparator === '$lt') {
115127
return ($actualValue < $expectedValue);
116128
} elseif ($comparator === '$lte') {

tests/QueryExpressionFilterTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,78 @@ public function testAttributeNotList()
349349
$this->assertTrue($filter->doesMatch(array('id' => 300)));
350350
}
351351

352+
public function testAttributeContainsString()
353+
{
354+
$filter = new QueryExpressionFilter(array(
355+
'key' => array(
356+
'$contains' => 'value'
357+
)
358+
));
359+
360+
$this->assertTrue($filter->doesMatch(array(
361+
'key' => 'the value exist'
362+
)));
363+
$this->assertFalse($filter->doesMatch(array(
364+
'key' => 'empty'
365+
)));
366+
}
367+
368+
public function testAttributeContainsArray()
369+
{
370+
$filter = new QueryExpressionFilter(array(
371+
'key' => array(
372+
'$contains' => 'value'
373+
)
374+
));
375+
376+
$this->assertTrue($filter->doesMatch(array(
377+
'key' => array('the', 'value', 'exists')
378+
)));
379+
$this->assertFalse($filter->doesMatch(array(
380+
'key' => array('not', 'present')
381+
)));
382+
}
383+
384+
public function testAttributeContainsAssoc()
385+
{
386+
$filter = new QueryExpressionFilter(array(
387+
'key' => array(
388+
'$contains' => 'value'
389+
)
390+
));
391+
392+
$this->assertTrue($filter->doesMatch(array(
393+
'key' => array(
394+
'value' => 123
395+
)
396+
)));
397+
$this->assertFalse($filter->doesMatch(array(
398+
'key' => array(
399+
'not' => 'present'
400+
)
401+
)));
402+
}
403+
404+
public function testAttributeContainsObject()
405+
{
406+
$filter = new QueryExpressionFilter(array(
407+
'key' => array(
408+
'$contains' => 'value'
409+
)
410+
));
411+
412+
$this->assertTrue($filter->doesMatch((object)array(
413+
'key' => (object)array(
414+
'value' => 123
415+
)
416+
)));
417+
$this->assertFalse($filter->doesMatch((object)array(
418+
'key' => (object)array(
419+
'not' => 'present'
420+
)
421+
)));
422+
}
423+
352424
/**
353425
* @expectedException DomainException
354426
*/
@@ -407,6 +479,24 @@ public function testInvalidOr()
407479
$filter->doesMatch(array('id' => 100));
408480
}
409481

482+
public function testAttributeContains()
483+
{
484+
$filter = new QueryExpressionFilter(array(
485+
'name' => array(
486+
'$contains' => 'Fried'
487+
)
488+
));
489+
490+
$this->assertFalse($filter->doesMatch(array(
491+
'id' => 100,
492+
'name' => 'Smith, George'
493+
)));
494+
$this->assertTrue($filter->doesMatch(array(
495+
'id' => 300,
496+
'name' => 'Smith, Friedrich'
497+
)));
498+
}
499+
410500
/**
411501
* @dataProvider dpFetchValue
412502
*/

0 commit comments

Comments
 (0)