From 57315992437bda6631cda7758af10abc7a689823 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Tue, 24 Oct 2017 12:58:52 +0200 Subject: [PATCH] added STARTS_WITH and ENDS_WITH filters --- README.md | 14 +++++---- src/Filters/AbstractFilter.php | 2 +- src/Filters/Criterion/EndsWithFilter.php | 29 +++++++++++++++++ src/Filters/Criterion/StartsWithFilter.php | 27 ++++++++++++++++ src/Filters/CriterionFilter.php | 12 +++++--- src/Filters/SortingFilter.php | 2 +- tests/QueryBuilderTest.php | 36 ++++++++++++++++++++++ 7 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 src/Filters/Criterion/EndsWithFilter.php create mode 100644 src/Filters/Criterion/StartsWithFilter.php diff --git a/README.md b/README.md index e60d08c..35e85dc 100644 --- a/README.md +++ b/README.md @@ -96,15 +96,17 @@ foreach ($qb->getResults() as $element){ * `<=` * `>=` * `!=` +* `ARRAY_MATCH` +* `CONTAINS` (case insensitive) +* `ENDS_WITH` +* `EQUALS_DATE` * `GT_DATE` * `GTE_DATE` -* `LT_DATE` -* `LTE_DATE` -* `EQUALS_DATE` * `IN_ARRAY` * `IN_ARRAY_INVERSED` -* `ARRAY_MATCH` -* `CONTAINS` (case insensitive) +* `LT_DATE` +* `LTE_DATE` +* `STARTS_WITH` ### Avaliable sorting operators @@ -122,7 +124,7 @@ use ArrayQuery\QueryBuilder; $qb = QueryBuilder::create($array); $qb - ->addCriterion('title', ['Leanne'], 'ARRAY') + ->addCriterion('title', ['Leanne'], 'IN_ARRAY') ->addCriterion('rate', '3', '>') ->sortedBy('title') ->limit(0, 10); diff --git a/src/Filters/AbstractFilter.php b/src/Filters/AbstractFilter.php index 092dc19..2b5e2c8 100644 --- a/src/Filters/AbstractFilter.php +++ b/src/Filters/AbstractFilter.php @@ -37,7 +37,7 @@ private static function convertObjectToArray($arrayElement) { $convertedArray = []; - foreach ((array)$arrayElement as $key => $element){ + foreach ((array)$arrayElement as $key => $element) { $key = explode("\\", $key); $key = end($key); $key = explode("\000", $key); diff --git a/src/Filters/Criterion/EndsWithFilter.php b/src/Filters/Criterion/EndsWithFilter.php new file mode 100644 index 0000000..253f771 --- /dev/null +++ b/src/Filters/Criterion/EndsWithFilter.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ArrayQuery\Filters\Criterion; + +class EndsWithFilter implements FilterInterface +{ + /** + * @param $value + * @param $valueToCompare + * @return bool + */ + public function match($value, $valueToCompare, $dateFormat = null) + { + $valueToCompareLenght = strlen($valueToCompare); + $valueLenght = strlen($value); + $starts = $valueLenght-$valueToCompareLenght; + $valueFoot = substr($value, $starts, $valueToCompareLenght); + + return $valueToCompare === $valueFoot; + } +} diff --git a/src/Filters/Criterion/StartsWithFilter.php b/src/Filters/Criterion/StartsWithFilter.php new file mode 100644 index 0000000..fb3b2f6 --- /dev/null +++ b/src/Filters/Criterion/StartsWithFilter.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ArrayQuery\Filters\Criterion; + +class StartsWithFilter implements FilterInterface +{ + /** + * @param $value + * @param $valueToCompare + * @return bool + */ + public function match($value, $valueToCompare, $dateFormat = null) + { + $valueToCompareLenght = strlen($valueToCompare); + $valueHead = substr($value, 0, $valueToCompareLenght); + + return $valueToCompare === $valueHead; + } +} diff --git a/src/Filters/CriterionFilter.php b/src/Filters/CriterionFilter.php index 1388716..821c763 100644 --- a/src/Filters/CriterionFilter.php +++ b/src/Filters/CriterionFilter.php @@ -24,15 +24,17 @@ class CriterionFilter extends AbstractFilter '<' => 'LessThanFilter', '<=' => 'LessThanEqualsFilter', '!=' => 'NotEqualsFilter', + 'ARRAY_MATCH' => 'ArrayMatchFilter', + 'CONTAINS' => 'ContainsFilter', + 'ENDS_WITH' => 'EndsWithFilter', + 'EQUALS_DATE' => 'EqualsDateFilter', 'GT_DATE' => 'GreaterThanDateFilter', 'GTE_DATE' => 'GreaterThanEqualsDateFilter', - 'LT_DATE' => 'LessThanDateFilter', - 'LTE_DATE' => 'LessThanEqualsDateFilter', - 'EQUALS_DATE' => 'EqualsDateFilter', 'IN_ARRAY' => 'InArrayFilter', 'IN_ARRAY_INVERSED' => 'InArrayInversedFilter', - 'ARRAY_MATCH' => 'ArrayMatchFilter', - 'CONTAINS' => 'ContainsFilter', + 'LT_DATE' => 'LessThanDateFilter', + 'LTE_DATE' => 'LessThanEqualsDateFilter', + 'STARTS_WITH' => 'StartsWithFilter', ]; /** diff --git a/src/Filters/SortingFilter.php b/src/Filters/SortingFilter.php index ca55ecf..6ce283b 100644 --- a/src/Filters/SortingFilter.php +++ b/src/Filters/SortingFilter.php @@ -47,7 +47,7 @@ private static function sort($results, $sortingArray) $valueA = self::getArrayElementValueFromKey($sortingArray['key'], $first); $valueB = self::getArrayElementValueFromKey($sortingArray['key'], $second); - if(isset($sortingArray['format'])){ + if (isset($sortingArray['format'])) { $valueA = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueA); $valueB = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueB); } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 805aa5a..ee87b6b 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -346,6 +346,42 @@ public function it_should_get_results_from_a_query_with_a_nested_key() } } + /** + * @test + */ + public function it_should_get_results_from_a_query_with_starts_with() + { + foreach ($this->usersArrays as $array) { + $qb = QueryBuilder::create($array) + ->addCriterion('username', 'Ka', 'STARTS_WITH') + ->sortedBy('username', 'ASC'); + + $results = $qb->getResults(); + + $this->assertEquals(2, $qb->getCount()); + $this->assertEquals(5, $results[0]['id']); + $this->assertEquals('Kamren', $results[0]['username']); + } + } + + /** + * @test + */ + public function it_should_get_results_from_a_query_with_ends_with() + { + foreach ($this->usersArrays as $array) { + $qb = QueryBuilder::create($array) + ->addCriterion('email', 'biz', 'ENDS_WITH') + ->sortedBy('email', 'DESC'); + + $results = $qb->getResults(); + + $this->assertEquals(3, $qb->getCount()); + $this->assertEquals(7, $results[0]['id']); + $this->assertEquals('Telly.Hoeger@billy.biz', $results[0]['email']); + } + } + /** * @test */