Skip to content

Commit c9b92f5

Browse files
author
Mauro Cassani
committed
added DATE_DESC and DATE_ASC sorting operator
1 parent 2848445 commit c9b92f5

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ foreach ($qb->getResults() as $element){
110110

111111
* `ASC` (default operator, can be omitted)
112112
* `DESC`
113+
* `DATE_ASC`
114+
* `DATE_DESC`
113115

114116
## Performing Queries
115117

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

133135
## Working with dates
134136

135-
You can perform queries based on datetime fields. You must specify **date format** if your format is not `YYYY-mm-dd`:
137+
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`:
136138

137139
```php
138140
use ArrayQuery\QueryBuilder;
@@ -141,7 +143,7 @@ $qb = QueryBuilder::create($array);
141143
$qb
142144
->addCriterion('registration_date', '01/05/2017', 'GT_DATE', 'd/m/Y')
143145
->addCriterion('rate', '3', '>')
144-
->sortedBy('title')
146+
->sortedBy('registration_date', `DATE_DESC`, 'd/m/Y')
145147
->limit(0, 10);
146148

147149
foreach ($qb->getResults() as $element){

src/Filters/SortingFilter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class SortingFilter extends AbstractFilter
1818
public static $operatorsMap = [
1919
'ASC',
2020
'DESC',
21+
'DATE_ASC',
22+
'DATE_DESC',
2123
];
2224

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

48-
if ($valueA === $valueB) {
50+
if(isset($sortingArray['format'])){
51+
$valueA = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueA);
52+
$valueB = \DateTimeImmutable::createFromFormat(($sortingArray['format']) ?: 'Y-m-d', $valueB);
53+
}
54+
55+
if ($valueA == $valueB) {
4956
return 0;
5057
}
5158

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

55-
if ($sortingArray['order'] === 'DESC') {
62+
if ($sortingArray['order'] === 'DESC' || $sortingArray['order'] === 'DATE_DESC') {
5663
return array_reverse($results);
5764
}
5865

src/QueryBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private function isAValidCriterionOperator($operator)
115115
*
116116
* @throws NotValidSortingOperatorException
117117
*/
118-
public function sortedBy($key, $operator = 'ASC')
118+
public function sortedBy($key, $operator = 'ASC', $format = null)
119119
{
120120
if (!$this->isAValidSortingOperator($operator)) {
121121
throw new NotValidSortingOperatorException($operator.' is not a valid sorting operator.');
@@ -124,6 +124,7 @@ public function sortedBy($key, $operator = 'ASC')
124124
$this->sortedBy = [
125125
'key' => $key,
126126
'order' => $operator,
127+
'format' => $format
127128
];
128129

129130
return $this;

tests/QueryBuilderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,40 @@ public function it_should_get_results_from_a_query_with_equals_date()
435435
$this->assertEquals('Chelsey Dietrich', $results[0]['name']);
436436
}
437437
}
438+
439+
/**
440+
* @test
441+
*/
442+
public function it_should_get_results_from_a_query_with_equals_date_sorted_by_date_asc()
443+
{
444+
foreach ($this->usersArrays as $array) {
445+
$qb = QueryBuilder::create($array)
446+
->addCriterion('registration_date', '01/05/2017', 'LTE_DATE', 'd/m/Y')
447+
->sortedBy('registration_date', 'DATE_ASC', 'd/m/Y');
448+
449+
$results = $qb->getResults();
450+
451+
$this->assertEquals(4, $qb->getCount());
452+
$this->assertEquals(10, $results[0]['id']);
453+
$this->assertEquals('Clementina DuBuque', $results[0]['name']);
454+
}
455+
}
456+
457+
/**
458+
* @test
459+
*/
460+
public function it_should_get_results_from_a_query_with_equals_date_sorted_by_date_desc()
461+
{
462+
foreach ($this->usersArrays as $array) {
463+
$qb = QueryBuilder::create($array)
464+
->addCriterion('registration_date', '01/05/2017', 'LTE_DATE', 'd/m/Y')
465+
->sortedBy('registration_date', 'DATE_DESC', 'd/m/Y');
466+
467+
$results = $qb->getResults();
468+
469+
$this->assertEquals(4, $qb->getCount());
470+
$this->assertEquals(3, $results[0]['id']);
471+
$this->assertEquals('Clementine Bauch', $results[0]['name']);
472+
}
473+
}
438474
}

0 commit comments

Comments
 (0)