Passed
Pull Request — master (#4418)
by Owen
12:11
created

StringHelper::useAlt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

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