1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The core class for unicode string manipulations inside other framework classes. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\DesignPatterns\AbstractSingleton as SingletonPattern; |
10
|
|
|
use \CryptoManana\Core\Interfaces\DesignPatterns\CoreStringBuilderInterface as StringManipulations; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class StringBuilder - The core component for string manipulations and encoding support. |
14
|
|
|
* |
15
|
|
|
* @package CryptoManana\Core |
16
|
|
|
*/ |
17
|
|
|
class StringBuilder extends SingletonPattern implements StringManipulations |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Internal flag to enable or disable the `mbstring` extension usage. |
21
|
|
|
* |
22
|
|
|
* @var bool Enable or disable unicode `mbstring` functions. |
23
|
|
|
*/ |
24
|
|
|
protected static $usingMbString = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Replace all occurrences of the search string with the replacement string. |
28
|
|
|
* |
29
|
|
|
* @param string|array|mixed $search The string for searching or an array with multiple values. |
30
|
|
|
* @param string|array|mixed $replace The replacement string or an array with multiple values. |
31
|
|
|
* @param string $subject The string being searched and replaced on. |
32
|
|
|
* @param null|int $count This will hold the number of matched and replaced values. |
33
|
|
|
* |
34
|
|
|
* @return string|array Returns a string with the replaced values. |
35
|
|
|
*/ |
36
|
4 |
|
protected static function multiByteStringReplace($search, $replace, $subject, &$count = null) |
37
|
|
|
{ |
38
|
4 |
|
$searches = is_array($search) ? array_values($search) : [$search]; |
39
|
|
|
|
40
|
4 |
|
$replacements = is_array($replace) ? array_values($replace) : [$replace]; |
41
|
4 |
|
$replacements = array_pad($replacements, count($searches), ''); |
42
|
|
|
|
43
|
4 |
|
foreach ($searches as $key => $searched) { |
44
|
4 |
|
$parts = mb_split(preg_quote($searched), $subject); |
45
|
4 |
|
$count += count($parts) - 1; |
46
|
4 |
|
$subject = implode($replacements[$key], $parts); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
return $subject; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check if the the `mbstring` extension usage is enabled or not. |
54
|
|
|
* |
55
|
|
|
* @return bool Is the component using `mbstring`. |
56
|
|
|
*/ |
57
|
2 |
|
public static function isUsingMbString() |
58
|
|
|
{ |
59
|
2 |
|
return self::$usingMbString; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Enable or disable the `mbstring` extension usage. |
64
|
|
|
* |
65
|
|
|
* @param bool|int $bool Flag for enabling or disabling the `mbstring` usage. |
66
|
|
|
* |
67
|
|
|
* @internal Use the `mbstring` extension only when you need custom encoding support. |
68
|
|
|
*/ |
69
|
20 |
|
public static function useMbString($bool = true) |
70
|
|
|
{ |
71
|
20 |
|
if ($bool == true) { |
72
|
20 |
|
self::$usingMbString = extension_loaded('mbstring'); |
73
|
|
|
} else { |
74
|
20 |
|
self::$usingMbString = false; |
75
|
|
|
} |
76
|
20 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the string's length. |
80
|
|
|
* |
81
|
|
|
* @param string|mixed $string The string for length measuring. |
82
|
|
|
* |
83
|
|
|
* @return int|false The string length or false on invalid parameter given. |
84
|
|
|
*/ |
85
|
48 |
|
public static function stringLength($string) |
86
|
|
|
{ |
87
|
48 |
|
if (!is_string($string)) { |
88
|
2 |
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
48 |
|
return (self::$usingMbString) ? mb_strlen($string) : strlen($string); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Make a string uppercase. |
96
|
|
|
* |
97
|
|
|
* @param string|mixed $string The string for uppercase conversion. |
98
|
|
|
* |
99
|
|
|
* @return string|false The string converted to uppercase or false on invalid parameter given. |
100
|
|
|
*/ |
101
|
282 |
|
public static function stringToUpper($string) |
102
|
|
|
{ |
103
|
282 |
|
if (!is_string($string)) { |
104
|
2 |
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
282 |
|
return (self::$usingMbString) ? mb_strtoupper($string) : strtoupper($string); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Make a string lowercase. |
112
|
|
|
* |
113
|
|
|
* @param string|mixed $string The string for lowercase conversion. |
114
|
|
|
* |
115
|
|
|
* @return string|false The string converted to lowercase or false on invalid parameter given. |
116
|
|
|
*/ |
117
|
2 |
|
public static function stringToLower($string) |
118
|
|
|
{ |
119
|
2 |
|
if (!is_string($string)) { |
120
|
2 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
return (self::$usingMbString) ? mb_strtolower($string) : strtolower($string); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get a character by its encoding integer code value. |
128
|
|
|
* |
129
|
|
|
* @param int $byteValue The integer code numerical value for character conversion. |
130
|
|
|
* |
131
|
|
|
* @return string|false The wanted character string or false on invalid parameter given. |
132
|
|
|
*/ |
133
|
46 |
|
public static function getChr($byteValue) |
134
|
|
|
{ |
135
|
46 |
|
$byteValue = filter_var( |
136
|
46 |
|
$byteValue, |
137
|
46 |
|
FILTER_VALIDATE_INT, |
138
|
|
|
[ |
139
|
|
|
"options" => [ |
140
|
46 |
|
"min_range" => 0, |
141
|
46 |
|
"max_range" => PHP_INT_MAX, |
142
|
|
|
], |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
|
146
|
46 |
|
if ($byteValue === false) { |
147
|
2 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$mbStringIsAvailable = ( |
151
|
46 |
|
self::$usingMbString && |
152
|
46 |
|
function_exists('mb_chr') // PHP >= 7.2.0 |
153
|
|
|
); |
154
|
|
|
|
155
|
46 |
|
return ($mbStringIsAvailable) ? mb_chr($byteValue) : chr($byteValue); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get a character's encoding integer code by its string representation. |
160
|
|
|
* |
161
|
|
|
* @param string|mixed $string The character string value for integer code conversion. |
162
|
|
|
* |
163
|
|
|
* @return int|false The wanted character code or false on invalid parameter given. |
164
|
|
|
*/ |
165
|
8 |
|
public static function getOrd($string) |
166
|
|
|
{ |
167
|
8 |
|
if (!is_string($string)) { |
168
|
2 |
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$mbStringIsAvailable = ( |
172
|
8 |
|
self::$usingMbString && |
173
|
8 |
|
function_exists('mb_ord') // PHP >= 7.2.0 |
174
|
|
|
); |
175
|
|
|
|
176
|
8 |
|
return ($mbStringIsAvailable) ? mb_ord($string) : ord($string); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Reverse a string. |
181
|
|
|
* |
182
|
|
|
* @param string|mixed $string The string for reversing. |
183
|
|
|
* |
184
|
|
|
* @return string|false The reversed string or false on invalid parameter given. |
185
|
|
|
*/ |
186
|
102 |
|
public static function stringReverse($string) |
187
|
|
|
{ |
188
|
102 |
|
if (!is_string($string)) { |
189
|
2 |
|
return false; |
190
|
|
|
} |
191
|
|
|
|
192
|
102 |
|
if (self::$usingMbString) { |
193
|
2 |
|
$length = mb_strlen($string); |
194
|
|
|
|
195
|
2 |
|
$tmp = ''; |
196
|
|
|
|
197
|
2 |
|
while ($length > 0) { |
198
|
2 |
|
$length--; |
199
|
|
|
|
200
|
2 |
|
$tmp .= mb_substr($string, $length, 1); |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
return $tmp; |
204
|
|
|
} else { |
205
|
102 |
|
return strrev($string); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Convert a string to an array. |
211
|
|
|
* |
212
|
|
|
* @param string|mixed $string The string for conversion to array. |
213
|
|
|
* @param int $chunkLength The chunk length for string splitting. |
214
|
|
|
* |
215
|
|
|
* @return array|false The string converted to an array or false on invalid parameter given. |
216
|
|
|
*/ |
217
|
20 |
|
public static function stringSplit($string, $chunkLength = 1) |
218
|
|
|
{ |
219
|
20 |
|
if (!is_string($string) || $chunkLength < 1) { |
220
|
2 |
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
20 |
|
$length = self::stringLength($string); |
224
|
|
|
|
225
|
20 |
|
if ($chunkLength >= $length) { |
226
|
2 |
|
return [$string]; |
227
|
|
|
} |
228
|
|
|
|
229
|
20 |
|
if (self::$usingMbString) { |
230
|
2 |
|
$tmp = []; |
231
|
|
|
|
232
|
|
|
do { |
233
|
2 |
|
$tmp[] = mb_substr($string, 0, $chunkLength); |
234
|
2 |
|
$string = mb_substr($string, $chunkLength); |
235
|
2 |
|
} while ($string !== '' && $string !== false); |
236
|
|
|
|
237
|
2 |
|
return $tmp; |
238
|
|
|
} else { |
239
|
20 |
|
return str_split($string, $chunkLength); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Replace all occurrences of the search string with the replacement string. It also supports arrays. |
245
|
|
|
* |
246
|
|
|
* @param string|array|mixed $search The string for searching or an array with multiple values. |
247
|
|
|
* @param string|array|mixed $replace The replacement string or an array with multiple values. |
248
|
|
|
* @param string|array $subject The string or array being searched and replaced on. |
249
|
|
|
* @param null|int $count This will hold the number of matched and replaced values. |
250
|
|
|
* |
251
|
|
|
* @return string|array Returns a string or an array with the replaced values. |
252
|
|
|
*/ |
253
|
148 |
|
public static function stringReplace($search, $replace, $subject, &$count = null) |
254
|
|
|
{ |
255
|
148 |
|
if (self::$usingMbString) { |
256
|
4 |
|
if (is_array($subject)) { |
257
|
2 |
|
foreach ($subject as $key => $value) { |
258
|
2 |
|
$subject[$key] = self::stringReplace($search, $replace, $value, $count); |
259
|
|
|
} |
260
|
|
|
} else { |
261
|
4 |
|
$subject = self::multiByteStringReplace($search, $replace, $subject, $count); |
262
|
|
|
} |
263
|
|
|
|
264
|
4 |
|
return $subject; |
265
|
|
|
} else { |
266
|
|
|
// This function is Unicode ready and does not have a `mb_*` substitute |
267
|
148 |
|
return str_replace($search, $replace, $subject, $count); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Fully strip whitespace from every position of a string. |
273
|
|
|
* |
274
|
|
|
* @param string|mixed $string The string for full trimming. |
275
|
|
|
* |
276
|
|
|
* @return string|false The fully trimmed string or false on invalid parameter given. |
277
|
|
|
*/ |
278
|
8 |
|
public static function stringFullTrimming($string) |
279
|
|
|
{ |
280
|
8 |
|
if (!is_string($string)) { |
281
|
2 |
|
return false; |
282
|
|
|
} |
283
|
|
|
|
284
|
8 |
|
return self::stringReplace([" ", "\t", "\n", "\r", "\0", "\x0B"], '', $string); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|