Commit 2c062f3a authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

fix: Исправление фильтров с датами

parent 41daf21e
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -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);
        }

+18 −9
Original line number Diff line number Diff line
@@ -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;