@@ -329,12 +329,8 @@ public static function controlCharacterPHP2OOXML($textValue)
329
329
330
330
/**
331
331
* Try to sanitize UTF8, stripping invalid byte sequences. Not perfect. Does not surrogate characters.
332
- *
333
- * @param string $textValue
334
- *
335
- * @return string
336
332
*/
337
- public static function sanitizeUTF8 ($ textValue )
333
+ public static function sanitizeUTF8 (string $ textValue ): string
338
334
{
339
335
if (self ::getIsIconvEnabled ()) {
340
336
$ textValue = @iconv ('UTF-8 ' , 'UTF-8 ' , $ textValue );
@@ -349,12 +345,8 @@ public static function sanitizeUTF8($textValue)
349
345
350
346
/**
351
347
* Check if a string contains UTF8 data.
352
- *
353
- * @param string $textValue
354
- *
355
- * @return bool
356
348
*/
357
- public static function isUTF8 ($ textValue )
349
+ public static function isUTF8 (string $ textValue ): bool
358
350
{
359
351
return $ textValue === '' || preg_match ('/^./su ' , $ textValue ) === 1 ;
360
352
}
@@ -364,10 +356,8 @@ public static function isUTF8($textValue)
364
356
* point as decimal separator in case locale is other than English.
365
357
*
366
358
* @param mixed $numericValue
367
- *
368
- * @return string
369
359
*/
370
- public static function formatNumber ($ numericValue )
360
+ public static function formatNumber ($ numericValue ): string
371
361
{
372
362
if (is_float ($ numericValue )) {
373
363
return str_replace (', ' , '. ' , $ numericValue );
@@ -385,10 +375,8 @@ public static function formatNumber($numericValue)
385
375
*
386
376
* @param string $textValue UTF-8 encoded string
387
377
* @param mixed[] $arrcRuns Details of rich text runs in $value
388
- *
389
- * @return string
390
378
*/
391
- public static function UTF8toBIFF8UnicodeShort ($ textValue , $ arrcRuns = [])
379
+ public static function UTF8toBIFF8UnicodeShort (string $ textValue , array $ arrcRuns = []): string
392
380
{
393
381
// character count
394
382
$ ln = self ::countCharacters ($ textValue , 'UTF-8 ' );
@@ -419,10 +407,8 @@ public static function UTF8toBIFF8UnicodeShort($textValue, $arrcRuns = [])
419
407
* see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3.
420
408
*
421
409
* @param string $textValue UTF-8 encoded string
422
- *
423
- * @return string
424
410
*/
425
- public static function UTF8toBIFF8UnicodeLong ($ textValue )
411
+ public static function UTF8toBIFF8UnicodeLong (string $ textValue ): string
426
412
{
427
413
// character count
428
414
$ ln = self ::countCharacters ($ textValue , 'UTF-8 ' );
@@ -436,13 +422,10 @@ public static function UTF8toBIFF8UnicodeLong($textValue)
436
422
/**
437
423
* Convert string from one encoding to another.
438
424
*
439
- * @param string $textValue
440
425
* @param string $to Encoding to convert to, e.g. 'UTF-8'
441
426
* @param string $from Encoding to convert from, e.g. 'UTF-16LE'
442
- *
443
- * @return string
444
427
*/
445
- public static function convertEncoding ($ textValue , $ to , $ from )
428
+ public static function convertEncoding (string $ textValue , string $ to , string $ from ): string
446
429
{
447
430
if (self ::getIsIconvEnabled ()) {
448
431
$ result = iconv ($ from , $ to . self ::$ iconvOptions , $ textValue );
@@ -457,88 +440,82 @@ public static function convertEncoding($textValue, $to, $from)
457
440
/**
458
441
* Get character count.
459
442
*
460
- * @param string $textValue
461
443
* @param string $encoding Encoding
462
444
*
463
445
* @return int Character count
464
446
*/
465
- public static function countCharacters ($ textValue , $ encoding = 'UTF-8 ' )
447
+ public static function countCharacters (string $ textValue , string $ encoding = 'UTF-8 ' ): int
466
448
{
467
- return mb_strlen ($ textValue ?? '' , $ encoding );
449
+ return mb_strlen ($ textValue , $ encoding );
468
450
}
469
451
470
452
/**
471
453
* Get a substring of a UTF-8 encoded string.
472
454
*
473
- * @param null| string $textValue UTF-8 encoded string
455
+ * @param string $textValue UTF-8 encoded string
474
456
* @param int $offset Start offset
475
457
* @param int $length Maximum number of characters in substring
476
- *
477
- * @return string
478
458
*/
479
- public static function substring ($ textValue , $ offset , $ length = 0 )
459
+ public static function substring (string $ textValue , int $ offset , int $ length = 0 ): string
480
460
{
481
- return mb_substr ($ textValue ?? '' , $ offset , $ length , 'UTF-8 ' );
461
+ return mb_substr ($ textValue , $ offset , $ length , 'UTF-8 ' );
482
462
}
483
463
484
464
/**
485
465
* Convert a UTF-8 encoded string to upper case.
486
466
*
487
467
* @param string $textValue UTF-8 encoded string
488
- *
489
- * @return string
490
468
*/
491
- public static function strToUpper ($ textValue )
469
+ public static function strToUpper (string $ textValue ): string
492
470
{
493
- return mb_convert_case ($ textValue ?? '' , MB_CASE_UPPER , 'UTF-8 ' );
471
+ return mb_convert_case ($ textValue , MB_CASE_UPPER , 'UTF-8 ' );
494
472
}
495
473
496
474
/**
497
475
* Convert a UTF-8 encoded string to lower case.
498
476
*
499
477
* @param string $textValue UTF-8 encoded string
500
- *
501
- * @return string
502
478
*/
503
- public static function strToLower ($ textValue )
479
+ public static function strToLower (string $ textValue ): string
504
480
{
505
- return mb_convert_case ($ textValue ?? '' , MB_CASE_LOWER , 'UTF-8 ' );
481
+ return mb_convert_case ($ textValue , MB_CASE_LOWER , 'UTF-8 ' );
506
482
}
507
483
508
484
/**
509
485
* Convert a UTF-8 encoded string to title/proper case
510
486
* (uppercase every first character in each word, lower case all other characters).
511
487
*
512
488
* @param string $textValue UTF-8 encoded string
513
- *
514
- * @return string
515
489
*/
516
- public static function strToTitle ($ textValue )
490
+ public static function strToTitle (string $ textValue ): string
517
491
{
518
492
return mb_convert_case ($ textValue , MB_CASE_TITLE , 'UTF-8 ' );
519
493
}
520
494
521
- public static function mbIsUpper ($ character )
495
+ public static function mbIsUpper (string $ character ): bool
522
496
{
523
- return mb_strtolower ($ character , 'UTF-8 ' ) != $ character ;
497
+ return mb_strtolower ($ character , 'UTF-8 ' ) !== $ character ;
524
498
}
525
499
526
- public static function mbStrSplit ($ string )
500
+ /**
501
+ * Splits a UTF-8 string into an array of individual characters.
502
+ */
503
+ public static function mbStrSplit (string $ string ): array
527
504
{
528
505
// Split at all position not after the start: ^
529
506
// and not before the end: $
530
- return preg_split ('/(?<!^)(?!$)/u ' , $ string );
507
+ $ split = preg_split ('/(?<!^)(?!$)/u ' , $ string );
508
+
509
+ return ($ split === false ) ? [] : $ split ;
531
510
}
532
511
533
512
/**
534
513
* Reverse the case of a string, so that all uppercase characters become lowercase
535
514
* and all lowercase characters become uppercase.
536
515
*
537
516
* @param string $textValue UTF-8 encoded string
538
- *
539
- * @return string
540
517
*/
541
- public static function strCaseReverse ($ textValue )
518
+ public static function strCaseReverse (string $ textValue ): string
542
519
{
543
520
$ characters = self ::mbStrSplit ($ textValue );
544
521
foreach ($ characters as &$ character ) {
@@ -557,10 +534,8 @@ public static function strCaseReverse($textValue)
557
534
* and convert it to a numeric if it is.
558
535
*
559
536
* @param string $operand string value to test
560
- *
561
- * @return bool
562
537
*/
563
- public static function convertToNumberIfFraction (&$ operand )
538
+ public static function convertToNumberIfFraction (string &$ operand ): bool
564
539
{
565
540
if (preg_match ('/^ ' . self ::STRING_REGEXP_FRACTION . '$/i ' , $ operand , $ match )) {
566
541
$ sign = ($ match [1 ] == '- ' ) ? '- ' : '+ ' ;
@@ -578,10 +553,8 @@ public static function convertToNumberIfFraction(&$operand)
578
553
/**
579
554
* Get the decimal separator. If it has not yet been set explicitly, try to obtain number
580
555
* formatting information from locale.
581
- *
582
- * @return string
583
556
*/
584
- public static function getDecimalSeparator ()
557
+ public static function getDecimalSeparator (): string
585
558
{
586
559
if (!isset (self ::$ decimalSeparator )) {
587
560
$ localeconv = localeconv ();
@@ -603,18 +576,16 @@ public static function getDecimalSeparator()
603
576
*
604
577
* @param string $separator Character for decimal separator
605
578
*/
606
- public static function setDecimalSeparator ($ separator ): void
579
+ public static function setDecimalSeparator (string $ separator ): void
607
580
{
608
581
self ::$ decimalSeparator = $ separator ;
609
582
}
610
583
611
584
/**
612
585
* Get the thousands separator. If it has not yet been set explicitly, try to obtain number
613
586
* formatting information from locale.
614
- *
615
- * @return string
616
587
*/
617
- public static function getThousandsSeparator ()
588
+ public static function getThousandsSeparator (): string
618
589
{
619
590
if (!isset (self ::$ thousandsSeparator )) {
620
591
$ localeconv = localeconv ();
@@ -636,18 +607,16 @@ public static function getThousandsSeparator()
636
607
*
637
608
* @param string $separator Character for thousands separator
638
609
*/
639
- public static function setThousandsSeparator ($ separator ): void
610
+ public static function setThousandsSeparator (string $ separator ): void
640
611
{
641
612
self ::$ thousandsSeparator = $ separator ;
642
613
}
643
614
644
615
/**
645
616
* Get the currency code. If it has not yet been set explicitly, try to obtain the
646
617
* symbol information from locale.
647
- *
648
- * @return string
649
618
*/
650
- public static function getCurrencyCode ()
619
+ public static function getCurrencyCode (): string
651
620
{
652
621
if (!empty (self ::$ currencyCode )) {
653
622
return self ::$ currencyCode ;
@@ -674,19 +643,19 @@ public static function getCurrencyCode()
674
643
*
675
644
* @param string $currencyCode Character for currency code
676
645
*/
677
- public static function setCurrencyCode ($ currencyCode ): void
646
+ public static function setCurrencyCode (string $ currencyCode ): void
678
647
{
679
648
self ::$ currencyCode = $ currencyCode ;
680
649
}
681
650
682
651
/**
683
652
* Convert SYLK encoded string to UTF-8.
684
653
*
685
- * @param string $textValue
654
+ * @param string $textValue SYLK encoded string
686
655
*
687
656
* @return string UTF-8 encoded string
688
657
*/
689
- public static function SYLKtoUTF8 ($ textValue )
658
+ public static function SYLKtoUTF8 (string $ textValue ): string
690
659
{
691
660
self ::buildCharacterSets ();
692
661
0 commit comments