| 1 |
<?php |
| 2 |
|
| 3 |
namespace OpenSpout\Common\Helper; |
| 4 |
|
| 5 |
/** |
| 6 |
* This class provides helper functions to work with strings and multibyte strings. |
| 7 |
* |
| 8 |
* @codeCoverageIgnore |
| 9 |
*/ |
| 10 |
class StringHelper |
| 11 |
{ |
| 12 |
/** @var bool Whether the mbstring extension is loaded */ |
| 13 |
protected $hasMbstringSupport; |
| 14 |
|
| 15 |
/** @var bool Whether the code is running with PHP7 or older versions */ |
| 16 |
private $isRunningPhp7OrOlder; |
| 17 |
|
| 18 |
/** @var array Locale info, used for number formatting */ |
| 19 |
private $localeInfo; |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->hasMbstringSupport = \extension_loaded('mbstring'); |
| 24 |
$this->isRunningPhp7OrOlder = version_compare(PHP_VERSION, '8.0.0') < 0; |
| 25 |
$this->localeInfo = localeconv(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Returns the length of the given string. |
| 30 |
* It uses the multi-bytes function is available. |
| 31 |
* |
| 32 |
* @see strlen |
| 33 |
* @see mb_strlen |
| 34 |
* |
| 35 |
* @param string $string |
| 36 |
* |
| 37 |
* @return int |
| 38 |
*/ |
| 39 |
public function getStringLength($string) |
| 40 |
{ |
| 41 |
return $this->hasMbstringSupport ? mb_strlen($string) : \strlen($string); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the position of the first occurrence of the given character/substring within the given string. |
| 46 |
* It uses the multi-bytes function is available. |
| 47 |
* |
| 48 |
* @see strpos |
| 49 |
* @see mb_strpos |
| 50 |
* |
| 51 |
* @param string $char Needle |
| 52 |
* @param string $string Haystack |
| 53 |
* |
| 54 |
* @return int Char/substring's first occurrence position within the string if found (starts at 0) or -1 if not found |
| 55 |
*/ |
| 56 |
public function getCharFirstOccurrencePosition($char, $string) |
| 57 |
{ |
| 58 |
$position = $this->hasMbstringSupport ? mb_strpos($string, $char) : strpos($string, $char); |
| 59 |
|
| 60 |
return (false !== $position) ? $position : -1; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the position of the last occurrence of the given character/substring within the given string. |
| 65 |
* It uses the multi-bytes function is available. |
| 66 |
* |
| 67 |
* @see strrpos |
| 68 |
* @see mb_strrpos |
| 69 |
* |
| 70 |
* @param string $char Needle |
| 71 |
* @param string $string Haystack |
| 72 |
* |
| 73 |
* @return int Char/substring's last occurrence position within the string if found (starts at 0) or -1 if not found |
| 74 |
*/ |
| 75 |
public function getCharLastOccurrencePosition($char, $string) |
| 76 |
{ |
| 77 |
$position = $this->hasMbstringSupport ? mb_strrpos($string, $char) : strrpos($string, $char); |
| 78 |
|
| 79 |
return (false !== $position) ? $position : -1; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Formats a numeric value (int or float) in a way that's compatible with the expected spreadsheet format. |
| 84 |
* |
| 85 |
* Formatting of float values is locale dependent in PHP < 8. |
| 86 |
* Thousands separators and decimal points vary from locale to locale (en_US: 12.34 vs pl_PL: 12,34). |
| 87 |
* However, float values must be formatted with no thousands separator and a "." as decimal point |
| 88 |
* to work properly. This method can be used to convert the value to the correct format before storing it. |
| 89 |
* |
| 90 |
* @see https://wiki.php.net/rfc/locale_independent_float_to_string for the changed behavior in PHP8. |
| 91 |
* |
| 92 |
* @param float|int $numericValue |
| 93 |
* |
| 94 |
* @return float|int|string |
| 95 |
*/ |
| 96 |
public function formatNumericValue($numericValue) |
| 97 |
{ |
| 98 |
if ($this->isRunningPhp7OrOlder && \is_float($numericValue)) { |
| 99 |
return str_replace( |
| 100 |
[$this->localeInfo['thousands_sep'], $this->localeInfo['decimal_point']], |
| 101 |
['', '.'], |
| 102 |
(string) $numericValue |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
return $numericValue; |
| 107 |
} |
| 108 |
} |
| 109 |
|