Skip to content
Snippets Groups Projects
Commit 6eb19f1e authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

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

parent 8409c1c0
No related branches found
No related tags found
1 merge request!1Фильтры Like, ILike, Range, In, Date, DateRange
<?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.');
}
}
......@@ -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;
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment