Failed Conditions
Push — develop ( 699da0...3b0c68 )
by Adrien
26:49
created

StringHelper::sanitizeUTF8()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
7
class StringHelper
8
{
9
    /**    Constants                */
10
    /**    Regular Expressions        */
11
    //    Fraction
12
    const STRING_REGEXP_FRACTION = '(-?)(\d+)\s+(\d+\/\d+)';
13
14
    /**
15
     * Control characters array.
16
     *
17
     * @var string[]
18
     */
19
    private static $controlCharacters = [];
20
21
    /**
22
     * SYLK Characters array.
23
     *
24
     * @var array
25
     */
26
    private static $SYLKCharacters = [];
27
28
    /**
29
     * Decimal separator.
30
     *
31
     * @var string
32
     */
33
    private static $decimalSeparator;
34
35
    /**
36
     * Thousands separator.
37
     *
38
     * @var string
39
     */
40
    private static $thousandsSeparator;
41
42
    /**
43
     * Currency code.
44
     *
45
     * @var string
46
     */
47
    private static $currencyCode;
48
49
    /**
50
     * Is iconv extension avalable?
51
     *
52
     * @var bool
53
     */
54
    private static $isIconvEnabled;
55
56
    /**
57
     * iconv options.
58
     *
59
     * @var string
60
     */
61
    private static $iconvOptions = '//IGNORE//TRANSLIT';
62
63
    /**
64
     * Build control characters array.
65
     */
66 56
    private static function buildControlCharacters()
67
    {
68 56
        for ($i = 0; $i <= 31; ++$i) {
69 56
            if ($i != 9 && $i != 10 && $i != 13) {
70 56
                $find = '_x' . sprintf('%04s', strtoupper(dechex($i))) . '_';
71 56
                $replace = chr($i);
72 56
                self::$controlCharacters[$find] = $replace;
73
            }
74
        }
75 56
    }
76
77
    /**
78
     * Build SYLK characters array.
79
     */
80 56
    private static function buildSYLKCharacters()
81
    {
82
        self::$SYLKCharacters = [
83 56
            "\x1B 0" => chr(0),
84 56
            "\x1B 1" => chr(1),
85 56
            "\x1B 2" => chr(2),
86 56
            "\x1B 3" => chr(3),
87 56
            "\x1B 4" => chr(4),
88 56
            "\x1B 5" => chr(5),
89 56
            "\x1B 6" => chr(6),
90 56
            "\x1B 7" => chr(7),
91 56
            "\x1B 8" => chr(8),
92 56
            "\x1B 9" => chr(9),
93 56
            "\x1B :" => chr(10),
94 56
            "\x1B ;" => chr(11),
95 56
            "\x1B <" => chr(12),
96 56
            "\x1B =" => chr(13),
97 56
            "\x1B >" => chr(14),
98 56
            "\x1B ?" => chr(15),
99 56
            "\x1B!0" => chr(16),
100 56
            "\x1B!1" => chr(17),
101 56
            "\x1B!2" => chr(18),
102 56
            "\x1B!3" => chr(19),
103 56
            "\x1B!4" => chr(20),
104 56
            "\x1B!5" => chr(21),
105 56
            "\x1B!6" => chr(22),
106 56
            "\x1B!7" => chr(23),
107 56
            "\x1B!8" => chr(24),
108 56
            "\x1B!9" => chr(25),
109 56
            "\x1B!:" => chr(26),
110 56
            "\x1B!;" => chr(27),
111 56
            "\x1B!<" => chr(28),
112 56
            "\x1B!=" => chr(29),
113 56
            "\x1B!>" => chr(30),
114 56
            "\x1B!?" => chr(31),
115 56
            "\x1B'?" => chr(127),
116 56
            "\x1B(0" => '€', // 128 in CP1252
117 56
            "\x1B(2" => '‚', // 130 in CP1252
118 56
            "\x1B(3" => 'ƒ', // 131 in CP1252
119 56
            "\x1B(4" => '„', // 132 in CP1252
120 56
            "\x1B(5" => '…', // 133 in CP1252
121 56
            "\x1B(6" => '†', // 134 in CP1252
122 56
            "\x1B(7" => '‡', // 135 in CP1252
123 56
            "\x1B(8" => 'ˆ', // 136 in CP1252
124 56
            "\x1B(9" => '‰', // 137 in CP1252
125 56
            "\x1B(:" => 'Š', // 138 in CP1252
126 56
            "\x1B(;" => '‹', // 139 in CP1252
127 56
            "\x1BNj" => 'Œ', // 140 in CP1252
128 56
            "\x1B(>" => 'Ž', // 142 in CP1252
129 56
            "\x1B)1" => '‘', // 145 in CP1252
130 56
            "\x1B)2" => '’', // 146 in CP1252
131 56
            "\x1B)3" => '“', // 147 in CP1252
132 56
            "\x1B)4" => '”', // 148 in CP1252
133 56
            "\x1B)5" => '•', // 149 in CP1252
134 56
            "\x1B)6" => '–', // 150 in CP1252
135 56
            "\x1B)7" => '—', // 151 in CP1252
136 56
            "\x1B)8" => '˜', // 152 in CP1252
137 56
            "\x1B)9" => '™', // 153 in CP1252
138 56
            "\x1B):" => 'š', // 154 in CP1252
139 56
            "\x1B);" => '›', // 155 in CP1252
140 56
            "\x1BNz" => 'œ', // 156 in CP1252
141 56
            "\x1B)>" => 'ž', // 158 in CP1252
142 56
            "\x1B)?" => 'Ÿ', // 159 in CP1252
143 56
            "\x1B*0" => ' ', // 160 in CP1252
144 56
            "\x1BN!" => '¡', // 161 in CP1252
145 56
            "\x1BN\"" => '¢', // 162 in CP1252
146 56
            "\x1BN#" => '£', // 163 in CP1252
147 56
            "\x1BN(" => '¤', // 164 in CP1252
148 56
            "\x1BN%" => '¥', // 165 in CP1252
149 56
            "\x1B*6" => '¦', // 166 in CP1252
150 56
            "\x1BN'" => '§', // 167 in CP1252
151 56
            "\x1BNH " => '¨', // 168 in CP1252
152 56
            "\x1BNS" => '©', // 169 in CP1252
153 56
            "\x1BNc" => 'ª', // 170 in CP1252
154 56
            "\x1BN+" => '«', // 171 in CP1252
155 56
            "\x1B*<" => '¬', // 172 in CP1252
156 56
            "\x1B*=" => '­', // 173 in CP1252
157 56
            "\x1BNR" => '®', // 174 in CP1252
158 56
            "\x1B*?" => '¯', // 175 in CP1252
159 56
            "\x1BN0" => '°', // 176 in CP1252
160 56
            "\x1BN1" => '±', // 177 in CP1252
161 56
            "\x1BN2" => '²', // 178 in CP1252
162 56
            "\x1BN3" => '³', // 179 in CP1252
163 56
            "\x1BNB " => '´', // 180 in CP1252
164 56
            "\x1BN5" => 'µ', // 181 in CP1252
165 56
            "\x1BN6" => '¶', // 182 in CP1252
166 56
            "\x1BN7" => '·', // 183 in CP1252
167 56
            "\x1B+8" => '¸', // 184 in CP1252
168 56
            "\x1BNQ" => '¹', // 185 in CP1252
169 56
            "\x1BNk" => 'º', // 186 in CP1252
170 56
            "\x1BN;" => '»', // 187 in CP1252
171 56
            "\x1BN<" => '¼', // 188 in CP1252
172 56
            "\x1BN=" => '½', // 189 in CP1252
173 56
            "\x1BN>" => '¾', // 190 in CP1252
174 56
            "\x1BN?" => '¿', // 191 in CP1252
175 56
            "\x1BNAA" => 'À', // 192 in CP1252
176 56
            "\x1BNBA" => 'Á', // 193 in CP1252
177 56
            "\x1BNCA" => 'Â', // 194 in CP1252
178 56
            "\x1BNDA" => 'Ã', // 195 in CP1252
179 56
            "\x1BNHA" => 'Ä', // 196 in CP1252
180 56
            "\x1BNJA" => 'Å', // 197 in CP1252
181 56
            "\x1BNa" => 'Æ', // 198 in CP1252
182 56
            "\x1BNKC" => 'Ç', // 199 in CP1252
183 56
            "\x1BNAE" => 'È', // 200 in CP1252
184 56
            "\x1BNBE" => 'É', // 201 in CP1252
185 56
            "\x1BNCE" => 'Ê', // 202 in CP1252
186 56
            "\x1BNHE" => 'Ë', // 203 in CP1252
187 56
            "\x1BNAI" => 'Ì', // 204 in CP1252
188 56
            "\x1BNBI" => 'Í', // 205 in CP1252
189 56
            "\x1BNCI" => 'Î', // 206 in CP1252
190 56
            "\x1BNHI" => 'Ï', // 207 in CP1252
191 56
            "\x1BNb" => 'Ð', // 208 in CP1252
192 56
            "\x1BNDN" => 'Ñ', // 209 in CP1252
193 56
            "\x1BNAO" => 'Ò', // 210 in CP1252
194 56
            "\x1BNBO" => 'Ó', // 211 in CP1252
195 56
            "\x1BNCO" => 'Ô', // 212 in CP1252
196 56
            "\x1BNDO" => 'Õ', // 213 in CP1252
197 56
            "\x1BNHO" => 'Ö', // 214 in CP1252
198 56
            "\x1B-7" => '×', // 215 in CP1252
199 56
            "\x1BNi" => 'Ø', // 216 in CP1252
200 56
            "\x1BNAU" => 'Ù', // 217 in CP1252
201 56
            "\x1BNBU" => 'Ú', // 218 in CP1252
202 56
            "\x1BNCU" => 'Û', // 219 in CP1252
203 56
            "\x1BNHU" => 'Ü', // 220 in CP1252
204 56
            "\x1B-=" => 'Ý', // 221 in CP1252
205 56
            "\x1BNl" => 'Þ', // 222 in CP1252
206 56
            "\x1BN{" => 'ß', // 223 in CP1252
207 56
            "\x1BNAa" => 'à', // 224 in CP1252
208 56
            "\x1BNBa" => 'á', // 225 in CP1252
209 56
            "\x1BNCa" => 'â', // 226 in CP1252
210 56
            "\x1BNDa" => 'ã', // 227 in CP1252
211 56
            "\x1BNHa" => 'ä', // 228 in CP1252
212 56
            "\x1BNJa" => 'å', // 229 in CP1252
213 56
            "\x1BNq" => 'æ', // 230 in CP1252
214 56
            "\x1BNKc" => 'ç', // 231 in CP1252
215 56
            "\x1BNAe" => 'è', // 232 in CP1252
216 56
            "\x1BNBe" => 'é', // 233 in CP1252
217 56
            "\x1BNCe" => 'ê', // 234 in CP1252
218 56
            "\x1BNHe" => 'ë', // 235 in CP1252
219 56
            "\x1BNAi" => 'ì', // 236 in CP1252
220 56
            "\x1BNBi" => 'í', // 237 in CP1252
221 56
            "\x1BNCi" => 'î', // 238 in CP1252
222 56
            "\x1BNHi" => 'ï', // 239 in CP1252
223 56
            "\x1BNs" => 'ð', // 240 in CP1252
224 56
            "\x1BNDn" => 'ñ', // 241 in CP1252
225 56
            "\x1BNAo" => 'ò', // 242 in CP1252
226 56
            "\x1BNBo" => 'ó', // 243 in CP1252
227 56
            "\x1BNCo" => 'ô', // 244 in CP1252
228 56
            "\x1BNDo" => 'õ', // 245 in CP1252
229 56
            "\x1BNHo" => 'ö', // 246 in CP1252
230 56
            "\x1B/7" => '÷', // 247 in CP1252
231 56
            "\x1BNy" => 'ø', // 248 in CP1252
232 56
            "\x1BNAu" => 'ù', // 249 in CP1252
233 56
            "\x1BNBu" => 'ú', // 250 in CP1252
234 56
            "\x1BNCu" => 'û', // 251 in CP1252
235 56
            "\x1BNHu" => 'ü', // 252 in CP1252
236 56
            "\x1B/=" => 'ý', // 253 in CP1252
237 56
            "\x1BN|" => 'þ', // 254 in CP1252
238 56
            "\x1BNHy" => 'ÿ', // 255 in CP1252
239
        ];
240 56
    }
241
242
    /**
243
     * Get whether iconv extension is available.
244
     *
245
     * @return bool
246
     */
247 156
    public static function getIsIconvEnabled()
248
    {
249 156
        if (isset(self::$isIconvEnabled)) {
250 156
            return self::$isIconvEnabled;
251
        }
252
253
        // Assume no problems with iconv
254 96
        self::$isIconvEnabled = true;
255
256
        // Fail if iconv doesn't exist
257 96
        if (!function_exists('iconv')) {
258
            self::$isIconvEnabled = false;
259 96
        } elseif (!@iconv('UTF-8', 'UTF-16LE', 'x')) {
260
            // Sometimes iconv is not working, and e.g. iconv('UTF-8', 'UTF-16LE', 'x') just returns false,
261
            self::$isIconvEnabled = false;
262 96
        } elseif (defined('PHP_OS') && @stristr(PHP_OS, 'AIX') && defined('ICONV_IMPL') && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && defined('ICONV_VERSION') && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
263
            // CUSTOM: IBM AIX iconv() does not work
264
            self::$isIconvEnabled = false;
265
        }
266
267
        // Deactivate iconv default options if they fail (as seen on IMB i)
268 96
        if (self::$isIconvEnabled && !@iconv('UTF-8', 'UTF-16LE' . self::$iconvOptions, 'x')) {
269
            self::$iconvOptions = '';
270
        }
271
272 96
        return self::$isIconvEnabled;
273
    }
274
275 66
    private static function buildCharacterSets()
276
    {
277 66
        if (empty(self::$controlCharacters)) {
278 56
            self::buildControlCharacters();
279
        }
280
281 66
        if (empty(self::$SYLKCharacters)) {
282 56
            self::buildSYLKCharacters();
283
        }
284 66
    }
285
286
    /**
287
     * Convert from OpenXML escaped control character to PHP control character.
288
     *
289
     * Excel 2007 team:
290
     * ----------------
291
     * That's correct, control characters are stored directly in the shared-strings table.
292
     * We do encode characters that cannot be represented in XML using the following escape sequence:
293
     * _xHHHH_ where H represents a hexadecimal character in the character's value...
294
     * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
295
     * element or in the shared string <t> element.
296
     *
297
     * @param string $value Value to unescape
298
     *
299
     * @return string
300
     */
301 20
    public static function controlCharacterOOXML2PHP($value)
302
    {
303 20
        self::buildCharacterSets();
304
305 20
        return str_replace(array_keys(self::$controlCharacters), array_values(self::$controlCharacters), $value);
306
    }
307
308
    /**
309
     * Convert from PHP control character to OpenXML escaped control character.
310
     *
311
     * Excel 2007 team:
312
     * ----------------
313
     * That's correct, control characters are stored directly in the shared-strings table.
314
     * We do encode characters that cannot be represented in XML using the following escape sequence:
315
     * _xHHHH_ where H represents a hexadecimal character in the character's value...
316
     * So you could end up with something like _x0008_ in a string (either in a cell value (<v>)
317
     * element or in the shared string <t> element.
318
     *
319
     * @param string $value Value to escape
320
     *
321
     * @return string
322
     */
323 61
    public static function controlCharacterPHP2OOXML($value)
324
    {
325 61
        self::buildCharacterSets();
326
327 61
        return str_replace(array_values(self::$controlCharacters), array_keys(self::$controlCharacters), $value);
328
    }
329
330
    /**
331
     * Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
332
     *
333
     * @param string $value
334
     *
335
     * @return string
336
     */
337 125
    public static function sanitizeUTF8($value)
338
    {
339 125
        if (self::getIsIconvEnabled()) {
340 125
            $value = @iconv('UTF-8', 'UTF-8', $value);
341
342 125
            return $value;
343
        }
344
345
        $value = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
346
347
        return $value;
348
    }
349
350
    /**
351
     * Check if a string contains UTF8 data.
352
     *
353
     * @param string $value
354
     *
355
     * @return bool
356
     */
357
    public static function isUTF8($value)
358
    {
359
        return $value === '' || preg_match('/^./su', $value) === 1;
360
    }
361
362
    /**
363
     * Formats a numeric value as a string for output in various output writers forcing
364
     * point as decimal separator in case locale is other than English.
365
     *
366
     * @param mixed $value
367
     *
368
     * @return string
369
     */
370 94
    public static function formatNumber($value)
371
    {
372 94
        if (is_float($value)) {
373 94
            return str_replace(',', '.', $value);
374
        }
375
376 83
        return (string) $value;
377
    }
378
379
    /**
380
     * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length)
381
     * Writes the string using uncompressed notation, no rich text, no Asian phonetics
382
     * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
383
     * although this will give wrong results for non-ASCII strings
384
     * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
385
     *
386
     * @param string $value UTF-8 encoded string
387
     * @param mixed[] $arrcRuns Details of rich text runs in $value
388
     *
389
     * @return string
390
     */
391 43
    public static function UTF8toBIFF8UnicodeShort($value, $arrcRuns = [])
392
    {
393
        // character count
394 43
        $ln = self::countCharacters($value, 'UTF-8');
395
        // option flags
396 43
        if (empty($arrcRuns)) {
397 43
            $data = pack('CC', $ln, 0x0001);
398
            // characters
399 43
            $data .= self::convertEncoding($value, 'UTF-16LE', 'UTF-8');
400
        } else {
401 9
            $data = pack('vC', $ln, 0x09);
402 9
            $data .= pack('v', count($arrcRuns));
403
            // characters
404 9
            $data .= self::convertEncoding($value, 'UTF-16LE', 'UTF-8');
405 9
            foreach ($arrcRuns as $cRun) {
406 9
                $data .= pack('v', $cRun['strlen']);
407 9
                $data .= pack('v', $cRun['fontidx']);
408
            }
409
        }
410
411 43
        return $data;
412
    }
413
414
    /**
415
     * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length)
416
     * Writes the string using uncompressed notation, no rich text, no Asian phonetics
417
     * If mbstring extension is not available, ASCII is assumed, and compressed notation is used
418
     * although this will give wrong results for non-ASCII strings
419
     * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
420
     *
421
     * @param string $value UTF-8 encoded string
422
     *
423
     * @return string
424
     */
425 43
    public static function UTF8toBIFF8UnicodeLong($value)
426
    {
427
        // character count
428 43
        $ln = self::countCharacters($value, 'UTF-8');
429
430
        // characters
431 43
        $chars = self::convertEncoding($value, 'UTF-16LE', 'UTF-8');
432
433 43
        $data = pack('vC', $ln, 0x0001) . $chars;
434
435 43
        return $data;
436
    }
437
438
    /**
439
     * Convert string from one encoding to another.
440
     *
441
     * @param string $value
442
     * @param string $to Encoding to convert to, e.g. 'UTF-8'
443
     * @param string $from Encoding to convert from, e.g. 'UTF-16LE'
444
     *
445
     * @return string
446
     */
447 61
    public static function convertEncoding($value, $to, $from)
448
    {
449 61
        if (self::getIsIconvEnabled()) {
450 61
            $result = iconv($from, $to . self::$iconvOptions, $value);
451 61
            if (false !== $result) {
452 61
                return $result;
453
            }
454
        }
455
456
        return mb_convert_encoding($value, $to, $from);
457
    }
458
459
    /**
460
     * Get character count.
461
     *
462
     * @param string $value
463
     * @param string $enc Encoding
464
     *
465
     * @return int Character count
466
     */
467 238
    public static function countCharacters($value, $enc = 'UTF-8')
468
    {
469 238
        return mb_strlen($value, $enc);
470
    }
471
472
    /**
473
     * Get a substring of a UTF-8 encoded string.
474
     *
475
     * @param string $pValue UTF-8 encoded string
476
     * @param int $pStart Start offset
477
     * @param int $pLength Maximum number of characters in substring
478
     *
479
     * @return string
480
     */
481 214
    public static function substring($pValue, $pStart, $pLength = 0)
482
    {
483 214
        return mb_substr($pValue, $pStart, $pLength, 'UTF-8');
484
    }
485
486
    /**
487
     * Convert a UTF-8 encoded string to upper case.
488
     *
489
     * @param string $pValue UTF-8 encoded string
490
     *
491
     * @return string
492
     */
493 4
    public static function strToUpper($pValue)
494
    {
495 4
        return mb_convert_case($pValue, MB_CASE_UPPER, 'UTF-8');
496
    }
497
498
    /**
499
     * Convert a UTF-8 encoded string to lower case.
500
     *
501
     * @param string $pValue UTF-8 encoded string
502
     *
503
     * @return string
504
     */
505 4
    public static function strToLower($pValue)
506
    {
507 4
        return mb_convert_case($pValue, MB_CASE_LOWER, 'UTF-8');
508
    }
509
510
    /**
511
     * Convert a UTF-8 encoded string to title/proper case
512
     * (uppercase every first character in each word, lower case all other characters).
513
     *
514
     * @param string $pValue UTF-8 encoded string
515
     *
516
     * @return string
517
     */
518 3
    public static function strToTitle($pValue)
519
    {
520 3
        return mb_convert_case($pValue, MB_CASE_TITLE, 'UTF-8');
521
    }
522
523 21
    public static function mbIsUpper($char)
524
    {
525 21
        return mb_strtolower($char, 'UTF-8') != $char;
526
    }
527
528 21
    public static function mbStrSplit($string)
529
    {
530
        // Split at all position not after the start: ^
531
        // and not before the end: $
532 21
        return preg_split('/(?<!^)(?!$)/u', $string);
533
    }
534
535
    /**
536
     * Reverse the case of a string, so that all uppercase characters become lowercase
537
     * and all lowercase characters become uppercase.
538
     *
539
     * @param string $pValue UTF-8 encoded string
540
     *
541
     * @return string
542
     */
543 21
    public static function strCaseReverse($pValue)
544
    {
545 21
        $characters = self::mbStrSplit($pValue);
546 21
        foreach ($characters as &$character) {
547 21
            if (self::mbIsUpper($character)) {
548 14
                $character = mb_strtolower($character, 'UTF-8');
549
            } else {
550 21
                $character = mb_strtoupper($character, 'UTF-8');
551
            }
552
        }
553
554 21
        return implode('', $characters);
1 ignored issue
show
Bug introduced by
It seems like $characters can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

554
        return implode('', /** @scrutinizer ignore-type */ $characters);
Loading history...
555
    }
556
557
    /**
558
     * Identify whether a string contains a fractional numeric value,
559
     * and convert it to a numeric if it is.
560
     *
561
     * @param string &$operand string value to test
562
     *
563
     * @return bool
564
     */
565 2
    public static function convertToNumberIfFraction(&$operand)
566
    {
567 2
        if (preg_match('/^' . self::STRING_REGEXP_FRACTION . '$/i', $operand, $match)) {
568
            $sign = ($match[1] == '-') ? '-' : '+';
569
            $fractionFormula = '=' . $sign . $match[2] . $sign . $match[3];
570
            $operand = Calculation::getInstance()->_calculateFormulaValue($fractionFormula);
571
572
            return true;
573
        }
574
575 2
        return false;
576
    }
577
578
    //    function convertToNumberIfFraction()
579
580
    /**
581
     * Get the decimal separator. If it has not yet been set explicitly, try to obtain number
582
     * formatting information from locale.
583
     *
584
     * @return string
585
     */
586 45
    public static function getDecimalSeparator()
587
    {
588 45
        if (!isset(self::$decimalSeparator)) {
589 10
            $localeconv = localeconv();
590 10
            self::$decimalSeparator = ($localeconv['decimal_point'] != '')
591 10
                ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
592
593 10
            if (self::$decimalSeparator == '') {
594
                // Default to .
595
                self::$decimalSeparator = '.';
596
            }
597
        }
598
599 45
        return self::$decimalSeparator;
600
    }
601
602
    /**
603
     * Set the decimal separator. Only used by NumberFormat::toFormattedString()
604
     * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
605
     *
606
     * @param string $pValue Character for decimal separator
607
     */
608 245
    public static function setDecimalSeparator($pValue)
609
    {
610 245
        self::$decimalSeparator = $pValue;
611 245
    }
612
613
    /**
614
     * Get the thousands separator. If it has not yet been set explicitly, try to obtain number
615
     * formatting information from locale.
616
     *
617
     * @return string
618
     */
619 53
    public static function getThousandsSeparator()
620
    {
621 53
        if (!isset(self::$thousandsSeparator)) {
622 10
            $localeconv = localeconv();
623 10
            self::$thousandsSeparator = ($localeconv['thousands_sep'] != '')
624 10
                ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep'];
625
626 10
            if (self::$thousandsSeparator == '') {
627
                // Default to .
628
                self::$thousandsSeparator = ',';
629
            }
630
        }
631
632 53
        return self::$thousandsSeparator;
633
    }
634
635
    /**
636
     * Set the thousands separator. Only used by NumberFormat::toFormattedString()
637
     * to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
638
     *
639
     * @param string $pValue Character for thousands separator
640
     */
641 245
    public static function setThousandsSeparator($pValue)
642
    {
643 245
        self::$thousandsSeparator = $pValue;
644 245
    }
645
646
    /**
647
     *    Get the currency code. If it has not yet been set explicitly, try to obtain the
648
     *        symbol information from locale.
649
     *
650
     * @return string
651
     */
652 19
    public static function getCurrencyCode()
653
    {
654 19
        if (!empty(self::$currencyCode)) {
655 18
            return self::$currencyCode;
656
        }
657 3
        self::$currencyCode = '$';
658 3
        $localeconv = localeconv();
659 3
        if (!empty($localeconv['currency_symbol'])) {
660 3
            self::$currencyCode = $localeconv['currency_symbol'];
661
662 3
            return self::$currencyCode;
663
        }
664
        if (!empty($localeconv['int_curr_symbol'])) {
665
            self::$currencyCode = $localeconv['int_curr_symbol'];
666
667
            return self::$currencyCode;
668
        }
669
670
        return self::$currencyCode;
671
    }
672
673
    /**
674
     * Set the currency code. Only used by NumberFormat::toFormattedString()
675
     *        to format output by \PhpOffice\PhpSpreadsheet\Writer\Html and \PhpOffice\PhpSpreadsheet\Writer\Pdf.
676
     *
677
     * @param string $pValue Character for currency code
678
     */
679 197
    public static function setCurrencyCode($pValue)
680
    {
681 197
        self::$currencyCode = $pValue;
682 197
    }
683
684
    /**
685
     * Convert SYLK encoded string to UTF-8.
686
     *
687
     * @param string $pValue
688
     *
689
     * @return string UTF-8 encoded string
690
     */
691 2
    public static function SYLKtoUTF8($pValue)
692
    {
693 2
        self::buildCharacterSets();
694
695
        // If there is no escape character in the string there is nothing to do
696 2
        if (strpos($pValue, '') === false) {
697 1
            return $pValue;
698
        }
699
700 1
        foreach (self::$SYLKCharacters as $k => $v) {
701 1
            $pValue = str_replace($k, $v, $pValue);
702
        }
703
704 1
        return $pValue;
705
    }
706
707
    /**
708
     * Retrieve any leading numeric part of a string, or return the full string if no leading numeric
709
     * (handles basic integer or float, but not exponent or non decimal).
710
     *
711
     * @param string $value
712
     *
713
     * @return mixed string or only the leading numeric part of the string
714
     */
715 84
    public static function testStringAsNumeric($value)
716
    {
717 84
        if (is_numeric($value)) {
718 84
            return $value;
719
        }
720 3
        $v = (float) $value;
721
722 3
        return (is_numeric(substr($value, 0, strlen($v)))) ? $v : $value;
723
    }
724
}
725