Skip to content

Commit

Permalink
added ARRAY_MATCH filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Aug 16, 2017
1 parent e5bba5e commit 99a9c94
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ foreach ($qb->getResults() as $element){
* `<=`
* `>=`
* `!=`
* `ARRAY`
* `ARRAY_INVERSED`
* `IN_ARRAY`
* `IN_ARRAY_INVERSED`
* `ARRAY_MATCH`
* `CONTAINS` (case insensitive)

### Avaliable sorting operators
Expand Down
24 changes: 24 additions & 0 deletions src/Filters/Criterion/ArrayMatchFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class ArrayMatchFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
{
return count(array_intersect($value, $valueToCompare)) ? true : false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ArrayQuery\Filters\Criterion;

class ArrayFilter implements FilterInterface
class InArrayFilter implements FilterInterface
{
/**
* @param $value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ArrayQuery\Filters\Criterion;

class ArrayInversedFilter implements FilterInterface
class InArrayInversedFilter implements FilterInterface
{
/**
* @param $value
Expand Down
5 changes: 3 additions & 2 deletions src/Filters/CriterionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ class CriterionFilter extends AbstractFilter
'<' => 'LessThanFilter',
'<=' => 'LessThanEqualsFilter',
'!=' => 'NotEqualsFilter',
'ARRAY' => 'ArrayFilter',
'ARRAY_INVERSED' => 'ArrayInversedFilter',
'IN_ARRAY' => 'InArrayFilter',
'IN_ARRAY_INVERSED' => 'InArrayInversedFilter',
'ARRAY_MATCH' => 'ArrayMatchFilter',
'CONTAINS' => 'ContainsFilter',
];

Expand Down
27 changes: 22 additions & 5 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function it_should_get_results_from_a_array_query()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('name', ['Leanne Graham', 'Ervin Howell', 'Clementine Bauch'], 'ARRAY');
->addCriterion('name', ['Leanne Graham', 'Ervin Howell', 'Clementine Bauch'], 'IN_ARRAY');

$this->assertCount(3, $qb->getResults());
}
Expand All @@ -260,11 +260,11 @@ public function it_should_get_results_from_a_array_query()
/**
* @test
*/
public function it_should_get_results_from_a_array_inversed_query()
public function it_should_get_results_from_a_IN_ARRAY_INVERSED_query()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('tags', 'pinapple', 'ARRAY_INVERSED')
->addCriterion('tags', 'pinapple', 'IN_ARRAY_INVERSED')
->sortedBy('name', 'ASC');

$results = $qb->getResults();
Expand All @@ -274,6 +274,23 @@ public function it_should_get_results_from_a_array_inversed_query()
}
}

/**
* @test
*/
public function it_should_get_results_from_a_array_match_query()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('tags', ['pear-pie', 'apple-pie'], 'ARRAY_MATCH')
->sortedBy('name', 'ASC');

$results = $qb->getResults();

$this->assertCount(6, $results);
$this->assertEquals('Clementina DuBuque', $results[0]['name']);
}
}

/**
* @test
*/
Expand All @@ -296,11 +313,11 @@ public function it_should_get_results_from_a_concatenated_query()
/**
* @test
*/
public function it_should_get_results_from_a_array_inversed_query_with_sorting_and_limits()
public function it_should_get_results_from_a_IN_ARRAY_INVERSED_query_with_sorting_and_limits()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('tags', 'pinapple', 'ARRAY_INVERSED')
->addCriterion('tags', 'pinapple', 'IN_ARRAY_INVERSED')
->sortedBy('id', 'DESC')
->limit(0, 3);

Expand Down
19 changes: 13 additions & 6 deletions tests/files/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"tags": [
"apple",
"pear"
"pear",
"apple-pie"
]
},
{
Expand Down Expand Up @@ -51,7 +52,9 @@
"tags": [
"apple",
"pinapple",
"pear"
"pear",
"apple-pie",
"pear-pie"
]
},
{
Expand Down Expand Up @@ -79,7 +82,8 @@
"tags": [
"apple",
"pinapple",
"pear"
"pear",
"apple-pie"
]
},
{
Expand Down Expand Up @@ -107,7 +111,8 @@
"tags": [
"apple",
"pinapple",
"pear"
"pear",
"apple-pie"
]
},
{
Expand Down Expand Up @@ -247,7 +252,8 @@
"tags": [
"apple",
"pinapple",
"pear"
"pear",
"pear-pie"
]
},
{
Expand Down Expand Up @@ -275,7 +281,8 @@
"tags": [
"apple",
"pinapple",
"pear"
"pear",
"pear-pie"
]
}
]

0 comments on commit 99a9c94

Please sign in to comment.