diff --git a/src/Filter/DateRange.php b/src/Filter/DateRange.php index 9acd75fd9f46190e42fa719929d073f9a7ed4bd3..5c3ab2facfffaf62b7f96fa7297d3b35a1da1200 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; }