Skip to content
Snippets Groups Projects
Range.php 1016 B
Newer Older
<?php

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 Range extends HttpFilter
{
    /** @throws FilterParameterValueIsNullException */
    public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
    {
        if (! isset($this->getHttpValue()['min']) && ! isset($this->getHttpValue()['max'])) {
            throw new FilterParameterValueIsNullException($this->field);
        }

        if (isset($this->getHttpValue()['min'])) {
            $queryBuilder->where(
                $this->getColumn() . '>= ' . $this->getHttpValue()['min']
            );
        }

        if (isset($this->getHttpValue()['max'])) {
            $queryBuilder->andWhere(
                $this->getColumn() . ' <= ' . $this->getHttpValue()['max']
            );
        }