Skip to content

Commit 8de55db

Browse files
committed
QueryNormalizer: Code optimalization and prepare for testing
1 parent fa6f6d6 commit 8de55db

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/NumberRewriter.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Model\Math;
46

57

@@ -113,24 +115,23 @@ class NumberRewriter
113115
// --------------------------------------------------- To number ---------------------------------------------------
114116

115117
/**
116-
* @param string $word
118+
* @param string $haystack
117119
* @return string
118120
*/
119-
public function toNumber(string $word): string
121+
public function toNumber(string $haystack): string
120122
{
121-
$word = trim(preg_replace('/\s+/', ' ', $word));
123+
$haystack = trim(preg_replace('/\s+/', ' ', $haystack));
122124

123125
foreach ($this->regex as $key => $value) {
124-
$word = preg_replace('/' . $key . '/', $value, $word);
126+
$haystack = (string) preg_replace('/' . $key . '/', $value, $haystack);
125127
}
126128

127-
foreach ($this->basic as $w => $n) {
128-
if ($w === $word) {
129-
return $n;
130-
}
129+
$return = '';
130+
foreach (explode(' ', $haystack) as $word) {
131+
$return .= $this->processWord($word) . ' ';
131132
}
132133

133-
return $word;
134+
return trim($return);
134135
}
135136

136137
// ---------------------------------------------------- To word ----------------------------------------------------
@@ -152,8 +153,8 @@ public function toWord(string $number): string
152153
}
153154

154155
foreach ($this->basic as $w => $n) {
155-
if ((string) $n === $number) {
156-
return $w;
156+
if ($n === $number) {
157+
return (string) $w;
157158
}
158159
}
159160

@@ -269,4 +270,19 @@ private function smartInflect(int $number, string $for1, string $for234, string
269270
return $forOther;
270271
}
271272

273+
/**
274+
* @param string $word
275+
* @return string
276+
*/
277+
private function processWord(string $word): string
278+
{
279+
foreach ($this->basic as $basicWord => $basicRewrite) {
280+
if ($basicWord === $word) {
281+
return (string) $basicRewrite;
282+
}
283+
}
284+
285+
return $word;
286+
}
287+
272288
}

src/QueryNormalizer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class QueryNormalizer
1717
private static $regexMap = [
1818
'\s*(\#|\/\/).*$' => '',
1919
'(\d)([,;])\s+(\d)' => '$1$2$3',
20-
// '(\d|[a-z])\s+(\d|[a-z])' => '$1$2',
2120
'(\d)\s+(\d)' => '$1$2',
2221
'(\d)(\*{2,}|\˘)(\d)' => '$1^$3',
2322
'(\d)\s*(\:|÷)\s*(\d)' => '$1/$3',
2423
'(^|[^\d\,])(\d+)\,(\d+)($|[^\d\,])' => '$1$2.$3$4',
24+
'(\d)\s+([a-z])' => '$1$2',
2525
'\+\-' => '-',
2626
'(\d+)\s*plus\s*(\d+)' => '$1+$2',
2727
'(\d+)\s*(krát|krat)\s*(\d+)' => '$1*$3',
@@ -56,6 +56,8 @@ public function __construct(NumberRewriter $numberRewriter)
5656
}
5757

5858
/**
59+
* Magic convertor of user input to normalized machine-readable form.
60+
*
5961
* @param string $query
6062
* @return string
6163
*/
@@ -95,7 +97,7 @@ private function taskFixBrackets(string $query): string
9597
$return = str_repeat('(', $rightCount - $leftCount) . $query;
9698
}
9799

98-
$redundantBrackets = function (string $return): string {
100+
$redundantBrackets = static function (string $return): string {
99101
$returnInner = $return;
100102
while (true) {
101103
if (preg_match('/^\((?<content>.+)\)$/', $returnInner, $bracketParser)) {

0 commit comments

Comments
 (0)