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 | 'force_non_breaking_whitespace' => 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 |
|
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 | * @return NumberFormatter |
||
167 | */ |
||
168 | 17 | public function setLocale($locale) |
|
174 | |||
175 | /** |
||
176 | * Get the locale to use. |
||
177 | * |
||
178 | * @return string|null |
||
179 | */ |
||
180 | 6 | public function getLocale() |
|
184 | |||
185 | /** |
||
186 | * Set decimals. |
||
187 | * |
||
188 | * @param int $decimals |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | 15 | public function setDecimals($decimals) |
|
198 | |||
199 | /** |
||
200 | * @return int |
||
201 | */ |
||
202 | 6 | public function getDecimals() |
|
206 | |||
207 | /** |
||
208 | * Set the number pattern, (#,##0.###, ....). |
||
209 | * |
||
210 | * @see http://php.net/manual/en/numberformatter.setpattern.php |
||
211 | * |
||
212 | * @param string $pattern |
||
213 | * |
||
214 | * @return NumberFormatter |
||
215 | */ |
||
216 | 8 | public function setPattern($pattern) |
|
222 | |||
223 | /** |
||
224 | * Get the number pattern. |
||
225 | * |
||
226 | * @return string|null |
||
227 | */ |
||
228 | 6 | public function getPattern() |
|
232 | } |
||
233 |