1 | <?php |
||||
2 | |||||
3 | namespace Common\Tool; |
||||
4 | |||||
5 | class StringTool |
||||
6 | { |
||||
7 | public static function startsWith($haystack, $needle, $case = true) |
||||
8 | { |
||||
9 | mb_internal_encoding('UTF-8'); |
||||
10 | |||||
11 | if ($case) { |
||||
12 | return mb_strpos($haystack, $needle, 0) === 0; |
||||
13 | } |
||||
14 | return mb_stripos($haystack, $needle, 0) === 0; |
||||
15 | } |
||||
16 | |||||
17 | public static function endsWith($haystack,$needle, $case = true) |
||||
18 | { |
||||
19 | mb_internal_encoding('UTF-8'); |
||||
20 | |||||
21 | $expectedPosition = mb_strlen($haystack) - mb_strlen($needle); |
||||
22 | if ($case) { |
||||
23 | return mb_strrpos($haystack, $needle, 0) === $expectedPosition; |
||||
24 | } |
||||
25 | return mb_strripos($haystack, $needle, 0) === $expectedPosition; |
||||
26 | } |
||||
27 | |||||
28 | public static function contains($haystack, $needle) |
||||
29 | { |
||||
30 | mb_internal_encoding('UTF-8'); |
||||
31 | |||||
32 | return mb_strpos($haystack, $needle) !== false; |
||||
33 | } |
||||
34 | |||||
35 | public static function capitalizeFirstLetter($string) |
||||
36 | { |
||||
37 | mb_internal_encoding('UTF-8'); |
||||
38 | |||||
39 | return mb_convert_case($string, MB_CASE_TITLE); |
||||
40 | } |
||||
41 | |||||
42 | public static function removePrefix($string, $prefix) |
||||
43 | { |
||||
44 | return mb_substr($string, mb_strlen($prefix)); |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Remove any non-ASCII characters and convert known non-ASCII characters |
||||
49 | * to their ASCII equivalents, if possible. |
||||
50 | * @param string $string |
||||
51 | * @return string |
||||
52 | * @link http://gist.github.com/119517 |
||||
53 | */ |
||||
54 | public static function convertAscii($string) |
||||
55 | { |
||||
56 | // Replace Single Curly Quotes |
||||
57 | $search[] = chr(226).chr(128).chr(152); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||||
58 | $replace[] = "'"; |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
59 | $search[] = chr(226).chr(128).chr(153); |
||||
60 | $replace[] = "'"; |
||||
61 | |||||
62 | // Replace Smart Double Curly Quotes |
||||
63 | $search[] = chr(226).chr(128).chr(156); |
||||
64 | $replace[] = '"'; |
||||
65 | $search[] = chr(226).chr(128).chr(157); |
||||
66 | $replace[] = '"'; |
||||
67 | |||||
68 | // Replace En Dash |
||||
69 | $search[] = chr(226).chr(128).chr(147); |
||||
70 | $replace[] = '-'; |
||||
71 | |||||
72 | // Replace Em Dash |
||||
73 | $search[] = chr(226).chr(128).chr(148); |
||||
74 | $replace[] = '-'; |
||||
75 | |||||
76 | // Replace Bullet |
||||
77 | $search[] = chr(226).chr(128).chr(162); |
||||
78 | $replace[] = '*'; |
||||
79 | |||||
80 | // Replace Middle Dot |
||||
81 | $search[] = chr(194).chr(183); |
||||
82 | $replace[] = '*'; |
||||
83 | |||||
84 | // Replace Ellipsis with three consecutive dots |
||||
85 | $search[] = chr(226).chr(128).chr(166); |
||||
86 | $replace[] = '...'; |
||||
87 | |||||
88 | // Apply Replacements |
||||
89 | $string = str_replace($search, $replace, $string); |
||||
90 | |||||
91 | // Remove any non-ASCII Characters (keeps cyrillic) |
||||
92 | $string = preg_replace("/[^\x80-\xFF, \x01-\x7F]/","", $string); |
||||
93 | |||||
94 | return $string; |
||||
95 | } |
||||
96 | |||||
97 | |||||
98 | public static function unaccent($string) |
||||
0 ignored issues
–
show
The parameter
$string is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
99 | { |
||||
100 | |||||
101 | } |
||||
102 | |||||
103 | public static function cleanSmsContent($message, $transliterateCyrillic = true) |
||||
104 | { |
||||
105 | $message = trim($message); |
||||
106 | $message = Transliterator::unaccent($message, $transliterateCyrillic); |
||||
107 | |||||
108 | return self::convertAscii($message, $transliterateCyrillic); |
||||
0 ignored issues
–
show
The call to
Common\Tool\StringTool::convertAscii() has too many arguments starting with $transliterateCyrillic .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||
109 | } |
||||
110 | |||||
111 | public static function getArrayWithRemovedPrefixRecursive(array $input, $prefix) |
||||
112 | { |
||||
113 | mb_internal_encoding('UTF-8'); |
||||
114 | |||||
115 | $return = array(); |
||||
116 | foreach ($input as $key => $value) { |
||||
117 | if (strpos($key, $prefix) === 0) { |
||||
118 | $key = substr($key, mb_strlen($prefix)); |
||||
119 | } |
||||
120 | |||||
121 | if (is_array($value)) { |
||||
122 | $value = self::getArrayWithRemovedPrefixRecursive($value, $prefix); |
||||
123 | } |
||||
124 | |||||
125 | $return[$key] = $value; |
||||
126 | } |
||||
127 | return $return; |
||||
128 | } |
||||
129 | |||||
130 | // possible TODO fix type Combionation to Combination |
||||
131 | public static function createCombionationsFromStringWords($phrase = ''): array |
||||
132 | { |
||||
133 | $phrase = preg_replace('/\s+/', ' ', strtolower($phrase)); |
||||
134 | $words = explode(' ', $phrase); |
||||
135 | |||||
136 | return self::createCombionationsFromWords($words); |
||||
137 | } |
||||
138 | |||||
139 | public static function createCombionationsFromWords($words = []): array |
||||
140 | { |
||||
141 | array_walk($words, 'trim'); |
||||
142 | |||||
143 | foreach ($words as $x => $value) { |
||||
144 | if (trim($value) === '') { |
||||
145 | unset($words[$x]); |
||||
146 | } |
||||
147 | } |
||||
148 | |||||
149 | return self::permuteCombinations($words); |
||||
150 | } |
||||
151 | |||||
152 | private static function permuteCombinations($items, $perms = []): array |
||||
153 | { |
||||
154 | $back = []; |
||||
155 | |||||
156 | if (empty($items)) { |
||||
157 | $back[] = implode(' ', $perms); |
||||
158 | } else { |
||||
159 | for ($i = count($items) - 1; $i >= 0; --$i) { |
||||
160 | $newitems = $items; |
||||
161 | $newperms = $perms; |
||||
162 | list($foo) = array_splice($newitems, $i, 1); |
||||
163 | array_unshift($newperms, $foo); |
||||
164 | $back = array_merge($back, self::permuteCombinations($newitems, $newperms)); |
||||
165 | } |
||||
166 | } |
||||
167 | |||||
168 | return $back; |
||||
169 | } |
||||
170 | |||||
171 | public static function toLower($string) |
||||
172 | { |
||||
173 | mb_internal_encoding('UTF-8'); |
||||
174 | |||||
175 | return mb_strtolower($string); |
||||
176 | } |
||||
177 | |||||
178 | public static function toUpper($string) |
||||
179 | { |
||||
180 | mb_internal_encoding('UTF-8'); |
||||
181 | |||||
182 | return mb_strtoupper($string); |
||||
183 | } |
||||
184 | |||||
185 | public static function toUTF8($string) |
||||
186 | { |
||||
187 | mb_internal_encoding('UTF-8'); |
||||
188 | return iconv('UTF-8', 'UTF-8//TRANSLIT', $string); |
||||
189 | } |
||||
190 | |||||
191 | /** |
||||
192 | * @param int $length |
||||
193 | * @return string |
||||
194 | */ |
||||
195 | public static function random($length = 8) |
||||
196 | { |
||||
197 | $numbers = '0123456789'; |
||||
198 | $numbersLength = strlen($numbers); |
||||
199 | |||||
200 | $characters = 'abcdefghijklmnopqrstuvwxyz'; |
||||
201 | $charactersLength = strlen($characters); |
||||
202 | |||||
203 | $string = ''; |
||||
204 | for ($i = 0; $i < $length; $i++) { |
||||
205 | $symbol = $numbers[rand(0, $numbersLength - 1)]; |
||||
206 | if (rand(0, 1)) { |
||||
207 | $symbol = $characters[rand(0, $charactersLength - 1)]; |
||||
208 | if (rand(0, 1)) { |
||||
209 | $symbol = self::toUpper($symbol); |
||||
210 | } |
||||
211 | } |
||||
212 | |||||
213 | $string .= $symbol; |
||||
214 | } |
||||
215 | |||||
216 | return $string; |
||||
217 | } |
||||
218 | |||||
219 | /** |
||||
220 | * @param $length |
||||
221 | * @return string |
||||
222 | */ |
||||
223 | public static function password($length = 8) |
||||
224 | { |
||||
225 | if ($length < 8) { |
||||
226 | throw new \InvalidArgumentException('Length must be more than 7'); |
||||
227 | } |
||||
228 | |||||
229 | $string = self::random($length); |
||||
230 | |||||
231 | if (!preg_match('/[a-z]/', $string)) { |
||||
232 | return self::random($length); |
||||
233 | } |
||||
234 | |||||
235 | if (!preg_match('/[A-Z]/', $string)) { |
||||
236 | return self::random($length); |
||||
237 | } |
||||
238 | |||||
239 | if (!preg_match('/\d/', $string)) { |
||||
240 | return self::random($length); |
||||
241 | } |
||||
242 | |||||
243 | return $string; |
||||
244 | } |
||||
245 | |||||
246 | /** |
||||
247 | * Transliterates polish chars to latin char equivalents |
||||
248 | * @param $string |
||||
249 | * @return mixed |
||||
250 | */ |
||||
251 | public static function polishToLatin($string) |
||||
252 | { |
||||
253 | $polishChars = ['Ą','Ć','Ę','Ł','Ó','Ś','Ź','Ż','Ń','ą','ć','ę','ł','ó','ś','ź','ż','ń']; |
||||
254 | $latinChars = ['A','C','E','L','O','S','Z','Z','N','a','c','e','l','o','s','z','z','n']; |
||||
255 | |||||
256 | return str_replace($polishChars, $latinChars, $string); |
||||
257 | } |
||||
258 | |||||
259 | /** |
||||
260 | * Tries to parse correct string from various price string formats |
||||
261 | * |
||||
262 | * '2000 EUR' -> '2000.00' |
||||
263 | * '2000.00' -> '2000.00' |
||||
264 | * '2,000 EUR' -> '2000.00' |
||||
265 | * '2.000' -> '2000.00' |
||||
266 | * '2.320,34 EUR' -> '2320.34' |
||||
267 | * '2,000,000.31'-> '2000000.31' |
||||
268 | * '2.000.000' -> '2000000.00' |
||||
269 | * '2.00' -> '2.00' |
||||
270 | * '2,000€' -> '2000.00' |
||||
271 | * |
||||
272 | * @param $price |
||||
273 | * @return string |
||||
274 | */ |
||||
275 | public static function cleanupPriceString($price) |
||||
276 | { |
||||
277 | $price = preg_replace('/[^0-9\.,]+/', '', $price); |
||||
278 | $commaPosition = strpos($price, ','); |
||||
279 | $dotPosition = strpos($price, '.'); |
||||
280 | $hasCommaSeparator = $commaPosition > 0; |
||||
281 | $hasDotSeparator = $dotPosition > 0; |
||||
282 | |||||
283 | $explodeBy = null; |
||||
284 | |||||
285 | if ($hasDotSeparator && $hasCommaSeparator) { |
||||
286 | if ($commaPosition > $dotPosition) { |
||||
287 | $explodeBy = ','; |
||||
288 | } else { |
||||
289 | $explodeBy = '.'; |
||||
290 | } |
||||
291 | } elseif ($hasDotSeparator) { |
||||
292 | $explodeBy = '.'; |
||||
293 | } elseif ($hasCommaSeparator) { |
||||
294 | $explodeBy = ','; |
||||
295 | } |
||||
296 | |||||
297 | $decimals = '00'; |
||||
298 | |||||
299 | if ($explodeBy) { |
||||
300 | $exploded = explode($explodeBy, $price); |
||||
301 | $possibleDecimals = $exploded[count($exploded) - 1]; |
||||
302 | |||||
303 | if (strlen($possibleDecimals) === 2) { |
||||
304 | unset($exploded[count($exploded) - 1]); |
||||
305 | $decimals = $possibleDecimals; |
||||
306 | } |
||||
307 | |||||
308 | $price = implode('', $exploded); |
||||
309 | } |
||||
310 | |||||
311 | $price = preg_replace('/[^0-9]+/', '', $price); |
||||
312 | $price = $price . '.' . $decimals; |
||||
313 | |||||
314 | return $price; |
||||
315 | } |
||||
316 | } |
||||
317 |