11<?php
22
3+ declare (strict_types=1 );
4+
35namespace 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}
0 commit comments