From 1c3dae500d21145ab8c3b74b1f74086f4ee1d5e8 Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Fri, 5 Jan 2018 11:54:41 +0100 Subject: [PATCH] alias feature --- README.md | 22 ++++++++++++++++++++++ src/Constants.php | 17 +++++++++++++++++ src/Filters/AbstractFilter.php | 5 ++--- src/Filters/CriterionFilter.php | 12 +++++++++--- src/QueryBuilder.php | 13 +++++++++++++ tests/QueryBuilderTest.php | 18 ++++++++++++++++++ 6 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/Constants.php diff --git a/README.md b/README.md index 0065dae..921f566 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,28 @@ foreach ($qb->getResults() as $element){ } ``` +## Aliases + +You can use aliases by using the `as` keyword as a delimiter. Do the following: + +```php +use ArrayQuery\QueryBuilder; + +$qb = QueryBuilder::create($array); +$qb + ->addCriterion('name as n', 'Ervin Howell') + ->addCriterion('username as user', 'Antonette') + ->addCriterion('address.street as add', 'Victor Plains'); + +foreach ($qb->getResults() as $element){ + // ... + // now you have + // $element['n'] + // $element['user'] + // $element['add'] +} +``` + ## More examples Please refer to [QueryBuilderTest](https://github.com/mauretto78/array-query/blob/master/tests/QueryBuilderTest.php) for more examples. diff --git a/src/Constants.php b/src/Constants.php new file mode 100644 index 0000000..fbc10f1 --- /dev/null +++ b/src/Constants.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ArrayQuery; + +interface Constants +{ + const ARRAY_SEPARATOR = '.'; + const ALIAS_DELIMITER = ' as '; +} diff --git a/src/Filters/AbstractFilter.php b/src/Filters/AbstractFilter.php index 2b5e2c8..a7c248c 100644 --- a/src/Filters/AbstractFilter.php +++ b/src/Filters/AbstractFilter.php @@ -10,12 +10,11 @@ namespace ArrayQuery\Filters; +use ArrayQuery\Constants; use ArrayQuery\Exceptions\NotValidKeyElementInArrayException; abstract class AbstractFilter { - const ARRAY_SEPARATOR = '.'; - /** * @param $key * @param $arrayElement @@ -24,7 +23,7 @@ abstract class AbstractFilter protected static function getArrayElementValueFromKey($key, $arrayElement) { return self::getValueFromKeysArray( - explode(self::ARRAY_SEPARATOR, $key), + explode(Constants::ARRAY_SEPARATOR, $key), (is_object($arrayElement)) ? self::convertObjectToArray($arrayElement) : $arrayElement ); } diff --git a/src/Filters/CriterionFilter.php b/src/Filters/CriterionFilter.php index 821c763..9d0a32e 100644 --- a/src/Filters/CriterionFilter.php +++ b/src/Filters/CriterionFilter.php @@ -10,6 +10,7 @@ namespace ArrayQuery\Filters; +use ArrayQuery\Constants; use ArrayQuery\Filters\Criterion\FilterInterface; class CriterionFilter extends AbstractFilter @@ -44,13 +45,18 @@ class CriterionFilter extends AbstractFilter */ public static function filter($criterion, $element) { - $value = self::getArrayElementValueFromKey($criterion['key'], $element); + $key = explode(Constants::ALIAS_DELIMITER, $criterion['key']); $valueToCompare = $criterion['value']; - $filterClass = 'ArrayQuery\\Filters\\Criterion\\'.self::$operatorsMap[$criterion['operator']]; + $operator = $criterion['operator']; + $dateFormat = $criterion['date_format']; + + $value = self::getArrayElementValueFromKey($key[0], $element); + + $filterClass = 'ArrayQuery\\Filters\\Criterion\\'.self::$operatorsMap[$operator]; /** @var FilterInterface $filter */ $filter = new $filterClass(); - return $filter->match($value, $valueToCompare, $criterion['date_format']); + return $filter->match($value, $valueToCompare, $dateFormat); } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 11fcb87..c8e588f 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -268,6 +268,19 @@ private function applyCriteriaFilter(array $array) return CriterionFilter::filter($criterion, $element); } ); + + $results = array_map(function($result) use ($criterion) { + $key = explode(Constants::ALIAS_DELIMITER, $criterion['key']); + if(count($key) > 1){ + $oldkey = explode(Constants::ARRAY_SEPARATOR, $key[0]); + $newkey = $key[1]; + + $result = (array)($result); + $result[$newkey] = $result[$oldkey[0]]; + unset($result[$oldkey[0]]); + } + return $result; + }, $results); } return $results; diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 2e22e64..7b6b842 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -599,4 +599,22 @@ public function it_should_get_results_from_a_query_with_joins() $this->assertEquals($results, $expectedArray); $this->assertEquals(2, $qb->getCount()); } + + /** + * @test + */ + public function it_should_get_results_from_a_query_with_aliases() + { + foreach ($this->usersArrays as $array) { + $qb = QueryBuilder::create($array) + ->addCriterion('name as n', 'Ervin Howell') + ->addCriterion('username as user', 'Antonette') + ->addCriterion('address.street as add', 'Victor Plains'); + + $this->assertCount(1, $qb->getResults()); + $this->assertArrayHasKey('n', $qb->getResults()[1]); + $this->assertArrayHasKey('user', $qb->getResults()[1]); + $this->assertArrayHasKey('add', $qb->getResults()[1]); + } + } }