From 2c062f3ad30f890991c6126a7d0880542732eefc Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Tue, 4 Mar 2025 16:04:29 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20=D1=81=20=D0=B4=D0=B0=D1=82=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Filter/Date.php | 8 ++++++-- src/Filter/DateRange.php | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Filter/Date.php b/src/Filter/Date.php index fe7e3ca..53d33de 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 a9360a0..ed38d2b 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; -- GitLab