Skip to content

Commit

Permalink
Use * as wildcard, thus align with kernel PR
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Feb 1, 2019
1 parent bd2d90b commit e648a6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
14 changes: 5 additions & 9 deletions lib/Query/Common/CriterionVisitor/Field/FieldLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)

$queries = [];
foreach ($searchFields as $name => $fieldType) {
$preparedValue = $this->escapeWildcard(
$this->toString(
$this->mapSearchFieldValue($criterion->value, $fieldType)
)
);
$preparedValue = $this->toString($this->mapSearchFieldValue($criterion->value, $fieldType));

// Todo: Verify (add integration tests) that % has actually worked with SQL engine before we add here & api doc
if (strpos($preparedValue, '%') !== false) {
$queries[] = $name . ':' . str_replace('%', '*', $preparedValue);
// Check if there is user supplied wildcard or not
if (strpos($preparedValue, '*') !== false) {
$queries[] = $name . ':' . $this->escapeExpressions($preparedValue, true);
} else {
$queries[] = $name . ':*' . $preparedValue . '*';
$queries[] = $name . ':"' . $this->escapeQuote($preparedValue) . '"';
}
}

Expand Down
17 changes: 12 additions & 5 deletions lib/Query/CriterionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,27 @@ protected function escapeQuote($string, $doubleQuote = false)
}

/**
* Escapes value for use in wildcard search.
* Escapes value for use in expressions.
*
* @param string $string
* @param bool $allowWildcard Allow "*" in expression.
*
* @param $value
* @return mixed
*/
protected function escapeWildcard($value)
protected function escapeExpressions($string, $allowWildcard = false)
{
$reservedCharacters = preg_quote('+-&|!(){}[]^"~*?:\\ ');
if ($allowWildcard) {
$reservedCharacters = preg_quote('+-&|!(){}[]^"~?:\\ ');
} else {
$reservedCharacters = preg_quote('+-&|!(){}[]^"~*?:\\ ');
}

return preg_replace_callback(
'/([' . $reservedCharacters . '])/',
function ($matches) {
return '\\' . $matches[0];
},
$value);
$string
);
}
}

0 comments on commit e648a6f

Please sign in to comment.