Skip to content

Commit

Permalink
Add nulls_always_first and nulls_always_last to nulls_comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
famoser committed Feb 28, 2021
1 parent 94863cd commit c44858b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Bridge/Doctrine/Common/Filter/OrderFilterInterface.php
Expand Up @@ -24,6 +24,8 @@ interface OrderFilterInterface
{
public const DIRECTION_ASC = 'ASC';
public const DIRECTION_DESC = 'DESC';
public const NULLS_ALWAYS_FIRST = 'nulls_always_first';
public const NULLS_ALWAYS_LAST = 'nulls_always_last';
public const NULLS_SMALLEST = 'nulls_smallest';
public const NULLS_LARGEST = 'nulls_largest';
public const NULLS_DIRECTION_MAP = [
Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Doctrine/Orm/Filter/OrderFilter.php
Expand Up @@ -103,7 +103,11 @@ protected function filterProperty(string $property, $direction, QueryBuilder $qu
}

if (null !== $nullsComparison = $this->properties[$property]['nulls_comparison'] ?? null) {
$nullsDirection = self::NULLS_DIRECTION_MAP[$nullsComparison][$direction];
if (\in_array($nullsComparison, [self::NULLS_ALWAYS_FIRST, self::NULLS_ALWAYS_LAST], true)) {
$nullsDirection = self::NULLS_ALWAYS_FIRST === $nullsComparison ? 'ASC' : 'DESC';
} else {
$nullsDirection = self::NULLS_DIRECTION_MAP[$nullsComparison][$direction];
}

$nullRankHiddenField = sprintf('_%s_%s_null_rank', $alias, $field);

Expand Down
56 changes: 56 additions & 0 deletions tests/Bridge/Doctrine/Common/Filter/OrderFilterTestTrait.php
Expand Up @@ -232,6 +232,62 @@ private function provideApplyTestArguments(): array
],
],
],
'nulls_always_first (asc)' => [
[
'dummyDate' => [
'nulls_comparison' => 'nulls_always_first',
],
'name' => null,
],
[
'order' => [
'dummyDate' => 'asc',
'name' => 'desc',
],
],
],
'nulls_always_first (desc)' => [
[
'dummyDate' => [
'nulls_comparison' => 'nulls_always_first',
],
'name' => null,
],
[
'order' => [
'dummyDate' => 'desc',
'name' => 'desc',
],
],
],
'nulls_always_last (asc)' => [
[
'dummyDate' => [
'nulls_comparison' => 'nulls_always_last',
],
'name' => null,
],
[
'order' => [
'dummyDate' => 'asc',
'name' => 'desc',
],
],
],
'nulls_always_last (desc)' => [
[
'dummyDate' => [
'nulls_comparison' => 'nulls_always_last',
],
'name' => null,
],
[
'order' => [
'dummyDate' => 'desc',
'name' => 'desc',
],
],
],
'not having order should not throw a deprecation (select unchanged)' => [
[
'id' => null,
Expand Down
20 changes: 20 additions & 0 deletions tests/Bridge/Doctrine/Orm/Filter/OrderFilterTest.php
Expand Up @@ -265,6 +265,26 @@ public function provideApplyTestData(): array
null,
$orderFilterFactory,
],
'nulls_always_first (asc)' => [
sprintf('SELECT o, CASE WHEN o.dummyDate IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dummyDate_null_rank FROM %s o ORDER BY _o_dummyDate_null_rank ASC, o.dummyDate ASC, o.name DESC', Dummy::class),
null,
$orderFilterFactory,
],
'nulls_always_first (desc)' => [
sprintf('SELECT o, CASE WHEN o.dummyDate IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dummyDate_null_rank FROM %s o ORDER BY _o_dummyDate_null_rank ASC, o.dummyDate DESC, o.name DESC', Dummy::class),
null,
$orderFilterFactory,
],
'nulls_always_last (asc)' => [
sprintf('SELECT o, CASE WHEN o.dummyDate IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dummyDate_null_rank FROM %s o ORDER BY _o_dummyDate_null_rank DESC, o.dummyDate ASC, o.name DESC', Dummy::class),
null,
$orderFilterFactory,
],
'nulls_always_last (desc)' => [
sprintf('SELECT o, CASE WHEN o.dummyDate IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dummyDate_null_rank FROM %s o ORDER BY _o_dummyDate_null_rank DESC, o.dummyDate DESC, o.name DESC', Dummy::class),
null,
$orderFilterFactory,
],
'not having order should not throw a deprecation (select unchanged)' => [
sprintf('SELECT o FROM %s o', Dummy::class),
null,
Expand Down

0 comments on commit c44858b

Please sign in to comment.