Skip to content

Commit

Permalink
Add support for \DateTimeImmutable at AbstractDateFilter::filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and franmomu committed Nov 8, 2020
1 parent 606cf31 commit b51d6ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Filter/AbstractDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value
}

// date filter should filter records for the whole days
if (false === $this->time && $value['value']['end'] instanceof \DateTime) {
if (false === $this->time && ($value['value']['end'] instanceof \DateTime || $value['value']['end'] instanceof \DateTimeImmutable)) {
// since the received `\DateTime` object uses the model timezone to represent
// the value submitted by the view (which can use a different timezone) and this
// value is intended to contain a time in the begining of a date (IE, if the model
// object is configured to use UTC timezone, the view object "2020-11-07 00:00:00.0-03:00"
// is transformed to "2020-11-07 03:00:00.0+00:00" in the model object), we increment
// the time part by adding "23:59:59" in order to cover the whole end date and get proper
// results from queries like "o.created_at <= :date_end".
$value['value']['end']->modify('+23 hours 59 minutes 59 seconds');
$value['value']['end'] = $value['value']['end']->modify('+23 hours 59 minutes 59 seconds');
}

// transform types
if ('timestamp' === $this->getOption('input_type')) {
$value['value']['start'] = $value['value']['start'] instanceof \DateTime ? $value['value']['start']->getTimestamp() : 0;
$value['value']['end'] = $value['value']['end'] instanceof \DateTime ? $value['value']['end']->getTimestamp() : 0;
$value['value']['start'] = $value['value']['start'] instanceof \DateTimeInterface ? $value['value']['start']->getTimestamp() : 0;
$value['value']['end'] = $value['value']['end'] instanceof \DateTimeInterface ? $value['value']['end']->getTimestamp() : 0;
}

// default type for range filter
Expand Down Expand Up @@ -120,7 +120,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value

// transform types
if ('timestamp' === $this->getOption('input_type')) {
$value['value'] = $value['value'] instanceof \DateTime ? $value['value']->getTimestamp() : 0;
$value['value'] = $value['value'] instanceof \DateTimeInterface ? $value['value']->getTimestamp() : 0;
}

// null / not null only check for col
Expand Down
23 changes: 23 additions & 0 deletions tests/Filter/DateRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,27 @@ public function provideDates(): iterable
new \DateTimeZone('Africa/Cairo'),
];
}

public function testFilterEndDateImmutable(): void
{
$filter = new DateRangeFilter();
$filter->initialize('field_name', ['field_options' => ['class' => 'FooBar']]);

$builder = new ProxyQuery(new QueryBuilder());

$endDateTime = new \DateTimeImmutable('2016-08-31');

$filter->filter($builder, 'alias', 'field', [
'type' => null,
'value' => [
'start' => '',
'end' => $endDateTime,
],
]);

$this->assertSame(['alias.field <= :field_name_1'], $builder->query);
$this->assertCount(1, $builder->parameters);
$this->assertSame($endDateTime->modify('+23 hours 59 minutes 59 seconds')->getTimestamp(), $builder->parameters['field_name_1']->getTimestamp());
$this->assertTrue($filter->isActive());
}
}

0 comments on commit b51d6ca

Please sign in to comment.