From b7b1400ba21250f650ce8669856bd227a4c7d6d4 Mon Sep 17 00:00:00 2001 From: "a.shamavov" Date: Mon, 3 Mar 2025 16:03:58 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20Dat?= =?UTF-8?q?eRange?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Filter/DateRange.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Filter/DateRange.php b/src/Filter/DateRange.php index 9acd75f..5c3ab2f 100644 --- a/src/Filter/DateRange.php +++ b/src/Filter/DateRange.php @@ -5,16 +5,29 @@ declare(strict_types=1); namespace IQDEV\Packages\DoctrineHttpFilter\Filter; use Doctrine\ORM\QueryBuilder; +use IQDEV\Packages\DoctrineHttpFilter\Exception\FilterParameterValueIsNullException; use IQDEV\Packages\DoctrineHttpFilter\HttpFilter; final class DateRange extends HttpFilter { + /** @throws FilterParameterValueIsNullException */ public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder { - $queryBuilder->where( - 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\' AND ' . - 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'', - ); + if (! isset($this->getHttpValue()['from']) && ! isset($this->getHttpValue()['to'])) { + throw new FilterParameterValueIsNullException($this->field); + } + + if (isset($this->getHttpValue()['from'])) { + $queryBuilder->where( + 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\'', + ); + } + + if (isset($this->getHttpValue()['to'])) { + $queryBuilder->andWhere( + 'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'', + ); + } return $queryBuilder; } -- GitLab