Skip to content

Commit

Permalink
added STARTS_WITH and ENDS_WITH filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Oct 24, 2017
1 parent 0bafe7c commit 5731599
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 13 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/AbstractFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions src/Filters/Criterion/EndsWithFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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 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;
}
}
27 changes: 27 additions & 0 deletions src/Filters/Criterion/StartsWithFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 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;
}
}
12 changes: 7 additions & 5 deletions src/Filters/CriterionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/SortingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]', $results[0]['email']);
}
}

/**
* @test
*/
Expand Down

0 comments on commit 5731599

Please sign in to comment.