Skip to content

Commit

Permalink
Merge pull request #1928 from Simperfit/feature/add-always-include-null
Browse files Browse the repository at this point in the history
feature: add a way to always include null in the date filter
  • Loading branch information
soyuka committed May 4, 2018
2 parents e3d50ff + 28bbbae commit dd2d49e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Bridge/Doctrine/Orm/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DateFilter extends AbstractContextAwareFilter
const EXCLUDE_NULL = 'exclude_null';
const INCLUDE_NULL_BEFORE = 'include_null_before';
const INCLUDE_NULL_AFTER = 'include_null_after';
const INCLUDE_NULL_BEFORE_AND_AFTER = 'include_null_before_and_after';
const DOCTRINE_DATE_TYPES = [
'date' => true,
'datetime' => true,
Expand Down Expand Up @@ -189,8 +190,9 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
if (null === $nullManagement || self::EXCLUDE_NULL === $nullManagement) {
$queryBuilder->andWhere($baseWhere);
} elseif (
(\in_array($operator, [self::PARAMETER_BEFORE, self::PARAMETER_STRICTLY_BEFORE], true) && self::INCLUDE_NULL_BEFORE === $nullManagement) ||
(\in_array($operator, [self::PARAMETER_AFTER, self::PARAMETER_STRICTLY_AFTER], true) && self::INCLUDE_NULL_AFTER === $nullManagement)
(self::INCLUDE_NULL_BEFORE === $nullManagement && \in_array($operator, [self::PARAMETER_BEFORE, self::PARAMETER_STRICTLY_BEFORE], true)) ||
(self::INCLUDE_NULL_AFTER === $nullManagement && \in_array($operator, [self::PARAMETER_AFTER, self::PARAMETER_STRICTLY_AFTER], true)) ||
(self::INCLUDE_NULL_BEFORE_AND_AFTER === $nullManagement && \in_array($operator, [self::PARAMETER_AFTER, self::PARAMETER_STRICTLY_AFTER, self::PARAMETER_BEFORE, self::PARAMETER_STRICTLY_BEFORE], true))
) {
$queryBuilder->andWhere($queryBuilder->expr()->orX(
$baseWhere,
Expand Down
11 changes: 11 additions & 0 deletions tests/Bridge/Doctrine/Orm/Filter/DateFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ public function provideApplyTestData(): array
],
sprintf('SELECT o FROM %s o WHERE o.dummyDate >= :dummyDate_p1 OR o.dummyDate IS NULL', Dummy::class),
],
'include null before and after (include_null_before_and_after)' => [
[
'dummyDate' => 'include_null_before_and_after',
],
[
'dummyDate' => [
'after' => '2015-04-05',
],
],
sprintf('SELECT o FROM %s o WHERE o.dummyDate >= :dummyDate_p1 OR o.dummyDate IS NULL', Dummy::class),
],
'bad date format' => [
[
'dummyDate' => null,
Expand Down

0 comments on commit dd2d49e

Please sign in to comment.