Skip to content

Commit

Permalink
alias feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Jan 5, 2018
1 parent 87df9bc commit 1c3dae5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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;

interface Constants
{
const ARRAY_SEPARATOR = '.';
const ALIAS_DELIMITER = ' as ';
}
5 changes: 2 additions & 3 deletions src/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

namespace ArrayQuery\Filters;

use ArrayQuery\Constants;
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;

abstract class AbstractFilter
{
const ARRAY_SEPARATOR = '.';

/**
* @param $key
* @param $arrayElement
Expand All @@ -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
);
}
Expand Down
12 changes: 9 additions & 3 deletions src/Filters/CriterionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ArrayQuery\Filters;

use ArrayQuery\Constants;
use ArrayQuery\Filters\Criterion\FilterInterface;

class CriterionFilter extends AbstractFilter
Expand Down Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}

0 comments on commit 1c3dae5

Please sign in to comment.