1 | <?php |
||
23 | class NumberFormatter implements FormatterInterface, LocalizableInterface, FormatterNumberInterface |
||
24 | { |
||
25 | public const NO_BREAK_SPACE_HEX = 'c2a0'; |
||
26 | public const NARROW_NO_BREAK_SPACE_HEX = 'e280af'; |
||
27 | |||
28 | /** |
||
29 | * Formatter instances. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $formatters = []; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $params = []; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $default_params = [ |
||
44 | 'decimals' => 2, |
||
45 | 'locale' => null, |
||
46 | 'pattern' => null, |
||
47 | 'disableUseOfNonBreakingSpaces' => false |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @param array $params |
||
52 | * |
||
53 | * @throws Exception\ExtensionNotLoadedException if ext/intl is not present |
||
54 | * @throws Exception\InvalidArgumentException |
||
55 | */ |
||
56 | 24 | public function __construct(array $params = []) |
|
73 | |||
74 | /** |
||
75 | * @throws Exception\InvalidArgumentException |
||
76 | * |
||
77 | * @param array $params |
||
78 | */ |
||
79 | 24 | protected function setParams($params) |
|
90 | |||
91 | 12 | protected function initWhitespaceSeparator(IntlNumberFormatter $formatter): void |
|
92 | { |
||
93 | 12 | if ($this->params['disableUseOfNonBreakingSpaces'] === true |
|
94 | 3 | && in_array(bin2hex($formatter->getSymbol(IntlNumberFormatter::GROUPING_SEPARATOR_SYMBOL)), [ |
|
95 | 3 | self::NARROW_NO_BREAK_SPACE_HEX, |
|
96 | 3 | self::NO_BREAK_SPACE_HEX |
|
97 | 12 | ], true)) { |
|
98 | 3 | $formatter->setSymbol(IntlNumberFormatter::GROUPING_SEPARATOR_SYMBOL, ' '); |
|
99 | } |
||
100 | 12 | } |
|
101 | |||
102 | 6 | protected function loadFormatterId(string $formatterId): void |
|
118 | |||
119 | /** |
||
120 | * Format a number. |
||
121 | * |
||
122 | * @param float $number |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 3 | public function format($number, ArrayObject $row = null): string |
|
141 | |||
142 | /** |
||
143 | * Throws an Exception when number cannot be formatted. |
||
144 | * |
||
145 | * @param IntlNumberFormatter $intlFormatter |
||
146 | * @param int|string|float $number |
||
147 | * |
||
148 | * @throws Exception\RuntimeException |
||
149 | */ |
||
150 | 4 | protected function throwNumberFormatterException(IntlNumberFormatter $intlFormatter, $number): void |
|
160 | |||
161 | /** |
||
162 | * Set locale to use instead of the default. |
||
163 | * |
||
164 | * @param string $locale |
||
165 | */ |
||
166 | 17 | public function setLocale(?string $locale): self |
|
172 | |||
173 | /** |
||
174 | * Get the locale to use. |
||
175 | * |
||
176 | * @return string|null |
||
177 | */ |
||
178 | 6 | public function getLocale(): ?string |
|
182 | |||
183 | /** |
||
184 | * Set decimals. |
||
185 | * |
||
186 | * @param int $decimals |
||
187 | */ |
||
188 | 15 | public function setDecimals($decimals): self |
|
194 | |||
195 | /** |
||
196 | * @return int |
||
197 | */ |
||
198 | 6 | public function getDecimals(): int |
|
202 | |||
203 | /** |
||
204 | * Set the number pattern, (#,##0.###, ....). |
||
205 | * |
||
206 | * @see http://php.net/manual/en/numberformatter.setpattern.php |
||
207 | * |
||
208 | * @param string $pattern |
||
209 | */ |
||
210 | 8 | public function setPattern($pattern): self |
|
216 | |||
217 | /** |
||
218 | * Get the number pattern. |
||
219 | * |
||
220 | * @return string|null |
||
221 | */ |
||
222 | 6 | public function getPattern(): ?string |
|
226 | |||
227 | 3 | public function setDisableUseOfNonBreakingSpaces(bool $disable = true): self |
|
233 | } |
||
234 |