From 42d99437923cd9b1ec7d1ec726fcd86f4e9d6821 Mon Sep 17 00:00:00 2001 From: Erki Aring Date: Mon, 14 Jul 2025 11:01:34 +0300 Subject: [PATCH] feat(doctrine): add SearchFilter STRATEGY_PATTERN --- src/Doctrine/Common/Filter/SearchFilterInterface.php | 10 ++++++++++ src/Doctrine/Odm/Filter/SearchFilter.php | 1 + src/Doctrine/Orm/Filter/SearchFilter.php | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/src/Doctrine/Common/Filter/SearchFilterInterface.php b/src/Doctrine/Common/Filter/SearchFilterInterface.php index 177289b4734..1885cf59a95 100644 --- a/src/Doctrine/Common/Filter/SearchFilterInterface.php +++ b/src/Doctrine/Common/Filter/SearchFilterInterface.php @@ -70,4 +70,14 @@ interface SearchFilterInterface * @var string Finds fields that are starting with the word case-insensitive */ public const STRATEGY_IWORD_START = 'iword_start'; + + /** + * @var string Finds fields that match the user-specified pattern + */ + public const STRATEGY_PATTERN = 'pattern'; + + /** + * @var string Finds fields that match the user-specified pattern case-insensitive + */ + public const STRATEGY_IPATTERN = 'ipattern'; } diff --git a/src/Doctrine/Odm/Filter/SearchFilter.php b/src/Doctrine/Odm/Filter/SearchFilter.php index c370e57dcf0..0d85e513a1d 100644 --- a/src/Doctrine/Odm/Filter/SearchFilter.php +++ b/src/Doctrine/Odm/Filter/SearchFilter.php @@ -276,6 +276,7 @@ private function getEqualityMatchStrategyValue(string $strategy, string $field, self::STRATEGY_START => new Regex("^$quotedValue", $caseSensitive ? '' : 'i'), self::STRATEGY_END => new Regex("$quotedValue$", $caseSensitive ? '' : 'i'), self::STRATEGY_WORD_START => new Regex("(^$quotedValue.*|.*\s$quotedValue.*)", $caseSensitive ? '' : 'i'), + self::STRATEGY_PATTERN => throw new InvalidArgumentException(\sprintf('strategy %s is not implemented.', $strategy)), default => throw new InvalidArgumentException(\sprintf('strategy %s does not exist.', $strategy)), }; } diff --git a/src/Doctrine/Orm/Filter/SearchFilter.php b/src/Doctrine/Orm/Filter/SearchFilter.php index d0caccb94d4..544d10b1c6e 100644 --- a/src/Doctrine/Orm/Filter/SearchFilter.php +++ b/src/Doctrine/Orm/Filter/SearchFilter.php @@ -334,6 +334,10 @@ protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuild $wrapCase((string) $queryBuilder->expr()->concat("'% '", $keyValueParameter, "'%'")) ) ), + self::STRATEGY_PATTERN => $queryBuilder->expr()->like( + $wrapCase($aliasedField), + $wrapCase($keyValueParameter) + ), default => throw new InvalidArgumentException(\sprintf('strategy %s does not exist.', $strategy)), }; }