PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.28
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.28
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / vendor / openspout / openspout / src / Common / Helper / StringHelper.php

StringHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.28, at vendor/openspout/openspout/src/Common/Helper/StringHelper.php

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