Skip to content

Commit

Permalink
added DATE_DESC and DATE_ASC sorting operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Oct 21, 2017
1 parent 2848445 commit c9b92f5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ foreach ($qb->getResults() as $element){

* `ASC` (default operator, can be omitted)
* `DESC`
* `DATE_ASC`
* `DATE_DESC`

## Performing Queries

Expand All @@ -132,7 +134,7 @@ foreach ($qb->getResults() as $element){

## Working with dates

You can perform queries based on datetime fields. You must specify **date format** if your format is not `YYYY-mm-dd`:
You can perform queries based on datetime fields. You can use `DATE_ASC` or `DATE_DESC` operator to sort results by date. You must specify **date format** if your format is not `YYYY-mm-dd`:

```php
use ArrayQuery\QueryBuilder;
Expand All @@ -141,7 +143,7 @@ $qb = QueryBuilder::create($array);
$qb
->addCriterion('registration_date', '01/05/2017', 'GT_DATE', 'd/m/Y')
->addCriterion('rate', '3', '>')
->sortedBy('title')
->sortedBy('registration_date', `DATE_DESC`, 'd/m/Y')
->limit(0, 10);

foreach ($qb->getResults() as $element){
Expand Down
11 changes: 9 additions & 2 deletions src/Filters/SortingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class SortingFilter extends AbstractFilter
public static $operatorsMap = [
'ASC',
'DESC',
'DATE_ASC',
'DATE_DESC',
];

/**
Expand Down Expand Up @@ -45,14 +47,19 @@ private static function sort($results, $sortingArray)
$valueA = self::getArrayElementValueFromKey($sortingArray['key'], $first);
$valueB = self::getArrayElementValueFromKey($sortingArray['key'], $second);

if ($valueA === $valueB) {
if(isset($sortingArray['format'])){
$valueA = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueA);
$valueB = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueB);
}

if ($valueA == $valueB) {
return 0;
}

return ($valueA < $valueB) ? -1 : 1;
});

if ($sortingArray['order'] === 'DESC') {
if ($sortingArray['order'] === 'DESC' || $sortingArray['order'] === 'DATE_DESC') {
return array_reverse($results);
}

Expand Down
3 changes: 2 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function isAValidCriterionOperator($operator)
*
* @throws NotValidSortingOperatorException
*/
public function sortedBy($key, $operator = 'ASC')
public function sortedBy($key, $operator = 'ASC', $format = null)
{
if (!$this->isAValidSortingOperator($operator)) {
throw new NotValidSortingOperatorException($operator.' is not a valid sorting operator.');
Expand All @@ -124,6 +124,7 @@ public function sortedBy($key, $operator = 'ASC')
$this->sortedBy = [
'key' => $key,
'order' => $operator,
'format' => $format
];

return $this;
Expand Down
36 changes: 36 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,40 @@ public function it_should_get_results_from_a_query_with_equals_date()
$this->assertEquals('Chelsey Dietrich', $results[0]['name']);
}
}

/**
* @test
*/
public function it_should_get_results_from_a_query_with_equals_date_sorted_by_date_asc()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('registration_date', '01/05/2017', 'LTE_DATE', 'd/m/Y')
->sortedBy('registration_date', 'DATE_ASC', 'd/m/Y');

$results = $qb->getResults();

$this->assertEquals(4, $qb->getCount());
$this->assertEquals(10, $results[0]['id']);
$this->assertEquals('Clementina DuBuque', $results[0]['name']);
}
}

/**
* @test
*/
public function it_should_get_results_from_a_query_with_equals_date_sorted_by_date_desc()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array)
->addCriterion('registration_date', '01/05/2017', 'LTE_DATE', 'd/m/Y')
->sortedBy('registration_date', 'DATE_DESC', 'd/m/Y');

$results = $qb->getResults();

$this->assertEquals(4, $qb->getCount());
$this->assertEquals(3, $results[0]['id']);
$this->assertEquals('Clementine Bauch', $results[0]['name']);
}
}
}

0 comments on commit c9b92f5

Please sign in to comment.