Total Complexity | 67 |
Total Lines | 635 |
Duplicated Lines | 0 % |
Coverage | 96.19% |
Changes | 0 |
Complex classes like StringHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StringHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class StringHelper |
||
6 | { |
||
7 | /** |
||
8 | * Control characters array. |
||
9 | * |
||
10 | * @var string[] |
||
11 | */ |
||
12 | private static array $controlCharacters = []; |
||
13 | |||
14 | /** |
||
15 | * SYLK Characters array. |
||
16 | */ |
||
17 | private static array $SYLKCharacters = []; |
||
18 | |||
19 | /** |
||
20 | * Decimal separator. |
||
21 | */ |
||
22 | private static ?string $decimalSeparator = null; |
||
23 | |||
24 | /** |
||
25 | * Thousands separator. |
||
26 | */ |
||
27 | private static ?string $thousandsSeparator = null; |
||
28 | |||
29 | /** |
||
30 | * Currency code. |
||
31 | */ |
||
32 | private static ?string $currencyCode = null; |
||
33 | |||
34 | /** |
||
35 | * Is iconv extension avalable? |
||
36 | */ |
||
37 | private static ?bool $isIconvEnabled; |
||
38 | |||
39 | /** |
||
40 | * iconv options. |
||
41 | */ |
||
42 | private static string $iconvOptions = '//IGNORE//TRANSLIT'; |
||
43 | |||
44 | /** |
||
45 | * Build control characters array. |
||
46 | */ |
||
47 | 107 | private static function buildControlCharacters(): void |
|
48 | { |
||
49 | 107 | for ($i = 0; $i <= 31; ++$i) { |
|
50 | 107 | if ($i != 9 && $i != 10 && $i != 13) { |
|
51 | 107 | $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_'; |
|
52 | 107 | $replace = chr($i); |
|
53 | 107 | self::$controlCharacters[$find] = $replace; |
|
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Build SYLK characters array. |
||
60 | */ |
||
61 | 107 | private static function buildSYLKCharacters(): void |
|
62 | { |
||
63 | 107 | self::$SYLKCharacters = [ |
|
64 | 107 | "\x1B 0" => chr(0), |
|
65 | 107 | "\x1B 1" => chr(1), |
|
66 | 107 | "\x1B 2" => chr(2), |
|
67 | 107 | "\x1B 3" => chr(3), |
|
68 | 107 | "\x1B 4" => chr(4), |
|
69 | 107 | "\x1B 5" => chr(5), |
|
70 | 107 | "\x1B 6" => chr(6), |
|
71 | 107 | "\x1B 7" => chr(7), |
|
72 | 107 | "\x1B 8" => chr(8), |
|
73 | 107 | "\x1B 9" => chr(9), |
|
74 | 107 | "\x1B :" => chr(10), |
|
75 | 107 | "\x1B ;" => chr(11), |
|
76 | 107 | "\x1B <" => chr(12), |
|
77 | 107 | "\x1B =" => chr(13), |
|
78 | 107 | "\x1B >" => chr(14), |
|
79 | 107 | "\x1B ?" => chr(15), |
|
80 | 107 | "\x1B!0" => chr(16), |
|
81 | 107 | "\x1B!1" => chr(17), |
|
82 | 107 | "\x1B!2" => chr(18), |
|
83 | 107 | "\x1B!3" => chr(19), |
|
84 | 107 | "\x1B!4" => chr(20), |
|
85 | 107 | "\x1B!5" => chr(21), |
|
86 | 107 | "\x1B!6" => chr(22), |
|
87 | 107 | "\x1B!7" => chr(23), |
|
88 | 107 | "\x1B!8" => chr(24), |
|
89 | 107 | "\x1B!9" => chr(25), |
|
90 | 107 | "\x1B!:" => chr(26), |
|
91 | 107 | "\x1B!;" => chr(27), |
|
92 | 107 | "\x1B!<" => chr(28), |
|
93 | 107 | "\x1B!=" => chr(29), |
|
94 | 107 | "\x1B!>" => chr(30), |
|
95 | 107 | "\x1B!?" => chr(31), |
|
96 | 107 | "\x1B'?" => chr(127), |
|
97 | 107 | "\x1B(0" => '€', // 128 in CP1252 |
|
98 | 107 | "\x1B(2" => '‚', // 130 in CP1252 |
|
99 | 107 | "\x1B(3" => 'ƒ', // 131 in CP1252 |
|
100 | 107 | "\x1B(4" => '„', // 132 in CP1252 |
|
101 | 107 | "\x1B(5" => '…', // 133 in CP1252 |
|
102 | 107 | "\x1B(6" => '†', // 134 in CP1252 |
|
103 | 107 | "\x1B(7" => '‡', // 135 in CP1252 |
|
104 | 107 | "\x1B(8" => 'ˆ', // 136 in CP1252 |
|
105 | 107 | "\x1B(9" => '‰', // 137 in CP1252 |
|
106 | 107 | "\x1B(:" => 'Š', // 138 in CP1252 |
|
107 | 107 | "\x1B(;" => '‹', // 139 in CP1252 |
|
108 | 107 | "\x1BNj" => 'Œ', // 140 in CP1252 |
|
109 | 107 | "\x1B(>" => 'Ž', // 142 in CP1252 |
|
110 | 107 | "\x1B)1" => '‘', // 145 in CP1252 |
|
111 | 107 | "\x1B)2" => '’', // 146 in CP1252 |
|
112 | 107 | "\x1B)3" => '“', // 147 in CP1252 |
|
113 | 107 | "\x1B)4" => '”', // 148 in CP1252 |
|
114 | 107 | "\x1B)5" => '•', // 149 in CP1252 |
|
115 | 107 | "\x1B)6" => '–', // 150 in CP1252 |
|
116 | 107 | "\x1B)7" => '—', // 151 in CP1252 |
|
117 | 107 | "\x1B)8" => '˜', // 152 in CP1252 |
|
118 | 107 | "\x1B)9" => '™', // 153 in CP1252 |
|
119 | 107 | "\x1B):" => 'š', // 154 in CP1252 |
|
120 | 107 | "\x1B);" => '›', // 155 in CP1252 |
|
121 | 107 | "\x1BNz" => 'œ', // 156 in CP1252 |
|
122 | 107 | "\x1B)>" => 'ž', // 158 in CP1252 |
|
123 | 107 | "\x1B)?" => 'Ÿ', // 159 in CP1252 |
|
124 | 107 | "\x1B*0" => ' ', // 160 in CP1252 |
|
125 | 107 | "\x1BN!" => '¡', // 161 in CP1252 |
|
126 | 107 | "\x1BN\"" => '¢', // 162 in CP1252 |
|
127 | 107 | "\x1BN#" => '£', // 163 in CP1252 |
|
128 | 107 | "\x1BN(" => '¤', // 164 in CP1252 |
|
129 | 107 | "\x1BN%" => '¥', // 165 in CP1252 |
|
130 | 107 | "\x1B*6" => '¦', // 166 in CP1252 |
|
131 | 107 | "\x1BN'" => '§', // 167 in CP1252 |
|
132 | 107 | "\x1BNH " => '¨', // 168 in CP1252 |
|
133 | 107 | "\x1BNS" => '©', // 169 in CP1252 |
|
134 | 107 | "\x1BNc" => 'ª', // 170 in CP1252 |
|
135 | 107 | "\x1BN+" => '«', // 171 in CP1252 |
|
136 | 107 | "\x1B*<" => '¬', // 172 in CP1252 |
|
137 | 107 | "\x1B*=" => '', // 173 in CP1252 |
|
138 | 107 | "\x1BNR" => '®', // 174 in CP1252 |
|
139 | 107 | "\x1B*?" => '¯', // 175 in CP1252 |
|
140 | 107 | "\x1BN0" => '°', // 176 in CP1252 |
|
141 | 107 | "\x1BN1" => '±', // 177 in CP1252 |
|
142 | 107 | "\x1BN2" => '²', // 178 in CP1252 |
|
143 | 107 | "\x1BN3" => '³', // 179 in CP1252 |
|
144 | 107 | "\x1BNB " => '´', // 180 in CP1252 |
|
145 | 107 | "\x1BN5" => 'µ', // 181 in CP1252 |
|
146 | 107 | "\x1BN6" => '¶', // 182 in CP1252 |
|
147 | 107 | "\x1BN7" => '·', // 183 in CP1252 |
|
148 | 107 | "\x1B+8" => '¸', // 184 in CP1252 |
|
149 | 107 | "\x1BNQ" => '¹', // 185 in CP1252 |
|
150 | 107 | "\x1BNk" => 'º', // 186 in CP1252 |
|
151 | 107 | "\x1BN;" => '»', // 187 in CP1252 |
|
152 | 107 | "\x1BN<" => '¼', // 188 in CP1252 |
|
153 | 107 | "\x1BN=" => '½', // 189 in CP1252 |
|
154 | 107 | "\x1BN>" => '¾', // 190 in CP1252 |
|
155 | 107 | "\x1BN?" => '¿', // 191 in CP1252 |
|
156 | 107 | "\x1BNAA" => 'À', // 192 in CP1252 |
|
157 | 107 | "\x1BNBA" => 'Á', // 193 in CP1252 |
|
158 | 107 | "\x1BNCA" => 'Â', // 194 in CP1252 |
|
159 | 107 | "\x1BNDA" => 'Ã', // 195 in CP1252 |
|
160 | 107 | "\x1BNHA" => 'Ä', // 196 in CP1252 |
|
161 | 107 | "\x1BNJA" => 'Å', // 197 in CP1252 |
|
162 | 107 | "\x1BNa" => 'Æ', // 198 in CP1252 |
|
163 | 107 | "\x1BNKC" => 'Ç', // 199 in CP1252 |
|
164 | 107 | "\x1BNAE" => 'È', // 200 in CP1252 |
|
165 | 107 | "\x1BNBE" => 'É', // 201 in CP1252 |
|
166 | 107 | "\x1BNCE" => 'Ê', // 202 in CP1252 |
|
167 | 107 | "\x1BNHE" => 'Ë', // 203 in CP1252 |
|
168 | 107 | "\x1BNAI" => 'Ì', // 204 in CP1252 |
|
169 | 107 | "\x1BNBI" => 'Í', // 205 in CP1252 |
|
170 | 107 | "\x1BNCI" => 'Î', // 206 in CP1252 |
|
171 | 107 | "\x1BNHI" => 'Ï', // 207 in CP1252 |
|
172 | 107 | "\x1BNb" => 'Ð', // 208 in CP1252 |
|
173 | 107 | "\x1BNDN" => 'Ñ', // 209 in CP1252 |
|
174 | 107 | "\x1BNAO" => 'Ò', // 210 in CP1252 |
|
175 | 107 | "\x1BNBO" => 'Ó', // 211 in CP1252 |
|
176 | 107 | "\x1BNCO" => 'Ô', // 212 in CP1252 |
|
177 | 107 | "\x1BNDO" => 'Õ', // 213 in CP1252 |
|
178 | 107 | "\x1BNHO" => 'Ö', // 214 in CP1252 |
|
179 | 107 | "\x1B-7" => '×', // 215 in CP1252 |
|
180 | 107 | "\x1BNi" => 'Ø', // 216 in CP1252 |
|
181 | 107 | "\x1BNAU" => 'Ù', // 217 in CP1252 |
|
182 | 107 | "\x1BNBU" => 'Ú', // 218 in CP1252 |
|
183 | 107 | "\x1BNCU" => 'Û', // 219 in CP1252 |
|
184 | 107 | "\x1BNHU" => 'Ü', // 220 in CP1252 |
|
185 | 107 | "\x1B-=" => 'Ý', // 221 in CP1252 |
|
186 | 107 | "\x1BNl" => 'Þ', // 222 in CP1252 |
|
187 | 107 | "\x1BN{" => 'ß', // 223 in CP1252 |
|
188 | 107 | "\x1BNAa" => 'à', // 224 in CP1252 |
|
189 | 107 | "\x1BNBa" => 'á', // 225 in CP1252 |
|
190 | 107 | "\x1BNCa" => 'â', // 226 in CP1252 |
|
191 | 107 | "\x1BNDa" => 'ã', // 227 in CP1252 |
|
192 | 107 | "\x1BNHa" => 'ä', // 228 in CP1252 |
|
193 | 107 | "\x1BNJa" => 'å', // 229 in CP1252 |
|
194 | 107 | "\x1BNq" => 'æ', // 230 in CP1252 |
|
195 | 107 | "\x1BNKc" => 'ç', // 231 in CP1252 |
|
196 | 107 | "\x1BNAe" => 'è', // 232 in CP1252 |
|
197 | 107 | "\x1BNBe" => 'é', // 233 in CP1252 |
|
198 | 107 | "\x1BNCe" => 'ê', // 234 in CP1252 |
|
199 | 107 | "\x1BNHe" => 'ë', // 235 in CP1252 |
|
200 | 107 | "\x1BNAi" => 'ì', // 236 in CP1252 |
|
201 | 107 | "\x1BNBi" => 'í', // 237 in CP1252 |
|
202 | 107 | "\x1BNCi" => 'î', // 238 in CP1252 |
|
203 | 107 | "\x1BNHi" => 'ï', // 239 in CP1252 |
|
204 | 107 | "\x1BNs" => 'ð', // 240 in CP1252 |
|
205 | 107 | "\x1BNDn" => 'ñ', // 241 in CP1252 |
|
206 | 107 | "\x1BNAo" => 'ò', // 242 in CP1252 |
|
207 | 107 | "\x1BNBo" => 'ó', // 243 in CP1252 |
|
208 | 107 | "\x1BNCo" => 'ô', // 244 in CP1252 |
|
209 | 107 | "\x1BNDo" => 'õ', // 245 in CP1252 |
|
210 | 107 | "\x1BNHo" => 'ö', // 246 in CP1252 |
|
211 | 107 | "\x1B/7" => '÷', // 247 in CP1252 |
|
212 | 107 | "\x1BNy" => 'ø', // 248 in CP1252 |
|
213 | 107 | "\x1BNAu" => 'ù', // 249 in CP1252 |
|
214 | 107 | "\x1BNBu" => 'ú', // 250 in CP1252 |
|
215 | 107 | "\x1BNCu" => 'û', // 251 in CP1252 |
|
216 | 107 | "\x1BNHu" => 'ü', // 252 in CP1252 |
|
217 | 107 | "\x1B/=" => 'ý', // 253 in CP1252 |
|
218 | 107 | "\x1BN|" => 'þ', // 254 in CP1252 |
|
219 | 107 | "\x1BNHy" => 'ÿ', // 255 in CP1252 |
|
220 | 107 | ]; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * Get whether iconv extension is available. |
||
225 | */ |
||
226 | 217 | public static function getIsIconvEnabled(): bool |
|
227 | { |
||
228 | 217 | if (isset(self::$isIconvEnabled)) { |
|
229 | 217 | return self::$isIconvEnabled; |
|
230 | } |
||
231 | |||
232 | // Assume no problems with iconv |
||
233 | 75 | self::$isIconvEnabled = true; |
|
234 | |||
235 | // Fail if iconv doesn't exist |
||
236 | 75 | if (!function_exists('iconv')) { |
|
237 | self::$isIconvEnabled = false; |
||
238 | 75 | } elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) { |
|
239 | // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false, |
||
240 | self::$isIconvEnabled = false; |
||
241 | 75 | } elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) { |
|
242 | // CUSTOM: IBM AIX iconv() does not work |
||
243 | self::$isIconvEnabled = false; |
||
244 | } |
||
245 | |||
246 | // Deactivate iconv default options if they fail (as seen on IMB i) |
||
247 | 75 | if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) { |
|
248 | self::$iconvOptions = ''; |
||
249 | } |
||
250 | |||
251 | 75 | return self::$isIconvEnabled; |
|
252 | } |
||
253 | |||
254 | 614 | private static function buildCharacterSets(): void |
|
255 | { |
||
256 | 614 | if (empty(self::$controlCharacters)) { |
|
257 | 107 | self::buildControlCharacters(); |
|
258 | } |
||
259 | |||
260 | 614 | if (empty(self::$SYLKCharacters)) { |
|
261 | 107 | self::buildSYLKCharacters(); |
|
262 | } |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Convert from OpenXML escaped control character to PHP control character. |
||
267 | * |
||
268 | * Excel 2007 team: |
||
269 | * ---------------- |
||
270 | * That's correct, control characters are stored directly in the shared-strings table. |
||
271 | * We do encode characters that cannot be represented in XML using the following escape sequence: |
||
272 | * _xHHHH_ where H represents a hexadecimal character in the character's value... |
||
273 | * So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
||
274 | * element or in the shared string <t> element. |
||
275 | * |
||
276 | * @param string $textValue Value to unescape |
||
277 | */ |
||
278 | 489 | public static function controlCharacterOOXML2PHP(string $textValue): string |
|
279 | { |
||
280 | 489 | self::buildCharacterSets(); |
|
281 | |||
282 | 489 | return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $textValue); |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * Convert from PHP control character to OpenXML escaped control character. |
||
287 | * |
||
288 | * Excel 2007 team: |
||
289 | * ---------------- |
||
290 | * That's correct, control characters are stored directly in the shared-strings table. |
||
291 | * We do encode characters that cannot be represented in XML using the following escape sequence: |
||
292 | * _xHHHH_ where H represents a hexadecimal character in the character's value... |
||
293 | * So you could end up with something like _x0008_ in a string (either in a cell value (<v>) |
||
294 | * element or in the shared string <t> element. |
||
295 | * |
||
296 | * @param string $textValue Value to escape |
||
297 | */ |
||
298 | 261 | public static function controlCharacterPHP2OOXML(string $textValue): string |
|
299 | { |
||
300 | 261 | self::buildCharacterSets(); |
|
301 | |||
302 | 261 | return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $textValue); |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * Try to sanitize UTF8, replacing invalid sequences with Unicode substitution characters. |
||
307 | */ |
||
308 | 8916 | public static function sanitizeUTF8(string $textValue): string |
|
309 | { |
||
310 | 8916 | $textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue); |
|
311 | 8916 | $subst = mb_substitute_character(); // default is question mark |
|
312 | 8916 | mb_substitute_character(65533); // Unicode substitution character |
|
313 | // Phpstan does not think this can return false. |
||
314 | 8916 | $returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8'); |
|
315 | 8916 | mb_substitute_character($subst); |
|
316 | |||
317 | 8916 | return $returnValue; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Check if a string contains UTF8 data. |
||
322 | */ |
||
323 | 1 | public static function isUTF8(string $textValue): bool |
|
324 | { |
||
325 | 1 | return $textValue === self::sanitizeUTF8($textValue); |
|
326 | } |
||
327 | |||
328 | /** |
||
329 | * Formats a numeric value as a string for output in various output writers forcing |
||
330 | * point as decimal separator in case locale is other than English. |
||
331 | */ |
||
332 | 911 | public static function formatNumber(float|int|string|null $numericValue): string |
|
333 | { |
||
334 | 911 | if (is_float($numericValue)) { |
|
335 | 911 | return str_replace(',', '.', (string) $numericValue); |
|
336 | } |
||
337 | |||
338 | 87 | return (string) $numericValue; |
|
339 | } |
||
340 | |||
341 | /** |
||
342 | * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) |
||
343 | * Writes the string using uncompressed notation, no rich text, no Asian phonetics |
||
344 | * If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
||
345 | * although this will give wrong results for non-ASCII strings |
||
346 | * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
||
347 | * |
||
348 | * @param string $textValue UTF-8 encoded string |
||
349 | * @param array<int, array{strlen: int, fontidx: int}> $arrcRuns Details of rich text runs in $value |
||
350 | */ |
||
351 | 109 | public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string |
|
352 | { |
||
353 | // character count |
||
354 | 109 | $ln = self::countCharacters($textValue, 'UTF-8'); |
|
355 | // option flags |
||
356 | 109 | if (empty($arrcRuns)) { |
|
357 | 109 | $data = pack('CC', $ln, 0x0001); |
|
358 | // characters |
||
359 | 109 | $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
|
360 | } else { |
||
361 | 11 | $data = pack('vC', $ln, 0x09); |
|
362 | 11 | $data .= pack('v', count($arrcRuns)); |
|
363 | // characters |
||
364 | 11 | $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
|
365 | 11 | foreach ($arrcRuns as $cRun) { |
|
366 | 11 | $data .= pack('v', $cRun['strlen']); |
|
367 | 11 | $data .= pack('v', $cRun['fontidx']); |
|
368 | } |
||
369 | } |
||
370 | |||
371 | 109 | return $data; |
|
372 | } |
||
373 | |||
374 | /** |
||
375 | * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) |
||
376 | * Writes the string using uncompressed notation, no rich text, no Asian phonetics |
||
377 | * If mbstring extension is not available, ASCII is assumed, and compressed notation is used |
||
378 | * although this will give wrong results for non-ASCII strings |
||
379 | * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3. |
||
380 | * |
||
381 | * @param string $textValue UTF-8 encoded string |
||
382 | */ |
||
383 | 110 | public static function UTF8toBIFF8UnicodeLong(string $textValue): string |
|
384 | { |
||
385 | // characters |
||
386 | 110 | $chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8'); |
|
387 | 110 | $ln = (int) (strlen($chars) / 2); // N.B. - strlen, not mb_strlen issue #642 |
|
388 | |||
389 | 110 | return pack('vC', $ln, 0x0001) . $chars; |
|
390 | } |
||
391 | |||
392 | /** |
||
393 | * Convert string from one encoding to another. |
||
394 | * |
||
395 | * @param string $to Encoding to convert to, e.g. 'UTF-8' |
||
396 | * @param string $from Encoding to convert from, e.g. 'UTF-16LE' |
||
397 | */ |
||
398 | 216 | public static function convertEncoding(string $textValue, string $to, string $from): string |
|
399 | { |
||
400 | 216 | if (self::getIsIconvEnabled()) { |
|
401 | 216 | $result = iconv($from, $to . self::$iconvOptions, $textValue); |
|
402 | 216 | if (false !== $result) { |
|
403 | 216 | return $result; |
|
404 | } |
||
405 | } |
||
406 | |||
407 | return mb_convert_encoding($textValue, $to, $from); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Get character count. |
||
412 | * |
||
413 | * @param string $encoding Encoding |
||
414 | * |
||
415 | * @return int Character count |
||
416 | */ |
||
417 | 10329 | public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int |
|
418 | { |
||
419 | 10329 | return mb_strlen($textValue, $encoding); |
|
420 | } |
||
421 | |||
422 | /** |
||
423 | * Get character count using mb_strwidth rather than mb_strlen. |
||
424 | * |
||
425 | * @param string $encoding Encoding |
||
426 | * |
||
427 | * @return int Character count |
||
428 | */ |
||
429 | 75 | public static function countCharactersDbcs(string $textValue, string $encoding = 'UTF-8'): int |
|
430 | { |
||
431 | 75 | return mb_strwidth($textValue, $encoding); |
|
432 | } |
||
433 | |||
434 | /** |
||
435 | * Get a substring of a UTF-8 encoded string. |
||
436 | * |
||
437 | * @param string $textValue UTF-8 encoded string |
||
438 | * @param int $offset Start offset |
||
439 | * @param ?int $length Maximum number of characters in substring |
||
440 | */ |
||
441 | 10300 | public static function substring(string $textValue, int $offset, ?int $length = 0): string |
|
442 | { |
||
443 | 10300 | return mb_substr($textValue, $offset, $length, 'UTF-8'); |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * Convert a UTF-8 encoded string to upper case. |
||
448 | * |
||
449 | * @param string $textValue UTF-8 encoded string |
||
450 | */ |
||
451 | 10115 | public static function strToUpper(string $textValue): string |
|
452 | { |
||
453 | 10115 | return mb_convert_case($textValue, MB_CASE_UPPER, 'UTF-8'); |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * Convert a UTF-8 encoded string to lower case. |
||
458 | * |
||
459 | * @param string $textValue UTF-8 encoded string |
||
460 | */ |
||
461 | 9873 | public static function strToLower(string $textValue): string |
|
462 | { |
||
463 | 9873 | return mb_convert_case($textValue, MB_CASE_LOWER, 'UTF-8'); |
|
464 | } |
||
465 | |||
466 | /** |
||
467 | * Convert a UTF-8 encoded string to title/proper case |
||
468 | * (uppercase every first character in each word, lower case all other characters). |
||
469 | * |
||
470 | * @param string $textValue UTF-8 encoded string |
||
471 | */ |
||
472 | 18 | public static function strToTitle(string $textValue): string |
|
473 | { |
||
474 | 18 | return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8'); |
|
475 | } |
||
476 | |||
477 | 21 | public static function mbIsUpper(string $character): bool |
|
480 | } |
||
481 | |||
482 | /** |
||
483 | * Splits a UTF-8 string into an array of individual characters. |
||
484 | */ |
||
485 | 21 | public static function mbStrSplit(string $string): array |
|
486 | { |
||
487 | // Split at all position not after the start: ^ |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Reverse the case of a string, so that all uppercase characters become lowercase |
||
496 | * and all lowercase characters become uppercase. |
||
497 | * |
||
498 | * @param string $textValue UTF-8 encoded string |
||
499 | */ |
||
500 | 21 | public static function strCaseReverse(string $textValue): string |
|
512 | } |
||
513 | |||
514 | private static function useAlt(string $altValue, string $default, bool $trimAlt): string |
||
515 | { |
||
516 | return ($trimAlt ? trim($altValue) : $altValue) ?: $default; |
||
517 | } |
||
518 | 1087 | ||
519 | private static function getLocaleValue(string $key, string $altKey, string $default, bool $trimAlt = false): string |
||
520 | 1087 | { |
|
521 | 117 | $localeconv = localeconv(); |
|
522 | 117 | $rslt = $localeconv[$key]; |
|
523 | 117 | // win-1252 implements Euro as 0x80 plus other symbols |
|
524 | if (preg_match('//u', $rslt) !== 1) { |
||
525 | 117 | $rslt = ''; |
|
526 | } |
||
527 | |||
528 | return $rslt ?: self::useAlt($localeconv[$altKey], $default, $trimAlt); |
||
529 | } |
||
530 | |||
531 | 1087 | /** |
|
532 | * Get the decimal separator. If it has not yet been set explicitly, try to obtain number |
||
533 | * formatting information from locale. |
||
534 | */ |
||
535 | public static function getDecimalSeparator(): string |
||
536 | { |
||
537 | if (!isset(self::$decimalSeparator)) { |
||
538 | self::$decimalSeparator = self::getLocaleValue('decimal_point', 'mon_decimal_point', '.'); |
||
539 | } |
||
540 | 923 | ||
541 | return self::$decimalSeparator; |
||
542 | 923 | } |
|
543 | |||
544 | /** |
||
545 | * Set the decimal separator. Only used by NumberFormat::toFormattedString() |
||
546 | * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
||
547 | * |
||
548 | * @param ?string $separator Character for decimal separator |
||
549 | 1099 | */ |
|
550 | public static function setDecimalSeparator(?string $separator): void |
||
553 | 118 | } |
|
554 | 118 | ||
555 | /** |
||
556 | 118 | * Get the thousands separator. If it has not yet been set explicitly, try to obtain number |
|
557 | * formatting information from locale. |
||
558 | */ |
||
559 | public static function getThousandsSeparator(): string |
||
560 | { |
||
561 | if (!isset(self::$thousandsSeparator)) { |
||
562 | 1099 | self::$thousandsSeparator = self::getLocaleValue('thousands_sep', 'mon_thousands_sep', ','); |
|
563 | } |
||
564 | |||
565 | return self::$thousandsSeparator; |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Set the thousands separator. Only used by NumberFormat::toFormattedString() |
||
570 | * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
||
571 | 923 | * |
|
572 | * @param ?string $separator Character for thousands separator |
||
573 | 923 | */ |
|
574 | public static function setThousandsSeparator(?string $separator): void |
||
575 | { |
||
576 | self::$thousandsSeparator = $separator; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | 77 | * Get the currency code. If it has not yet been set explicitly, try to obtain the |
|
581 | * symbol information from locale. |
||
582 | 77 | */ |
|
583 | 56 | public static function getCurrencyCode(bool $trimAlt = false): string |
|
584 | { |
||
585 | 27 | if (!isset(self::$currencyCode)) { |
|
586 | 27 | self::$currencyCode = self::getLocaleValue('currency_symbol', 'int_curr_symbol', '$', $trimAlt); |
|
587 | 27 | } |
|
588 | 27 | ||
589 | return self::$currencyCode; |
||
1 ignored issue
–
show
|
|||
590 | 27 | } |
|
591 | |||
592 | /** |
||
593 | * Set the currency code. Only used by NumberFormat::toFormattedString() |
||
594 | * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf. |
||
595 | * |
||
596 | * @param ?string $currencyCode Character for currency code |
||
597 | */ |
||
598 | public static function setCurrencyCode(?string $currencyCode): void |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Convert SYLK encoded string to UTF-8. |
||
605 | * |
||
606 | * @param string $textValue SYLK encoded string |
||
607 | 922 | * |
|
608 | * @return string UTF-8 encoded string |
||
609 | 922 | */ |
|
610 | public static function SYLKtoUTF8(string $textValue): string |
||
611 | { |
||
612 | self::buildCharacterSets(); |
||
613 | |||
614 | // If there is no escape character in the string there is nothing to do |
||
615 | if (!str_contains($textValue, '')) { |
||
616 | return $textValue; |
||
617 | } |
||
618 | |||
619 | 11 | foreach (self::$SYLKCharacters as $k => $v) { |
|
620 | $textValue = str_replace($k, $v, $textValue); |
||
621 | 11 | } |
|
622 | |||
623 | return $textValue; |
||
624 | 11 | } |
|
625 | 10 | ||
626 | /** |
||
627 | * Retrieve any leading numeric part of a string, or return the full string if no leading numeric |
||
628 | 3 | * (handles basic integer or float, but not exponent or non decimal). |
|
629 | 3 | * |
|
630 | * @return float|string string or only the leading numeric part of the string |
||
631 | */ |
||
632 | 3 | public static function testStringAsNumeric(string $textValue): float|string |
|
640 | } |
||
641 | } |
||
642 |