Passed
Pull Request — master (#4323)
by Owen
11:36
created

StringHelper::strlenAllowNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
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 106
    private static function buildControlCharacters(): void
48
    {
49 106
        for ($i = 0; $i <= 31; ++$i) {
50 106
            if ($i != 9 && $i != 10 && $i != 13) {
51 106
                $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
52 106
                $replace = chr($i);
53 106
                self::$controlCharacters[$find] = $replace;
54
            }
55
        }
56
    }
57
58
    /**
59
     * Build SYLK characters array.
60
     */
61 106
    private static function buildSYLKCharacters(): void
62
    {
63 106
        self::$SYLKCharacters = [
64 106
            "\x1B 0" => chr(0),
65 106
            "\x1B 1" => chr(1),
66 106
            "\x1B 2" => chr(2),
67 106
            "\x1B 3" => chr(3),
68 106
            "\x1B 4" => chr(4),
69 106
            "\x1B 5" => chr(5),
70 106
            "\x1B 6" => chr(6),
71 106
            "\x1B 7" => chr(7),
72 106
            "\x1B 8" => chr(8),
73 106
            "\x1B 9" => chr(9),
74 106
            "\x1B :" => chr(10),
75 106
            "\x1B ;" => chr(11),
76 106
            "\x1B <" => chr(12),
77 106
            "\x1B =" => chr(13),
78 106
            "\x1B >" => chr(14),
79 106
            "\x1B ?" => chr(15),
80 106
            "\x1B!0" => chr(16),
81 106
            "\x1B!1" => chr(17),
82 106
            "\x1B!2" => chr(18),
83 106
            "\x1B!3" => chr(19),
84 106
            "\x1B!4" => chr(20),
85 106
            "\x1B!5" => chr(21),
86 106
            "\x1B!6" => chr(22),
87 106
            "\x1B!7" => chr(23),
88 106
            "\x1B!8" => chr(24),
89 106
            "\x1B!9" => chr(25),
90 106
            "\x1B!:" => chr(26),
91 106
            "\x1B!;" => chr(27),
92 106
            "\x1B!<" => chr(28),
93 106
            "\x1B!=" => chr(29),
94 106
            "\x1B!>" => chr(30),
95 106
            "\x1B!?" => chr(31),
96 106
            "\x1B'?" => chr(127),
97 106
            "\x1B(0" => '€', // 128 in CP1252
98 106
            "\x1B(2" => '‚', // 130 in CP1252
99 106
            "\x1B(3" => 'ƒ', // 131 in CP1252
100 106
            "\x1B(4" => '„', // 132 in CP1252
101 106
            "\x1B(5" => '…', // 133 in CP1252
102 106
            "\x1B(6" => '†', // 134 in CP1252
103 106
            "\x1B(7" => '‡', // 135 in CP1252
104 106
            "\x1B(8" => 'ˆ', // 136 in CP1252
105 106
            "\x1B(9" => '‰', // 137 in CP1252
106 106
            "\x1B(:" => 'Š', // 138 in CP1252
107 106
            "\x1B(;" => '‹', // 139 in CP1252
108 106
            "\x1BNj" => 'Œ', // 140 in CP1252
109 106
            "\x1B(>" => 'Ž', // 142 in CP1252
110 106
            "\x1B)1" => '‘', // 145 in CP1252
111 106
            "\x1B)2" => '’', // 146 in CP1252
112 106
            "\x1B)3" => '“', // 147 in CP1252
113 106
            "\x1B)4" => '”', // 148 in CP1252
114 106
            "\x1B)5" => '•', // 149 in CP1252
115 106
            "\x1B)6" => '–', // 150 in CP1252
116 106
            "\x1B)7" => '—', // 151 in CP1252
117 106
            "\x1B)8" => '˜', // 152 in CP1252
118 106
            "\x1B)9" => '™', // 153 in CP1252
119 106
            "\x1B):" => 'š', // 154 in CP1252
120 106
            "\x1B);" => '›', // 155 in CP1252
121 106
            "\x1BNz" => 'œ', // 156 in CP1252
122 106
            "\x1B)>" => 'ž', // 158 in CP1252
123 106
            "\x1B)?" => 'Ÿ', // 159 in CP1252
124 106
            "\x1B*0" => ' ', // 160 in CP1252
125 106
            "\x1BN!" => '¡', // 161 in CP1252
126 106
            "\x1BN\"" => '¢', // 162 in CP1252
127 106
            "\x1BN#" => '£', // 163 in CP1252
128 106
            "\x1BN(" => '¤', // 164 in CP1252
129 106
            "\x1BN%" => '¥', // 165 in CP1252
130 106
            "\x1B*6" => '¦', // 166 in CP1252
131 106
            "\x1BN'" => '§', // 167 in CP1252
132 106
            "\x1BNH " => '¨', // 168 in CP1252
133 106
            "\x1BNS" => '©', // 169 in CP1252
134 106
            "\x1BNc" => 'ª', // 170 in CP1252
135 106
            "\x1BN+" => '«', // 171 in CP1252
136 106
            "\x1B*<" => '¬', // 172 in CP1252
137 106
            "\x1B*=" => '­', // 173 in CP1252
138 106
            "\x1BNR" => '®', // 174 in CP1252
139 106
            "\x1B*?" => '¯', // 175 in CP1252
140 106
            "\x1BN0" => '°', // 176 in CP1252
141 106
            "\x1BN1" => '±', // 177 in CP1252
142 106
            "\x1BN2" => '²', // 178 in CP1252
143 106
            "\x1BN3" => '³', // 179 in CP1252
144 106
            "\x1BNB " => '´', // 180 in CP1252
145 106
            "\x1BN5" => 'µ', // 181 in CP1252
146 106
            "\x1BN6" => '¶', // 182 in CP1252
147 106
            "\x1BN7" => '·', // 183 in CP1252
148 106
            "\x1B+8" => '¸', // 184 in CP1252
149 106
            "\x1BNQ" => '¹', // 185 in CP1252
150 106
            "\x1BNk" => 'º', // 186 in CP1252
151 106
            "\x1BN;" => '»', // 187 in CP1252
152 106
            "\x1BN<" => '¼', // 188 in CP1252
153 106
            "\x1BN=" => '½', // 189 in CP1252
154 106
            "\x1BN>" => '¾', // 190 in CP1252
155 106
            "\x1BN?" => '¿', // 191 in CP1252
156 106
            "\x1BNAA" => 'À', // 192 in CP1252
157 106
            "\x1BNBA" => 'Á', // 193 in CP1252
158 106
            "\x1BNCA" => 'Â', // 194 in CP1252
159 106
            "\x1BNDA" => 'Ã', // 195 in CP1252
160 106
            "\x1BNHA" => 'Ä', // 196 in CP1252
161 106
            "\x1BNJA" => 'Å', // 197 in CP1252
162 106
            "\x1BNa" => 'Æ', // 198 in CP1252
163 106
            "\x1BNKC" => 'Ç', // 199 in CP1252
164 106
            "\x1BNAE" => 'È', // 200 in CP1252
165 106
            "\x1BNBE" => 'É', // 201 in CP1252
166 106
            "\x1BNCE" => 'Ê', // 202 in CP1252
167 106
            "\x1BNHE" => 'Ë', // 203 in CP1252
168 106
            "\x1BNAI" => 'Ì', // 204 in CP1252
169 106
            "\x1BNBI" => 'Í', // 205 in CP1252
170 106
            "\x1BNCI" => 'Î', // 206 in CP1252
171 106
            "\x1BNHI" => 'Ï', // 207 in CP1252
172 106
            "\x1BNb" => 'Ð', // 208 in CP1252
173 106
            "\x1BNDN" => 'Ñ', // 209 in CP1252
174 106
            "\x1BNAO" => 'Ò', // 210 in CP1252
175 106
            "\x1BNBO" => 'Ó', // 211 in CP1252
176 106
            "\x1BNCO" => 'Ô', // 212 in CP1252
177 106
            "\x1BNDO" => 'Õ', // 213 in CP1252
178 106
            "\x1BNHO" => 'Ö', // 214 in CP1252
179 106
            "\x1B-7" => '×', // 215 in CP1252
180 106
            "\x1BNi" => 'Ø', // 216 in CP1252
181 106
            "\x1BNAU" => 'Ù', // 217 in CP1252
182 106
            "\x1BNBU" => 'Ú', // 218 in CP1252
183 106
            "\x1BNCU" => 'Û', // 219 in CP1252
184 106
            "\x1BNHU" => 'Ü', // 220 in CP1252
185 106
            "\x1B-=" => 'Ý', // 221 in CP1252
186 106
            "\x1BNl" => 'Þ', // 222 in CP1252
187 106
            "\x1BN{" => 'ß', // 223 in CP1252
188 106
            "\x1BNAa" => 'à', // 224 in CP1252
189 106
            "\x1BNBa" => 'á', // 225 in CP1252
190 106
            "\x1BNCa" => 'â', // 226 in CP1252
191 106
            "\x1BNDa" => 'ã', // 227 in CP1252
192 106
            "\x1BNHa" => 'ä', // 228 in CP1252
193 106
            "\x1BNJa" => 'å', // 229 in CP1252
194 106
            "\x1BNq" => 'æ', // 230 in CP1252
195 106
            "\x1BNKc" => 'ç', // 231 in CP1252
196 106
            "\x1BNAe" => 'è', // 232 in CP1252
197 106
            "\x1BNBe" => 'é', // 233 in CP1252
198 106
            "\x1BNCe" => 'ê', // 234 in CP1252
199 106
            "\x1BNHe" => 'ë', // 235 in CP1252
200 106
            "\x1BNAi" => 'ì', // 236 in CP1252
201 106
            "\x1BNBi" => 'í', // 237 in CP1252
202 106
            "\x1BNCi" => 'î', // 238 in CP1252
203 106
            "\x1BNHi" => 'ï', // 239 in CP1252
204 106
            "\x1BNs" => 'ð', // 240 in CP1252
205 106
            "\x1BNDn" => 'ñ', // 241 in CP1252
206 106
            "\x1BNAo" => 'ò', // 242 in CP1252
207 106
            "\x1BNBo" => 'ó', // 243 in CP1252
208 106
            "\x1BNCo" => 'ô', // 244 in CP1252
209 106
            "\x1BNDo" => 'õ', // 245 in CP1252
210 106
            "\x1BNHo" => 'ö', // 246 in CP1252
211 106
            "\x1B/7" => '÷', // 247 in CP1252
212 106
            "\x1BNy" => 'ø', // 248 in CP1252
213 106
            "\x1BNAu" => 'ù', // 249 in CP1252
214 106
            "\x1BNBu" => 'ú', // 250 in CP1252
215 106
            "\x1BNCu" => 'û', // 251 in CP1252
216 106
            "\x1BNHu" => 'ü', // 252 in CP1252
217 106
            "\x1B/=" => 'ý', // 253 in CP1252
218 106
            "\x1BN|" => 'þ', // 254 in CP1252
219 106
            "\x1BNHy" => 'ÿ', // 255 in CP1252
220 106
        ];
221
    }
222
223
    /**
224
     * Get whether iconv extension is available.
225
     */
226 218
    public static function getIsIconvEnabled(): bool
227
    {
228 218
        if (isset(self::$isIconvEnabled)) {
229 218
            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 617
    private static function buildCharacterSets(): void
255
    {
256 617
        if (empty(self::$controlCharacters)) {
257 106
            self::buildControlCharacters();
258
        }
259
260 617
        if (empty(self::$SYLKCharacters)) {
261 106
            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 490
    public static function controlCharacterOOXML2PHP(string $textValue): string
279
    {
280 490
        self::buildCharacterSets();
281
282 490
        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 264
    public static function controlCharacterPHP2OOXML(string $textValue): string
299
    {
300 264
        self::buildCharacterSets();
301
302 264
        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 9031
    public static function sanitizeUTF8(string $textValue): string
309
    {
310 9031
        $textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue);
311 9031
        $subst = mb_substitute_character(); // default is question mark
312 9031
        mb_substitute_character(65533); // Unicode substitution character
313
        // Phpstan does not think this can return false.
314 9031
        $returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
315 9031
        mb_substitute_character($subst);
316
317 9031
        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 925
    public static function formatNumber(float|int|string|null $numericValue): string
333
    {
334 925
        if (is_float($numericValue)) {
335 925
            return str_replace(',', '.', (string) $numericValue);
336
        }
337
338 91
        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 217
    public static function convertEncoding(string $textValue, string $to, string $from): string
399
    {
400 217
        if (self::getIsIconvEnabled()) {
401 217
            $result = iconv($from, $to . self::$iconvOptions, $textValue);
402 217
            if (false !== $result) {
403 217
                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 10455
    public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int
418
    {
419 10455
        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 10426
    public static function substring(string $textValue, int $offset, ?int $length = 0): string
442
    {
443 10426
        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 10241
    public static function strToUpper(string $textValue): string
452
    {
453 10241
        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 9995
    public static function strToLower(string $textValue): string
462
    {
463 9995
        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
478
    {
479 21
        return mb_strtolower($character, 'UTF-8') !== $character;
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: ^
488
        // and not before the end: $
489 21
        $split = preg_split('/(?<!^)(?!$)/u', $string);
490
491 21
        return ($split === false) ? [] : $split;
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
501
    {
502 21
        $characters = self::mbStrSplit($textValue);
503 21
        foreach ($characters as &$character) {
504 21
            if (self::mbIsUpper($character)) {
505 14
                $character = mb_strtolower($character, 'UTF-8');
506
            } else {
507 17
                $character = mb_strtoupper($character, 'UTF-8');
508
            }
509
        }
510
511 21
        return implode('', $characters);
512
    }
513
514
    private static function useAlt(string $altValue, string $default, bool $trimAlt): string
515
    {
516
        return ($trimAlt ? trim($altValue) : $altValue) ?: $default;
517
    }
518
519 127
    private static function getLocaleValue(string $key, string $altKey, string $default, bool $trimAlt = false): string
520
    {
521 127
        $localeconv = localeconv();
522 127
        $rslt = $localeconv[$key];
523
        // win-1252 implements Euro as 0x80 plus other symbols
524 127
        if (preg_match('//u', $rslt) !== 1) {
525
            $rslt = '';
526
        }
527
528 127
        return $rslt ?: self::useAlt($localeconv[$altKey], $default, $trimAlt);
529
    }
530
531
    /**
532
     * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
533
     * formatting information from locale.
534
     */
535 1091
    public static function getDecimalSeparator(): string
536
    {
537 1091
        if (!isset(self::$decimalSeparator)) {
538 116
            self::$decimalSeparator = self::getLocaleValue('decimal_point', 'mon_decimal_point', '.');
539
        }
540
541 1091
        return self::$decimalSeparator;
542
    }
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
     */
550 923
    public static function setDecimalSeparator(?string $separator): void
551
    {
552 923
        self::$decimalSeparator = $separator;
553
    }
554
555
    /**
556
     * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
557
     * formatting information from locale.
558
     */
559 1103
    public static function getThousandsSeparator(): string
560
    {
561 1103
        if (!isset(self::$thousandsSeparator)) {
562 117
            self::$thousandsSeparator = self::getLocaleValue('thousands_sep', 'mon_thousands_sep', ',');
563
        }
564
565 1103
        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
     *
572
     * @param ?string $separator Character for thousands separator
573
     */
574 923
    public static function setThousandsSeparator(?string $separator): void
575
    {
576 923
        self::$thousandsSeparator = $separator;
577
    }
578
579
    /**
580
     *    Get the currency code. If it has not yet been set explicitly, try to obtain the
581
     *        symbol information from locale.
582
     */
583 77
    public static function getCurrencyCode(bool $trimAlt = false): string
584
    {
585 77
        if (!isset(self::$currencyCode)) {
586 27
            self::$currencyCode = self::getLocaleValue('currency_symbol', 'int_curr_symbol', '$', $trimAlt);
587
        }
588
589 77
        return self::$currencyCode;
590
    }
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 922
    public static function setCurrencyCode(?string $currencyCode): void
599
    {
600 922
        self::$currencyCode = $currencyCode;
601
    }
602
603
    /**
604
     * Convert SYLK encoded string to UTF-8.
605
     *
606
     * @param string $textValue SYLK encoded string
607
     *
608
     * @return string UTF-8 encoded string
609
     */
610 11
    public static function SYLKtoUTF8(string $textValue): string
611
    {
612 11
        self::buildCharacterSets();
613
614
        // If there is no escape character in the string there is nothing to do
615 11
        if (!str_contains($textValue, '')) {
616 10
            return $textValue;
617
        }
618
619 3
        foreach (self::$SYLKCharacters as $k => $v) {
620 3
            $textValue = str_replace($k, $v, $textValue);
621
        }
622
623 3
        return $textValue;
624
    }
625
626
    /**
627
     * Retrieve any leading numeric part of a string, or return the full string if no leading numeric
628
     * (handles basic integer or float, but not exponent or non decimal).
629
     *
630
     * @return float|string string or only the leading numeric part of the string
631
     */
632 284
    public static function testStringAsNumeric(string $textValue): float|string
633
    {
634 284
        if (is_numeric($textValue)) {
635 281
            return $textValue;
636
        }
637 9
        $v = (float) $textValue;
638
639 9
        return (is_numeric(substr($textValue, 0, strlen((string) $v)))) ? $v : $textValue;
640
    }
641
642
    public static function strlenAllowNull(?string $string): int
643
    {
644
        return strlen("$string");
645
    }
646
}
647