-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added STARTS_WITH and ENDS_WITH filters
- Loading branch information
Mauro Cassani
committed
Oct 24, 2017
1 parent
0bafe7c
commit 5731599
Showing
7 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|