From a262b4751e7506a523ff3a0ef5ce0fafb0385811 Mon Sep 17 00:00:00 2001 From: sgtwinters <14836040+sgtwinters@users.noreply.github.com> Date: Wed, 10 Jan 2018 21:05:25 +0100 Subject: [PATCH] added a more fuzzy search filter --- .../Operators/CollectionOperator.php | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/JohannesSchobel/DingoQueryMapper/Operators/CollectionOperator.php b/src/JohannesSchobel/DingoQueryMapper/Operators/CollectionOperator.php index 1edc720..0306fe4 100644 --- a/src/JohannesSchobel/DingoQueryMapper/Operators/CollectionOperator.php +++ b/src/JohannesSchobel/DingoQueryMapper/Operators/CollectionOperator.php @@ -127,16 +127,32 @@ private function createEvaluationRule($key, $operator, $value) // now check if the operator was "(not) like"? if (strpos($operator, 'like') !== false) { - $value = str_replace('%', '', $value); - $rule = "substr('%s', 0, strlen('%s')) %s '%s'"; // haystack, $needle, $comparable, $needle - $expectedResult = '==='; + // check for %value% to perform a more fuzzy match + if (preg_match('/%(.*).{1,}?%/', $value)) { + $value = str_replace('%', '', $value); + $rule = "%sis_int(stripos('%s', '%s')) ? true : false"; - if (stripos($operator, 'not') !== false) { - // it is a NOT LIKE operator - $expectedResult = '!=='; - } + $expectedResult = ''; + + if (stripos($operator, 'not') !== false) { + // it is a NOT LIKE operator + $expectedResult = '!'; + } + + $rule = sprintf($rule, $expectedResult, $key, $value); + } else { + $value = str_replace('%', '', $value); + $rule = "substr('%s', 0, strlen('%s')) %s '%s'"; // haystack, $needle, $comparable, $needle + + $expectedResult = '==='; + + if (stripos($operator, 'not') !== false) { + // it is a NOT LIKE operator + $expectedResult = '!=='; + } - $rule = sprintf($rule, $key, $value, $expectedResult, $value); + $rule = sprintf($rule, $key, $value, $expectedResult, $value); + } } return $rule;