diff --git a/src/Filter/Date.php b/src/Filter/Date.php index fe7e3caf77c3d5346a94eafc68de0d05b8164298..53d33de29c4a902a3899d16912c164ff8ffae1aa 100644 --- a/src/Filter/Date.php +++ b/src/Filter/Date.php @@ -14,10 +14,14 @@ final class Date extends HttpFilter public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder { $queryBuilder->where( - 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') = :' . $this->getParameterKey(), + 'SUBSTRING(' . $this->getColumn() . ', 0, 11) = :' . $this->getParameterKey(), ); - if ($this->getHttpValue() === null) { + if (is_null($this->getHttpValue())) { + throw new FilterParameterValueIsNullException($this->field); + } + + if (! \DateTimeImmutable::createFromFormat('Y-m-d', $this->getHttpValue())) { throw new FilterParameterValueIsNullException($this->field); } diff --git a/src/Filter/DateRange.php b/src/Filter/DateRange.php index a9360a0e487cfd293ee62f7427121bf9fb53d5ef..ed38d2bc8d2350f1b8b1e784b57cdc9eb4086816 100644 --- a/src/Filter/DateRange.php +++ b/src/Filter/DateRange.php @@ -13,20 +13,29 @@ final class DateRange extends HttpFilter /** @throws FilterParameterValueIsNullException */ public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder { - if (! isset($this->getHttpValue()['from']) && ! isset($this->getHttpValue()['to'])) { + $httpValues = $this->getHttpValue(); + $fromDate = $httpValues['from'] ?? null; + $toDate = $httpValues['to'] ?? null; + + if (is_null($fromDate) && is_null($toDate)) { + throw new FilterParameterValueIsNullException($this->field); + } + + $fromDate = \DateTimeImmutable::createFromFormat('Y-m-d', $fromDate ?? ''); + $toDate = \DateTimeImmutable::createFromFormat('Y-m-d', $toDate ?? ''); + + if (! $fromDate && ! $toDate){ throw new FilterParameterValueIsNullException($this->field); } - if (isset($this->getHttpValue()['from'])) { - $queryBuilder->where( - 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\'', - ); + if ($fromDate) { + $queryBuilder->where($this->getColumn() . ' >= :fromDate') + ->setParameter('fromDate', $fromDate->setTime(0, 0, 0)); } - if (isset($this->getHttpValue()['to'])) { - $queryBuilder->andWhere( - 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'', - ); + if ($toDate) { + $queryBuilder->andWhere($this->getColumn() . ' <= :toDate') + ->setParameter('toDate', $toDate->setTime(23, 59, 59)); } return $queryBuilder;