38 lines
829 B
PHP
38 lines
829 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace MintyPHP\Support\Search;
|
||
|
|
|
||
|
|
class SearchQueryNormalizer
|
||
|
|
{
|
||
|
|
public static function normalizeLikeQuery(string $query): string
|
||
|
|
{
|
||
|
|
$query = trim($query);
|
||
|
|
if ($query === '') {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
$query = str_replace('\\', '\\\\', $query);
|
||
|
|
$query = str_replace('_', '\\_', $query);
|
||
|
|
$query = str_replace('*', '%', $query);
|
||
|
|
|
||
|
|
if (!str_starts_with($query, '%')) {
|
||
|
|
$query = '%' . $query;
|
||
|
|
}
|
||
|
|
if (!str_ends_with($query, '%')) {
|
||
|
|
$query .= '%';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $query;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function normalizeScoreQuery(string $query): string
|
||
|
|
{
|
||
|
|
$query = trim($query);
|
||
|
|
if ($query === '') {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
|
||
|
|
return str_replace(['*', '%', '_'], '', $query);
|
||
|
|
}
|
||
|
|
}
|