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

fix: Обработка null значения в фильтре Where

parent 8409c1c0
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace IQDEV\Packages\DoctrineHttpFilter\Exception;

use Exception;

class FilterParameterValueIsNullException extends Exception
{
    public function __construct(string $key)
    {
        parent::__construct('Filter parameter value of key [' . $key . '] is null.');
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -5,16 +5,22 @@ 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 Where extends HttpFilter
{
    /** @throws FilterParameterValueIsNullException */
    public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
    {
        $queryBuilder->where(
            $this->getColumn() . ' = :' . $this->getParameterKey(),
        );

        if ($this->getHttpValue() === null) {
            throw new FilterParameterValueIsNullException($this->field);
        }

        $queryBuilder->setParameter($this->getParameterKey(), $this->getHttpValue());

        return $queryBuilder;
+3 −3
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ abstract class HttpFilter

    public function __construct(
        protected string $tableAlias,
        protected string $filed,
        protected string $field,
        ?Request $request = null,
    ) {
        $this->request = $request ?? Request::createFromGlobals();
@@ -23,7 +23,7 @@ abstract class HttpFilter

    protected function getColumn(): string
    {
        return $this->tableAlias . '.' . $this->filed;
        return $this->tableAlias . '.' . $this->field;
    }

    protected function getHttpValue(): mixed
@@ -37,7 +37,7 @@ abstract class HttpFilter
            return null;
        }

        return $filter[$this->filed] ?? null;
        return $filter[$this->field] ?? null;
    }

    public function getParameterKey(): string