Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 = null;
38
 
39
    /**
40
     * iconv options.
41
     */
42
    private static string $iconvOptions = '//IGNORE//TRANSLIT';
43
 
44
    /**
45
     * Build control characters array.
46
     */
47
    private static function buildControlCharacters(): void
48
    {
49
        for ($i = 0; $i <= 31; ++$i) {
50
            if ($i != 9 && $i != 10 && $i != 13) {
51
                $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
52
                $replace = chr($i);
53
                self::$controlCharacters[$find] = $replace;
54
            }
55
        }
56
    }
57
 
58
    /**
59
     * Build SYLK characters array.
60
     */
61
    private static function buildSYLKCharacters(): void
62
    {
63
        self::$SYLKCharacters = [
64
            "\x1B 0" => chr(0),
65
            "\x1B 1" => chr(1),
66
            "\x1B 2" => chr(2),
67
            "\x1B 3" => chr(3),
68
            "\x1B 4" => chr(4),
69
            "\x1B 5" => chr(5),
70
            "\x1B 6" => chr(6),
71
            "\x1B 7" => chr(7),
72
            "\x1B 8" => chr(8),
73
            "\x1B 9" => chr(9),
74
            "\x1B :" => chr(10),
75
            "\x1B ;" => chr(11),
76
            "\x1B <" => chr(12),
77
            "\x1B =" => chr(13),
78
            "\x1B >" => chr(14),
79
            "\x1B ?" => chr(15),
80
            "\x1B!0" => chr(16),
81
            "\x1B!1" => chr(17),
82
            "\x1B!2" => chr(18),
83
            "\x1B!3" => chr(19),
84
            "\x1B!4" => chr(20),
85
            "\x1B!5" => chr(21),
86
            "\x1B!6" => chr(22),
87
            "\x1B!7" => chr(23),
88
            "\x1B!8" => chr(24),
89
            "\x1B!9" => chr(25),
90
            "\x1B!:" => chr(26),
91
            "\x1B!;" => chr(27),
92
            "\x1B!<" => chr(28),
93
            "\x1B!=" => chr(29),
94
            "\x1B!>" => chr(30),
95
            "\x1B!?" => chr(31),
96
            "\x1B'?" => chr(127),
97
            "\x1B(0" => '€', // 128 in CP1252
98
            "\x1B(2" => '‚', // 130 in CP1252
99
            "\x1B(3" => 'Æ’', // 131 in CP1252
100
            "\x1B(4" => '„', // 132 in CP1252
101
            "\x1B(5" => '…', // 133 in CP1252
102
            "\x1B(6" => '†', // 134 in CP1252
103
            "\x1B(7" => '‡', // 135 in CP1252
104
            "\x1B(8" => 'ˆ', // 136 in CP1252
105
            "\x1B(9" => '‰', // 137 in CP1252
106
            "\x1B(:" => 'Š', // 138 in CP1252
107
            "\x1B(;" => '‹', // 139 in CP1252
108
            "\x1BNj" => 'Å’', // 140 in CP1252
109
            "\x1B(>" => 'Ž', // 142 in CP1252
110
            "\x1B)1" => '‘', // 145 in CP1252
111
            "\x1B)2" => '’', // 146 in CP1252
112
            "\x1B)3" => '“', // 147 in CP1252
113
            "\x1B)4" => '”', // 148 in CP1252
114
            "\x1B)5" => '•', // 149 in CP1252
115
            "\x1B)6" => '–', // 150 in CP1252
116
            "\x1B)7" => '—', // 151 in CP1252
117
            "\x1B)8" => '˜', // 152 in CP1252
118
            "\x1B)9" => 'â„¢', // 153 in CP1252
119
            "\x1B):" => 'š', // 154 in CP1252
120
            "\x1B);" => '›', // 155 in CP1252
121
            "\x1BNz" => 'Å“', // 156 in CP1252
122
            "\x1B)>" => 'ž', // 158 in CP1252
123
            "\x1B)?" => 'Ÿ', // 159 in CP1252
124
            "\x1B*0" => ' ', // 160 in CP1252
125
            "\x1BN!" => '¡', // 161 in CP1252
126
            "\x1BN\"" => '¢', // 162 in CP1252
127
            "\x1BN#" => '£', // 163 in CP1252
128
            "\x1BN(" => '¤', // 164 in CP1252
129
            "\x1BN%" => '¥', // 165 in CP1252
130
            "\x1B*6" => '¦', // 166 in CP1252
131
            "\x1BN'" => '§', // 167 in CP1252
132
            "\x1BNH " => '¨', // 168 in CP1252
133
            "\x1BNS" => '©', // 169 in CP1252
134
            "\x1BNc" => 'ª', // 170 in CP1252
135
            "\x1BN+" => '«', // 171 in CP1252
136
            "\x1B*<" => '¬', // 172 in CP1252
137
            "\x1B*=" => '­', // 173 in CP1252
138
            "\x1BNR" => '®', // 174 in CP1252
139
            "\x1B*?" => '¯', // 175 in CP1252
140
            "\x1BN0" => '°', // 176 in CP1252
141
            "\x1BN1" => '±', // 177 in CP1252
142
            "\x1BN2" => '²', // 178 in CP1252
143
            "\x1BN3" => '³', // 179 in CP1252
144
            "\x1BNB " => '´', // 180 in CP1252
145
            "\x1BN5" => 'µ', // 181 in CP1252
146
            "\x1BN6" => '¶', // 182 in CP1252
147
            "\x1BN7" => '·', // 183 in CP1252
148
            "\x1B+8" => '¸', // 184 in CP1252
149
            "\x1BNQ" => '¹', // 185 in CP1252
150
            "\x1BNk" => 'º', // 186 in CP1252
151
            "\x1BN;" => '»', // 187 in CP1252
152
            "\x1BN<" => '¼', // 188 in CP1252
153
            "\x1BN=" => '½', // 189 in CP1252
154
            "\x1BN>" => '¾', // 190 in CP1252
155
            "\x1BN?" => '¿', // 191 in CP1252
156
            "\x1BNAA" => 'À', // 192 in CP1252
157
            "\x1BNBA" => 'Á', // 193 in CP1252
158
            "\x1BNCA" => 'Â', // 194 in CP1252
159
            "\x1BNDA" => 'Ã', // 195 in CP1252
160
            "\x1BNHA" => 'Ä', // 196 in CP1252
161
            "\x1BNJA" => 'Å', // 197 in CP1252
162
            "\x1BNa" => 'Æ', // 198 in CP1252
163
            "\x1BNKC" => 'Ç', // 199 in CP1252
164
            "\x1BNAE" => 'È', // 200 in CP1252
165
            "\x1BNBE" => 'É', // 201 in CP1252
166
            "\x1BNCE" => 'Ê', // 202 in CP1252
167
            "\x1BNHE" => 'Ë', // 203 in CP1252
168
            "\x1BNAI" => 'Ì', // 204 in CP1252
169
            "\x1BNBI" => 'Í', // 205 in CP1252
170
            "\x1BNCI" => 'Î', // 206 in CP1252
171
            "\x1BNHI" => 'Ï', // 207 in CP1252
172
            "\x1BNb" => 'Ð', // 208 in CP1252
173
            "\x1BNDN" => 'Ñ', // 209 in CP1252
174
            "\x1BNAO" => 'Ò', // 210 in CP1252
175
            "\x1BNBO" => 'Ó', // 211 in CP1252
176
            "\x1BNCO" => 'Ô', // 212 in CP1252
177
            "\x1BNDO" => 'Õ', // 213 in CP1252
178
            "\x1BNHO" => 'Ö', // 214 in CP1252
179
            "\x1B-7" => '×', // 215 in CP1252
180
            "\x1BNi" => 'Ø', // 216 in CP1252
181
            "\x1BNAU" => 'Ù', // 217 in CP1252
182
            "\x1BNBU" => 'Ú', // 218 in CP1252
183
            "\x1BNCU" => 'Û', // 219 in CP1252
184
            "\x1BNHU" => 'Ü', // 220 in CP1252
185
            "\x1B-=" => 'Ý', // 221 in CP1252
186
            "\x1BNl" => 'Þ', // 222 in CP1252
187
            "\x1BN{" => 'ß', // 223 in CP1252
188
            "\x1BNAa" => 'à', // 224 in CP1252
189
            "\x1BNBa" => 'á', // 225 in CP1252
190
            "\x1BNCa" => 'â', // 226 in CP1252
191
            "\x1BNDa" => 'ã', // 227 in CP1252
192
            "\x1BNHa" => 'ä', // 228 in CP1252
193
            "\x1BNJa" => 'å', // 229 in CP1252
194
            "\x1BNq" => 'æ', // 230 in CP1252
195
            "\x1BNKc" => 'ç', // 231 in CP1252
196
            "\x1BNAe" => 'è', // 232 in CP1252
197
            "\x1BNBe" => 'é', // 233 in CP1252
198
            "\x1BNCe" => 'ê', // 234 in CP1252
199
            "\x1BNHe" => 'ë', // 235 in CP1252
200
            "\x1BNAi" => 'ì', // 236 in CP1252
201
            "\x1BNBi" => 'í', // 237 in CP1252
202
            "\x1BNCi" => 'î', // 238 in CP1252
203
            "\x1BNHi" => 'ï', // 239 in CP1252
204
            "\x1BNs" => 'ð', // 240 in CP1252
205
            "\x1BNDn" => 'ñ', // 241 in CP1252
206
            "\x1BNAo" => 'ò', // 242 in CP1252
207
            "\x1BNBo" => 'ó', // 243 in CP1252
208
            "\x1BNCo" => 'ô', // 244 in CP1252
209
            "\x1BNDo" => 'õ', // 245 in CP1252
210
            "\x1BNHo" => 'ö', // 246 in CP1252
211
            "\x1B/7" => '÷', // 247 in CP1252
212
            "\x1BNy" => 'ø', // 248 in CP1252
213
            "\x1BNAu" => 'ù', // 249 in CP1252
214
            "\x1BNBu" => 'ú', // 250 in CP1252
215
            "\x1BNCu" => 'û', // 251 in CP1252
216
            "\x1BNHu" => 'ü', // 252 in CP1252
217
            "\x1B/=" => 'ý', // 253 in CP1252
218
            "\x1BN|" => 'þ', // 254 in CP1252
219
            "\x1BNHy" => 'ÿ', // 255 in CP1252
220
        ];
221
    }
