Skip to content

Commit 26c893a

Browse files
authored
QueryNormalizer: Fix new user cases.
1 parent f539849 commit 26c893a

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/QueryNormalizer.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,19 @@ public function __construct(NumberRewriter $numberRewriter)
6767
*/
6868
public function normalize(string $query): string
6969
{
70-
$query = Strings::trim(Strings::normalize($query));
70+
$query = Strings::trim(Strings::normalize($query), " \t\n\r\"'");
71+
$query = $this->removeEmoji($query);
7172
$query = (string) preg_replace('/\s+/', ' ', $query);
7273
$query = (string) preg_replace('/=\??$/', '', $query);
7374

7475
$queryNew = '';
7576
foreach (explode('=', $query) as $queryParser) {
76-
$queryPart = $queryParser;
77+
$queryPart = trim($queryParser);
7778
$queryPart = $this->taskFixBrackets($queryPart);
7879
$queryPart = $this->taskRewriteWordNumber($queryPart);
7980
$queryPart = $this->taskNormalizeNumber($queryPart);
8081
$queryPart = $this->taskRegexReplaceMap($queryPart);
81-
$queryNew .= ($queryNew ? '=' : '') . $queryPart;
82+
$queryNew .= ($queryNew !== '' ? '=' : '') . $queryPart;
8283
}
8384

8485
return $queryNew;
@@ -90,10 +91,7 @@ public function normalize(string $query): string
9091
*/
9192
private function taskFixBrackets(string $query): string
9293
{
93-
$leftCount = substr_count($query, '(');
94-
$rightCount = substr_count($query, ')');
95-
96-
if ($leftCount === $rightCount) {
94+
if (($leftCount = substr_count($query, '(')) === ($rightCount = substr_count($query, ')'))) {
9795
$return = $query;
9896
} elseif ($leftCount > $rightCount) {
9997
$return = $query . str_repeat(')', $leftCount - $rightCount);
@@ -130,7 +128,7 @@ private function taskRegexReplaceMap(string $query): string
130128
$oldQuery = $query;
131129

132130
foreach (self::$regexMap as $regex => $replace) {
133-
$query = preg_replace('/' . $regex . '/', $replace, $query);
131+
$query = (string) preg_replace('/' . $regex . '/', $replace, $query);
134132
}
135133

136134
if ($oldQuery === $query) {
@@ -147,7 +145,7 @@ private function taskRegexReplaceMap(string $query): string
147145
*/
148146
private function taskNormalizeNumber(string $query): string
149147
{
150-
return preg_replace_callback('/([\d\,]+\,\d{3})\.(\d+)/', function (array $match) {
148+
return preg_replace_callback('/([\d\,]+\,\d{3})\.(\d+)/', static function (array $match): string {
151149
return preg_replace('/\D/', '', $match[1]) . '.' . $match[2];
152150
}, $query);
153151
}
@@ -161,4 +159,28 @@ private function taskRewriteWordNumber(string $query): string
161159
return $this->numberRewriter->toNumber($query);
162160
}
163161

162+
/**
163+
* @param string $query
164+
* @return string
165+
*/
166+
private function removeEmoji(string $query): string
167+
{
168+
// Match Emoticons
169+
$query = (string) preg_replace('/[\x{1F600}-\x{1F64F}]/u', '', $query);
170+
171+
// Match Miscellaneous Symbols and Pictographs
172+
$query = (string) preg_replace('/[\x{1F300}-\x{1F5FF}]/u', '', $query);
173+
174+
// Match Transport And Map Symbols
175+
$query = (string) preg_replace('/[\x{1F680}-\x{1F6FF}]/u', '', $query);
176+
177+
// Match Miscellaneous Symbols
178+
$query = (string) preg_replace('/[\x{2600}-\x{26FF}]/u', '', $query);
179+
180+
// Match Dingbats
181+
$query = (string) preg_replace('/[\x{2700}-\x{27BF}]/u', '', $query);
182+
183+
return $query;
184+
}
185+
164186
}

0 commit comments

Comments
 (0)