Commit 228130a7 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Corrections et améliorations dans les SearchFilter

parent 2b2c6d94
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -9,7 +9,12 @@ class StrReducedTextSearchFilter extends TextSearchFilter
    protected function applyToQueryBuilderUsingWhereField(QueryBuilder $qb)
    {
        $qb
            ->andWhere(sprintf("strReduce(%s) %s strReduce(:%s)", $this->whereField, $this->getOperator(), $paramName = uniqid('p')))
            ->andWhere(sprintf(
                "strReduce(%s) %s strReduce(:%s)",
                $this->whereField,
                $this->getOperator(),
                $paramName = uniqid('p')
            ))
            ->setParameter($paramName, $this->getComparisonValue());
    }

@@ -17,7 +22,13 @@ class StrReducedTextSearchFilter extends TextSearchFilter
    {
        $alias = current($qb->getRootAliases());
        $qb
            ->andWhere(sprintf("strReduce(%s.%s) %s strReduce(:%s)", $alias, $this->getName(), $this->getOperator(), $paramName = uniqid('p')))
            ->andWhere(sprintf(
                "strReduce(%s.%s) %s strReduce(:%s)",
                $alias,
                $this->getName(),
                $this->getOperator(),
                $paramName = uniqid('p')
            ))
            ->setParameter($paramName, $this->getComparisonValue());
    }
}
+14 −3
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ use Doctrine\ORM\QueryBuilder;
class TextSearchFilter extends SearchFilter
{
    protected bool $useLikeOperator = false;
    protected string $likeOperator = 'LIKE';

    /**
     * @param bool $useLikeOperator
@@ -23,9 +24,19 @@ class TextSearchFilter extends SearchFilter
        return $this;
    }

    /**
     * @param string $likeOperator
     * @return self
     */
    public function setLikeOperator(string $likeOperator): self
    {
        $this->likeOperator = $likeOperator;
        return $this;
    }

    protected function canApplyToQueryBuilder(): bool
    {
        $filterValue = $this->getValue();
        $filterValue = trim($this->getValue());

        return $filterValue !== null && strlen($filterValue) > 1;
    }
@@ -47,12 +58,12 @@ class TextSearchFilter extends SearchFilter

    protected function getOperator(): string
    {
        return $this->useLikeOperator ? 'LIKE' : '=';
        return $this->useLikeOperator ? $this->likeOperator : '=';
    }

    public function getComparisonValue(): string
    {
        $filterValue = $this->getValue();
        $filterValue = trim($this->getValue());

        return $this->useLikeOperator ? "%$filterValue%" : $filterValue;
    }
+1 −1
Original line number Diff line number Diff line
@@ -335,7 +335,7 @@ abstract class SearchService implements SearchServiceInterface
        $qb = $this->createQueryBuilder();

        foreach ($this->filters as $filter) {
            if ($filter->getValue()) {
            if ($filter->getValue() !== null) {
                $filter->applyToQueryBuilder($qb);
            }
        }
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace Doctorant\Search;
use Application\Search\Filter\TextSearchFilter;
use Doctrine\ORM\QueryBuilder;

class DoctorantSearchFilter extends TextSearchFilter
class DoctorantSearchFilter extends TextSearchFilter // todo : hériter de StrReducedTextSearchFilter
{
    const NAME = 'doctorant';