222
 
223
    /**
224
     * Get whether iconv extension is available.
225
     */
226
    public static function getIsIconvEnabled(): bool
227
    {
228
        if (isset(self::$isIconvEnabled)) {
229
            return self::$isIconvEnabled;
230
        }
231
 
232
        // Assume no problems with iconv
233
        self::$isIconvEnabled = true;
234
 
235
        // Fail if iconv doesn't exist
236
        if (!function_exists('iconv')) {
237
            self::$isIconvEnabled = false;
238
        } 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
        } 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
        if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) {
248
            self::$iconvOptions = '';
249
        }
250
 
251
        return self::$isIconvEnabled;
252
    }
253
 
254
    private static function buildCharacterSets(): void
255
    {
256
        if (empty(self::$controlCharacters)) {
257
            self::buildControlCharacters();
258
        }
259
 
260
        if (empty(self::$SYLKCharacters)) {
261
            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
    public static function controlCharacterOOXML2PHP(string $textValue): string
279
    {
280
        self::buildCharacterSets();
281
 
282
        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
    public static function controlCharacterPHP2OOXML(string $textValue): string
299
    {
300
        self::buildCharacterSets();
301
 
302
        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
    public static function sanitizeUTF8(string $textValue): string
309
    {
310
        $textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue);
311
        $subst = mb_substitute_character(); // default is question mark
312
        mb_substitute_character(65533); // Unicode substitution character
313
        // Phpstan does not think this can return false.
314
        $returnValue = mb_convert_encoding($textValue, 'UTF-8', 'UTF-8');
315
        mb_substitute_character($subst);
316
 
317
        return $returnValue;
318
    }
319
 
320
    /**
321
     * Check if a string contains UTF8 data.
322
     */
323
    public static function isUTF8(string $textValue): bool
324
    {
325
        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
    public static function formatNumber(float|int|string|null $numericValue): string
333
    {
334
        if (is_float($numericValue)) {
335
            return str_replace(',', '.', (string) $numericValue);
336
        }
337
 
338
        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
    public static function UTF8toBIFF8UnicodeShort(string $textValue, array $arrcRuns = []): string
352
    {
353
        // character count
354
        $ln = self::countCharacters($textValue, 'UTF-8');
355
        // option flags
356
        if (empty($arrcRuns)) {
357
            $data = pack('CC', $ln, 0x0001);
358
            // characters
359
            $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
360
        } else {
361
            $data = pack('vC', $ln, 0x09);
362
            $data .= pack('v', count($arrcRuns));
363
            // characters
364
            $data .= self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
365
            foreach ($arrcRuns as $cRun) {
366
                $data .= pack('v', $cRun['strlen']);
367
                $data .= pack('v', $cRun['fontidx']);
368
            }
369
        }
370
 
371
        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
    public static function UTF8toBIFF8UnicodeLong(string $textValue): string
384
    {
385
        // characters
386
        $chars = self::convertEncoding($textValue, 'UTF-16LE', 'UTF-8');
387
        $ln = (int) (strlen($chars) / 2);  // N.B. - strlen, not mb_strlen issue #642
388
 
389
        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
    public static function convertEncoding(string $textValue, string $to, string $from): string
399
    {
400
        if (self::getIsIconvEnabled()) {
401
            $result = iconv($from, $to . self::$iconvOptions, $textValue);
402
            if (false !== $result) {
403
                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
    public static function countCharacters(string $textValue, string $encoding = 'UTF-8'): int
418
    {
419
        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
    public static function countCharactersDbcs(string $textValue, string $encoding = 'UTF-8'): int
430
    {
431
        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
    public static function substring(string $textValue, int $offset, ?int $length = 0): string
442
    {
443
        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
    public static function strToUpper(string $textValue): string
452
    {
453
        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
    public static function strToLower(string $textValue): string
462
    {
463
        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
    public static function strToTitle(string $textValue): string
473
    {
474
        return mb_convert_case($textValue, MB_CASE_TITLE, 'UTF-8');
475
    }
476
 
477
    public static function mbIsUpper(string $character): bool
478
    {
479
        return mb_strtolower($character, 'UTF-8') !== $character;
480
    }
481
 
482
    /**
483
     * Splits a UTF-8 string into an array of individual characters.
484
     */
485
    public static function mbStrSplit(string $string): array
486
    {
487
        // Split at all position not after the start: ^
488
        // and not before the end: $
489
        $split = preg_split('/(?<!^)(?!$)/u', $string);
490
 
491
        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
    public static function strCaseReverse(string $textValue): string
501
    {
502
        $characters = self::mbStrSplit($textValue);
503
        foreach ($characters as &$character) {
504
            if (self::mbIsUpper($character)) {
505
                $character = mb_strtolower($character, 'UTF-8');
506
            } else {
507
                $character = mb_strtoupper($character, 'UTF-8');
508
            }
509
        }
510
 
511
        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
    private static function getLocaleValue(string $key, string $altKey, string $default, bool $trimAlt = false): string
520
    {
521
        $localeconv = localeconv();
522
        $rslt = $localeconv[$key];
523
        // win-1252 implements Euro as 0x80 plus other symbols
524
        if (preg_match('//u', $rslt) !== 1) {
525
            $rslt = '';
526
        }
527
 
528
        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
    public static function getDecimalSeparator(): string
536
    {
537
        if (!isset(self::$decimalSeparator)) {
538
            self::$decimalSeparator = self::getLocaleValue('decimal_point', 'mon_decimal_point', '.');
539
        }
540
 
541
        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
    public static function setDecimalSeparator(?string $separator): void
551
    {
552
        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
    public static function getThousandsSeparator(): string
560
    {
561
        if (!isset(self::$thousandsSeparator)) {
562
            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
     *
572
     * @param ?string $separator Character for thousands separator
573
     */
574
    public static function setThousandsSeparator(?string $separator): void
575
    {
576
        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
    public static function getCurrencyCode(bool $trimAlt = false): string
584
    {
585
        if (!isset(self::$currencyCode)) {
586
            self::$currencyCode = self::getLocaleValue('currency_symbol', 'int_curr_symbol', '$', $trimAlt);
587
        }
588
 
589
        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
    public static function setCurrencyCode(?string $currencyCode): void
599
    {
600
        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
    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
        foreach (self::$SYLKCharacters as $k => $v) {
620
            $textValue = str_replace($k, $v, $textValue);
621
        }
622
 
623
        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
    public static function testStringAsNumeric(string $textValue): float|string
633
    {
634
        if (is_numeric($textValue)) {
635
            return $textValue;
636
        }
637
        $v = (float) $textValue;
638
 
639
        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
}