|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Locale\Stub; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Locale\Stub\StubLocale; |
|
|
15 |
use Symfony\Component\Locale\Exception\NotImplementedException; |
|
|
16 |
use Symfony\Component\Locale\Exception\MethodNotImplementedException; |
|
|
17 |
use Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException; |
|
|
18 |
use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException; |
|
|
19 |
|
|
|
20 |
/** |
|
|
21 |
* Provides a stub NumberFormatter for the 'en' locale. |
|
|
22 |
* |
|
|
23 |
* @author Eriksen Costa <eriksen.costa@infranology.com.br> |
|
|
24 |
*/ |
|
|
25 |
class StubNumberFormatter |
|
|
26 |
{ |
|
|
27 |
/** |
|
|
28 |
* Constants defined by the intl extension, not class constants in NumberFormatter |
|
|
29 |
* TODO: remove if the Form component drop the call to the intl_is_failure() function |
|
|
30 |
* |
|
|
31 |
* @see StubNumberFormatter::getErrorCode() |
|
|
32 |
* @see StubNumberFormatter::getErrorMessage() |
|
|
33 |
*/ |
|
|
34 |
const U_ZERO_ERROR = 0; |
|
|
35 |
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR'; |
|
|
36 |
|
|
|
37 |
/** Format style constants */ |
|
|
38 |
const PATTERN_DECIMAL = 0; |
|
|
39 |
const DECIMAL = 1; |
|
|
40 |
const CURRENCY = 2; |
|
|
41 |
const PERCENT = 3; |
|
|
42 |
const SCIENTIFIC = 4; |
|
|
43 |
const SPELLOUT = 5; |
|
|
44 |
const ORDINAL = 6; |
|
|
45 |
const DURATION = 7; |
|
|
46 |
const PATTERN_RULEBASED = 9; |
|
|
47 |
const IGNORE = 0; |
|
|
48 |
const DEFAULT_STYLE = 1; |
|
|
49 |
|
|
|
50 |
/** Format type constants */ |
|
|
51 |
const TYPE_DEFAULT = 0; |
|
|
52 |
const TYPE_INT32 = 1; |
|
|
53 |
const TYPE_INT64 = 2; |
|
|
54 |
const TYPE_DOUBLE = 3; |
|
|
55 |
const TYPE_CURRENCY = 4; |
|
|
56 |
|
|
|
57 |
/** Numeric attribute constants */ |
|
|
58 |
const PARSE_INT_ONLY = 0; |
|
|
59 |
const GROUPING_USED = 1; |
|
|
60 |
const DECIMAL_ALWAYS_SHOWN = 2; |
|
|
61 |
const MAX_INTEGER_DIGITS = 3; |
|
|
62 |
const MIN_INTEGER_DIGITS = 4; |
|
|
63 |
const INTEGER_DIGITS = 5; |
|
|
64 |
const MAX_FRACTION_DIGITS = 6; |
|
|
65 |
const MIN_FRACTION_DIGITS = 7; |
|
|
66 |
const FRACTION_DIGITS = 8; |
|
|
67 |
const MULTIPLIER = 9; |
|
|
68 |
const GROUPING_SIZE = 10; |
|
|
69 |
const ROUNDING_MODE = 11; |
|
|
70 |
const ROUNDING_INCREMENT = 12; |
|
|
71 |
const FORMAT_WIDTH = 13; |
|
|
72 |
const PADDING_POSITION = 14; |
|
|
73 |
const SECONDARY_GROUPING_SIZE = 15; |
|
|
74 |
const SIGNIFICANT_DIGITS_USED = 16; |
|
|
75 |
const MIN_SIGNIFICANT_DIGITS = 17; |
|
|
76 |
const MAX_SIGNIFICANT_DIGITS = 18; |
|
|
77 |
const LENIENT_PARSE = 19; |
|
|
78 |
|
|
|
79 |
/** Text attribute constants */ |
|
|
80 |
const POSITIVE_PREFIX = 0; |
|
|
81 |
const POSITIVE_SUFFIX = 1; |
|
|
82 |
const NEGATIVE_PREFIX = 2; |
|
|
83 |
const NEGATIVE_SUFFIX = 3; |
|
|
84 |
const PADDING_CHARACTER = 4; |
|
|
85 |
const CURRENCY_CODE = 5; |
|
|
86 |
const DEFAULT_RULESET = 6; |
|
|
87 |
const PUBLIC_RULESETS = 7; |
|
|
88 |
|
|
|
89 |
/** Format symbol constants */ |
|
|
90 |
const DECIMAL_SEPARATOR_SYMBOL = 0; |
|
|
91 |
const GROUPING_SEPARATOR_SYMBOL = 1; |
|
|
92 |
const PATTERN_SEPARATOR_SYMBOL = 2; |
|
|
93 |
const PERCENT_SYMBOL = 3; |
|
|
94 |
const ZERO_DIGIT_SYMBOL = 4; |
|
|
95 |
const DIGIT_SYMBOL = 5; |
|
|
96 |
const MINUS_SIGN_SYMBOL = 6; |
|
|
97 |
const PLUS_SIGN_SYMBOL = 7; |
|
|
98 |
const CURRENCY_SYMBOL = 8; |
|
|
99 |
const INTL_CURRENCY_SYMBOL = 9; |
|
|
100 |
const MONETARY_SEPARATOR_SYMBOL = 10; |
|
|
101 |
const EXPONENTIAL_SYMBOL = 11; |
|
|
102 |
const PERMILL_SYMBOL = 12; |
|
|
103 |
const PAD_ESCAPE_SYMBOL = 13; |
|
|
104 |
const INFINITY_SYMBOL = 14; |
|
|
105 |
const NAN_SYMBOL = 15; |
|
|
106 |
const SIGNIFICANT_DIGIT_SYMBOL = 16; |
|
|
107 |
const MONETARY_GROUPING_SEPARATOR_SYMBOL = 17; |
|
|
108 |
|
|
|
109 |
/** Rounding mode values used by NumberFormatter::setAttribute() with NumberFormatter::ROUNDING_MODE attribute */ |
|
|
110 |
const ROUND_CEILING = 0; |
|
|
111 |
const ROUND_FLOOR = 1; |
|
|
112 |
const ROUND_DOWN = 2; |
|
|
113 |
const ROUND_UP = 3; |
|
|
114 |
const ROUND_HALFEVEN = 4; |
|
|
115 |
const ROUND_HALFDOWN = 5; |
|
|
116 |
const ROUND_HALFUP = 6; |
|
|
117 |
|
|
|
118 |
/** Pad position values used by NumberFormatter::setAttribute() with NumberFormatter::PADDING_POSITION attribute */ |
|
|
119 |
const PAD_BEFORE_PREFIX = 0; |
|
|
120 |
const PAD_AFTER_PREFIX = 1; |
|
|
121 |
const PAD_BEFORE_SUFFIX = 2; |
|
|
122 |
const PAD_AFTER_SUFFIX = 3; |
|
|
123 |
|
|
|
124 |
/** |
|
|
125 |
* Default values for the en locale |
|
|
126 |
* @var array |
|
|
127 |
*/ |
|
|
128 |
private $attributes = array( |
|
|
129 |
self::FRACTION_DIGITS => 0, |
|
|
130 |
self::GROUPING_USED => 1, |
|
|
131 |
self::ROUNDING_MODE => self::ROUND_HALFEVEN |
|
|
132 |
); |
|
|
133 |
|
|
|
134 |
/** |
|
|
135 |
* Holds the initialized attributes code |
|
|
136 |
* @var array |
|
|
137 |
*/ |
|
|
138 |
private $initializedAttributes = array(); |
|
|
139 |
|
|
|
140 |
/** |
|
|
141 |
* The supported styles to the constructor $styles argument |
|
|
142 |
* @var array |
|
|
143 |
*/ |
|
|
144 |
static private $supportedStyles = array( |
|
|
145 |
'CURRENCY' => self::CURRENCY, |
|
|
146 |
'DECIMAL' => self::DECIMAL |
|
|
147 |
); |
|
|
148 |
|
|
|
149 |
/** |
|
|
150 |
* Supported attributes to the setAttribute() $attr argument |
|
|
151 |
* @var array |
|
|
152 |
*/ |
|
|
153 |
static private $supportedAttributes = array( |
|
|
154 |
'FRACTION_DIGITS' => self::FRACTION_DIGITS, |
|
|
155 |
'GROUPING_USED' => self::GROUPING_USED, |
|
|
156 |
'ROUNDING_MODE' => self::ROUNDING_MODE |
|
|
157 |
); |
|
|
158 |
|
|
|
159 |
/** |
|
|
160 |
* The available rounding modes for setAttribute() usage with |
|
|
161 |
* StubNumberFormatter::ROUNDING_MODE. StubNumberFormatter::ROUND_DOWN |
|
|
162 |
* and StubNumberFormatter::ROUND_UP does not have a PHP only equivalent |
|
|
163 |
* |
|
|
164 |
* @var array |
|
|
165 |
*/ |
|
|
166 |
static private $roundingModes = array( |
|
|
167 |
'ROUND_HALFEVEN' => self::ROUND_HALFEVEN, |
|
|
168 |
'ROUND_HALFDOWN' => self::ROUND_HALFDOWN, |
|
|
169 |
'ROUND_HALFUP' => self::ROUND_HALFUP |
|
|
170 |
); |
|
|
171 |
|
|
|
172 |
/** |
|
|
173 |
* The mapping between NumberFormatter rounding modes to the available |
|
|
174 |
* modes in PHP's round() function. |
|
|
175 |
* |
|
|
176 |
* @see http://www.php.net/manual/en/function.round.php |
|
|
177 |
* @var array |
|
|
178 |
*/ |
|
|
179 |
static private $phpRoundingMap = array( |
|
|
180 |
self::ROUND_HALFDOWN => \PHP_ROUND_HALF_DOWN, |
|
|
181 |
self::ROUND_HALFEVEN => \PHP_ROUND_HALF_EVEN, |
|
|
182 |
self::ROUND_HALFUP => \PHP_ROUND_HALF_UP |
|
|
183 |
); |
|
|
184 |
|
|
|
185 |
/** |
|
|
186 |
* The maximum values of the integer type in 32 bit platforms. |
|
|
187 |
* @var array |
|
|
188 |
*/ |
|
|
189 |
static private $intRange = array( |
|
|
190 |
'positive' => 2147483647, |
|
|
191 |
'negative' => -2147483648 |
|
|
192 |
); |
|
|
193 |
|
|
|
194 |
/** |
|
|
195 |
* @var string |
|
|
196 |
*/ |
|
|
197 |
private $locale = null; |
|
|
198 |
|
|
|
199 |
/** |
|
|
200 |
* @var int |
|
|
201 |
*/ |
|
|
202 |
private $style = null; |
|
|
203 |
|
|
|
204 |
/** |
|
|
205 |
* Constructor |
|
|
206 |
* |
|
|
207 |
* @param string $locale The locale code |
|
|
208 |
* @param int $style Style of the formatting, one of the format style constants |
|
|
209 |
* @param string $pattern A pattern string in case $style is NumberFormat::PATTERN_DECIMAL or |
|
|
210 |
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax |
|
|
211 |
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation |
|
|
212 |
* @see http://www.php.net/manual/en/numberformatter.create.php |
|
|
213 |
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details |
|
|
214 |
* @see http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details |
|
|
215 |
* @throws MethodArgumentValueNotImplementedException When $locale different than 'en' is passed |
|
|
216 |
* @throws MethodArgumentValueNotImplementedException When the $style is not supported |
|
|
217 |
* @throws MethodArgumentNotImplementedException When the pattern value is different than null |
|
|
218 |
*/ |
|
|
219 |
public function __construct($locale = 'en', $style = null, $pattern = null) |
|
|
220 |
{ |
|
|
221 |
if ('en' != $locale) { |
|
|
222 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported'); |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
if (!in_array($style, self::$supportedStyles)) { |
|
|
226 |
$message = sprintf('The available styles are: %s.', implode(', ', array_keys(self::$supportedStyles))); |
|
|
227 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'style', $style, $message); |
|
|
228 |
} |
|
|
229 |
|
|
|
230 |
if (null !== $pattern) { |
|
|
231 |
throw new MethodArgumentNotImplementedException(__METHOD__, 'pattern'); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
$this->locale = $locale; |
|
|
235 |
$this->style = $style; |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
/** |
|
|
239 |
* Static constructor |
|
|
240 |
* |
|
|
241 |
* @param string $locale The locale code |
|
|
242 |
* @param int $style Style of the formatting, one of the format style constants |
|
|
243 |
* @param string $pattern A pattern string in case $style is NumberFormat::PATTERN_DECIMAL or |
|
|
244 |
* NumberFormat::PATTERN_RULEBASED. It must conform to the syntax |
|
|
245 |
* described in the ICU DecimalFormat or ICU RuleBasedNumberFormat documentation |
|
|
246 |
* @see http://www.php.net/manual/en/numberformatter.create.php |
|
|
247 |
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details |
|
|
248 |
* @see http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details |
|
|
249 |
* @throws MethodArgumentValueNotImplementedException When $locale different than 'en' is passed |
|
|
250 |
* @throws MethodArgumentValueNotImplementedException When the $style is not supported |
|
|
251 |
* @throws MethodArgumentNotImplementedException When the pattern value is different than null |
|
|
252 |
*/ |
|
|
253 |
static public function create($locale = 'en', $style = null, $pattern = null) |
|
|
254 |
{ |
|
|
255 |
return new self($locale, $style, $pattern); |
|
|
256 |
} |
|
|
257 |
|
|
|
258 |
/** |
|
|
259 |
* Format a currency value |
|
|
260 |
* |
|
|
261 |
* @param float $value The numeric currency value |
|
|
262 |
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use |
|
|
263 |
* @return string The formatted currency value |
|
|
264 |
* @see http://www.php.net/manual/en/numberformatter.formatcurrency.php |
|
|
265 |
* @see http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm |
|
|
266 |
*/ |
|
|
267 |
public function formatCurrency($value, $currency) |
|
|
268 |
{ |
|
|
269 |
if ($this->style == self::DECIMAL) { |
|
|
270 |
return $this->format($value); |
|
|
271 |
} |
|
|
272 |
|
|
|
273 |
$symbol = $this->getCurrencySymbol($currency); |
|
|
274 |
$fractionDigits = $this->getCurrencyFractionDigits($currency); |
|
|
275 |
|
|
|
276 |
$value = $this->roundCurrency($value, $currency); |
|
|
277 |
|
|
|
278 |
$negative = false; |
|
|
279 |
if (0 > $value) { |
|
|
280 |
$negative = true; |
|
|
281 |
$value *= -1; |
|
|
282 |
} |
|
|
283 |
|
|
|
284 |
$value = $this->formatNumber($value, $fractionDigits); |
|
|
285 |
|
|
|
286 |
$ret = $symbol.$value; |
|
|
287 |
|
|
|
288 |
return $negative ? '('.$ret.')' : $ret; |
|
|
289 |
} |
|
|
290 |
|
|
|
291 |
/** |
|
|
292 |
* Format a number |
|
|
293 |
* |
|
|
294 |
* @param number $value The value to format |
|
|
295 |
* @param int $type Type of the formatting, one of the format type constants |
|
|
296 |
* @return Boolean|string The formatted value or false on error |
|
|
297 |
* @see http://www.php.net/manual/en/numberformatter.format.php |
|
|
298 |
* @throws RuntimeException If the method is called with the class $style 'CURRENCY' |
|
|
299 |
* @throws MethodArgumentNotImplementedException If the $type is different than TYPE_DEFAULT |
|
|
300 |
*/ |
|
|
301 |
public function format($value, $type = self::TYPE_DEFAULT) |
|
|
302 |
{ |
|
|
303 |
// The original NumberFormatter does not support this format type |
|
|
304 |
if ($type == self::TYPE_CURRENCY) { |
|
|
305 |
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING); |
|
|
306 |
|
|
|
307 |
return false; |
|
|
308 |
} |
|
|
309 |
|
|
|
310 |
if ($this->style == self::CURRENCY) { |
|
|
311 |
throw new \RuntimeException(sprintf( |
|
|
312 |
'%s() method does not support the formatting of currencies (instance with CURRENCY style). %s', |
|
|
313 |
__METHOD__, NotImplementedException::INTL_INSTALL_MESSAGE |
|
|
314 |
)); |
|
|
315 |
} |
|
|
316 |
|
|
|
317 |
// Only the default type is supported. |
|
|
318 |
if ($type != self::TYPE_DEFAULT) { |
|
|
319 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'type', $type, 'Only TYPE_DEFAULT is supported'); |
|
|
320 |
} |
|
|
321 |
|
|
|
322 |
$fractionDigits = $this->getAttribute(self::FRACTION_DIGITS); |
|
|
323 |
|
|
|
324 |
$value = $this->round($value, $fractionDigits); |
|
|
325 |
|
|
|
326 |
return $this->formatNumber($value, $fractionDigits); |
|
|
327 |
} |
|
|
328 |
|
|
|
329 |
/** |
|
|
330 |
* Returns an attribute value |
|
|
331 |
* |
|
|
332 |
* @param int $attr An attribute specifier, one of the numeric attribute constants |
|
|
333 |
* @return Boolean|int The attribute value on success or false on error |
|
|
334 |
* @see http://www.php.net/manual/en/numberformatter.getattribute.php |
|
|
335 |
*/ |
|
|
336 |
public function getAttribute($attr) |
|
|
337 |
{ |
|
|
338 |
return isset($this->attributes[$attr]) ? $this->attributes[$attr] : null; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
/** |
|
|
342 |
* Returns formatter's last error code. Always returns the U_ZERO_ERROR class constant value |
|
|
343 |
* |
|
|
344 |
* @return int The error code from last formatter call |
|
|
345 |
* @see http://www.php.net/manual/en/numberformatter.geterrorcode.php |
|
|
346 |
*/ |
|
|
347 |
public function getErrorCode() |
|
|
348 |
{ |
|
|
349 |
return self::U_ZERO_ERROR; |
|
|
350 |
} |
|
|
351 |
|
|
|
352 |
/** |
|
|
353 |
* Returns formatter's last error message. Always returns the U_ZERO_ERROR_MESSAGE class constant value |
|
|
354 |
* |
|
|
355 |
* @return string The error message from last formatter call |
|
|
356 |
* @see http://www.php.net/manual/en/numberformatter.geterrormessage.php |
|
|
357 |
*/ |
|
|
358 |
public function getErrorMessage() |
|
|
359 |
{ |
|
|
360 |
return self::U_ZERO_ERROR_MESSAGE; |
|
|
361 |
} |
|
|
362 |
|
|
|
363 |
/** |
|
|
364 |
* Returns the formatter's locale |
|
|
365 |
* |
|
|
366 |
* @param int $type The locale name type to return between valid or actual (StubLocale::VALID_LOCALE or StubLocale::ACTUAL_LOCALE, respectively) |
|
|
367 |
* @return string The locale name used to create the formatter |
|
|
368 |
* @see http://www.php.net/manual/en/numberformatter.getlocale.php |
|
|
369 |
*/ |
|
|
370 |
public function getLocale($type = StubLocale::ACTUAL_LOCALE) |
|
|
371 |
{ |
|
|
372 |
return $this->locale; |
|
|
373 |
} |
|
|
374 |
|
|
|
375 |
/** |
|
|
376 |
* Returns the formatter's pattern |
|
|
377 |
* |
|
|
378 |
* @return Boolean|string The pattern string used by the formatter or false on error |
|
|
379 |
* @see http://www.php.net/manual/en/numberformatter.getpattern.php |
|
|
380 |
* @throws MethodNotImplementedException |
|
|
381 |
*/ |
|
|
382 |
public function getPattern() |
|
|
383 |
{ |
|
|
384 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
385 |
} |
|
|
386 |
|
|
|
387 |
/** |
|
|
388 |
* Returns a formatter symbol value |
|
|
389 |
* |
|
|
390 |
* @param int $attr A symbol specifier, one of the format symbol constants |
|
|
391 |
* @return Boolean|string The symbol value or false on error |
|
|
392 |
* @see http://www.php.net/manual/en/numberformatter.getsymbol.php |
|
|
393 |
* @throws MethodNotImplementedException |
|
|
394 |
*/ |
|
|
395 |
public function getSymbol($attr) |
|
|
396 |
{ |
|
|
397 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
398 |
} |
|
|
399 |
|
|
|
400 |
/** |
|
|
401 |
* Returns a formatter text attribute value |
|
|
402 |
* |
|
|
403 |
* @param int $attr An attribute specifier, one of the text attribute constants |
|
|
404 |
* @return Boolean|string The attribute value or false on error |
|
|
405 |
* @see http://www.php.net/manual/en/numberformatter.gettextattribute.php |
|
|
406 |
* @throws MethodNotImplementedException |
|
|
407 |
*/ |
|
|
408 |
public function getTextAttribute($attr) |
|
|
409 |
{ |
|
|
410 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
411 |
} |
|
|
412 |
|
|
|
413 |
/** |
|
|
414 |
* Parse a currency number |
|
|
415 |
* |
|
|
416 |
* @param string $value The value to parse |
|
|
417 |
* @param string $currency Parameter to receive the currency name (reference) |
|
|
418 |
* @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended |
|
|
419 |
* @return Boolean|string The parsed numeric value of false on error |
|
|
420 |
* @see http://www.php.net/manual/en/numberformatter.parsecurrency.php |
|
|
421 |
* @throws MethodNotImplementedException |
|
|
422 |
*/ |
|
|
423 |
public function parseCurrency($value, &$currency, &$position = null) |
|
|
424 |
{ |
|
|
425 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
426 |
} |
|
|
427 |
|
|
|
428 |
/** |
|
|
429 |
* Parse a number |
|
|
430 |
* |
|
|
431 |
* @param string $value The value to parse |
|
|
432 |
* @param string $type Type of the formatting, one of the format type constants. NumberFormatter::TYPE_DOUBLE by default |
|
|
433 |
* @param int $position Offset to begin the parsing on return this value will hold the offset at which the parsing ended |
|
|
434 |
* @return Boolean|string The parsed value of false on error |
|
|
435 |
* @see http://www.php.net/manual/en/numberformatter.parse.php |
|
|
436 |
* @throws MethodArgumentValueNotImplementedException When $type equals to TYPE_INT64, behavior not implemented |
|
|
437 |
* @throws MethodArgumentNotImplementedException When $position different than null, behavior not implemented |
|
|
438 |
*/ |
|
|
439 |
public function parse($value, $type = self::TYPE_DOUBLE, &$position = null) |
|
|
440 |
{ |
|
|
441 |
if ($type == self::TYPE_DEFAULT || $type == self::TYPE_CURRENCY) { |
|
|
442 |
trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING); |
|
|
443 |
|
|
|
444 |
return false; |
|
|
445 |
} |
|
|
446 |
|
|
|
447 |
// Not implemented, the NumberFormatter behavior is inconsistency |
|
|
448 |
if ($type == self::TYPE_INT64) { |
|
|
449 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'type', $type); |
|
|
450 |
} |
|
|
451 |
|
|
|
452 |
// We don't calculate the position when parsing the value |
|
|
453 |
if (null !== $position) { |
|
|
454 |
throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); |
|
|
455 |
} |
|
|
456 |
|
|
|
457 |
preg_match('/^([^0-9\-]{0,})(.*)/', $value, $matches); |
|
|
458 |
|
|
|
459 |
// Any string before the numeric value causes error in the parsing |
|
|
460 |
if (isset($matches[1]) && !empty($matches[1])) { |
|
|
461 |
return false; |
|
|
462 |
} |
|
|
463 |
|
|
|
464 |
// Remove everything that is not number or dot (.) |
|
|
465 |
$value = preg_replace('/[^0-9\.\-]/', '', $value); |
|
|
466 |
|
|
|
467 |
return $this->convertValueDataType($value, $type); |
|
|
468 |
} |
|
|
469 |
|
|
|
470 |
/** |
|
|
471 |
* Set an attribute |
|
|
472 |
* |
|
|
473 |
* @param int $attr An attribute specifier, one of the numeric attribute constants |
|
|
474 |
* @param int $value The attribute value |
|
|
475 |
* @return Boolean true on success or false on failure |
|
|
476 |
* @see http://www.php.net/manual/en/numberformatter.setattribute.php |
|
|
477 |
* @throws MethodArgumentValueNotImplementedException When the $attr is not supported |
|
|
478 |
* @throws MethodArgumentValueNotImplementedException When the $value is not supported |
|
|
479 |
*/ |
|
|
480 |
public function setAttribute($attr, $value) |
|
|
481 |
{ |
|
|
482 |
if (!in_array($attr, self::$supportedAttributes)) { |
|
|
483 |
$message = sprintf( |
|
|
484 |
'The available attributes are: %s', |
|
|
485 |
implode(', ', array_keys(self::$supportedAttributes)) |
|
|
486 |
); |
|
|
487 |
|
|
|
488 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'attr', $value, $message); |
|
|
489 |
} |
|
|
490 |
|
|
|
491 |
if (self::$supportedAttributes['ROUNDING_MODE'] == $attr && $this->isInvalidRoundingMode($value)) { |
|
|
492 |
$message = sprintf( |
|
|
493 |
'The supported values for ROUNDING_MODE are: %s', |
|
|
494 |
implode(', ', array_keys(self::$roundingModes)) |
|
|
495 |
); |
|
|
496 |
|
|
|
497 |
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'attr', $value, $message); |
|
|
498 |
} |
|
|
499 |
|
|
|
500 |
if (self::$supportedAttributes['GROUPING_USED'] == $attr) { |
|
|
501 |
$value = $this->normalizeGroupingUsedValue($value); |
|
|
502 |
} |
|
|
503 |
|
|
|
504 |
if (self::$supportedAttributes['FRACTION_DIGITS'] == $attr) { |
|
|
505 |
$value = $this->normalizeFractionDigitsValue($value); |
|
|
506 |
} |
|
|
507 |
|
|
|
508 |
$this->attributes[$attr] = $value; |
|
|
509 |
$this->initializedAttributes[$attr] = true; |
|
|
510 |
|
|
|
511 |
return true; |
|
|
512 |
} |
|
|
513 |
|
|
|
514 |
/** |
|
|
515 |
* Set the formatter's pattern |
|
|
516 |
* |
|
|
517 |
* @param string $pattern A pattern string in conformance with the ICU DecimalFormat documentation |
|
|
518 |
* @return Boolean true on success or false on failure |
|
|
519 |
* @see http://www.php.net/manual/en/numberformatter.setpattern.php |
|
|
520 |
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details |
|
|
521 |
* @throws MethodNotImplementedException |
|
|
522 |
*/ |
|
|
523 |
public function setPattern($pattern) |
|
|
524 |
{ |
|
|
525 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
526 |
} |
|
|
527 |
|
|
|
528 |
/** |
|
|
529 |
* Set the formatter's symbol |
|
|
530 |
* |
|
|
531 |
* @param int $attr A symbol specifier, one of the format symbol constants |
|
|
532 |
* @param string $value The value for the symbol |
|
|
533 |
* @return Boolean true on success or false on failure |
|
|
534 |
* @see http://www.php.net/manual/en/numberformatter.setsymbol.php |
|
|
535 |
* @throws MethodNotImplementedException |
|
|
536 |
*/ |
|
|
537 |
public function setSymbol($attr, $value) |
|
|
538 |
{ |
|
|
539 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
540 |
} |
|
|
541 |
|
|
|
542 |
/** |
|
|
543 |
* Set a text attribute |
|
|
544 |
* |
|
|
545 |
* @param int $attr An attribute specifier, one of the text attribute constants |
|
|
546 |
* @param int $value The attribute value |
|
|
547 |
* @return Boolean true on success or false on failure |
|
|
548 |
* @see http://www.php.net/manual/en/numberformatter.settextattribute.php |
|
|
549 |
* @throws MethodNotImplementedException |
|
|
550 |
*/ |
|
|
551 |
public function setTextAttribute($attr, $value) |
|
|
552 |
{ |
|
|
553 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
554 |
} |
|
|
555 |
|
|
|
556 |
/** |
|
|
557 |
* Rounds a currency value, applying increment rounding if applicable |
|
|
558 |
* |
|
|
559 |
* When a currency have a rounding increment, an extra round is made after the first one. The rounding factor is |
|
|
560 |
* determined in the ICU data and is explained as of: |
|
|
561 |
* |
|
|
562 |
* "the rounding increment is given in units of 10^(-fraction_digits)" |
|
|
563 |
* |
|
|
564 |
* The only actual rounding data as of this writing, is CHF. |
|
|
565 |
* |
|
|
566 |
* @param float $value The numeric currency value |
|
|
567 |
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use |
|
|
568 |
* @return string The rounded numeric currency value |
|
|
569 |
* @see http://en.wikipedia.org/wiki/Swedish_rounding |
|
|
570 |
* @see http://www.docjar.com/html/api/com/ibm/icu/util/Currency.java.html#1007 |
|
|
571 |
*/ |
|
|
572 |
private function roundCurrency($value, $currency) |
|
|
573 |
{ |
|
|
574 |
$fractionDigits = $this->getCurrencyFractionDigits($currency); |
|
|
575 |
$roundingIncrement = $this->getCurrencyRoundingIncrement($currency); |
|
|
576 |
|
|
|
577 |
// Round with the formatter rounding mode |
|
|
578 |
$value = $this->round($value, $fractionDigits); |
|
|
579 |
|
|
|
580 |
// Swiss rounding |
|
|
581 |
if (0 < $roundingIncrement && 0 < $fractionDigits) { |
|
|
582 |
$roundingFactor = $roundingIncrement / pow(10, $fractionDigits); |
|
|
583 |
$value = round($value / $roundingFactor) * $roundingFactor; |
|
|
584 |
} |
|
|
585 |
|
|
|
586 |
return $value; |
|
|
587 |
} |
|
|
588 |
|
|
|
589 |
/** |
|
|
590 |
* Returns the currency symbol |
|
|
591 |
* |
|
|
592 |
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use |
|
|
593 |
* @return string The currency symbol |
|
|
594 |
*/ |
|
|
595 |
private function getCurrencySymbol($currency) |
|
|
596 |
{ |
|
|
597 |
$currencies = StubLocale::getCurrenciesData($this->locale); |
|
|
598 |
|
|
|
599 |
return $currencies[$currency]['symbol']; |
|
|
600 |
} |
|
|
601 |
|
|
|
602 |
/** |
|
|
603 |
* Returns the fraction digits of a currency |
|
|
604 |
* |
|
|
605 |
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use |
|
|
606 |
* @return string The fraction digits of a currency |
|
|
607 |
*/ |
|
|
608 |
private function getCurrencyFractionDigits($currency) |
|
|
609 |
{ |
|
|
610 |
$currencies = StubLocale::getCurrenciesData($this->locale); |
|
|
611 |
|
|
|
612 |
return $currencies[$currency]['fractionDigits']; |
|
|
613 |
} |
|
|
614 |
|
|
|
615 |
/** |
|
|
616 |
* Returns the rounding increment of a currency |
|
|
617 |
* |
|
|
618 |
* @param string $currency The 3-letter ISO 4217 currency code indicating the currency to use |
|
|
619 |
* @return string The rounding increment of a currency |
|
|
620 |
*/ |
|
|
621 |
private function getCurrencyRoundingIncrement($currency) |
|
|
622 |
{ |
|
|
623 |
$currencies = StubLocale::getCurrenciesData($this->locale); |
|
|
624 |
|
|
|
625 |
return $currencies[$currency]['roundingIncrement']; |
|
|
626 |
} |
|
|
627 |
|
|
|
628 |
/** |
|
|
629 |
* Rounds a value. |
|
|
630 |
* |
|
|
631 |
* @param numeric $value The value to round |
|
|
632 |
* @param int $precision The number of decimal digits to round to |
|
|
633 |
* @return numeric The rounded value |
|
|
634 |
*/ |
|
|
635 |
private function round($value, $precision) |
|
|
636 |
{ |
|
|
637 |
$precision = $this->getUnitializedPrecision($value, $precision); |
|
|
638 |
|
|
|
639 |
$roundingMode = self::$phpRoundingMap[$this->getAttribute(self::ROUNDING_MODE)]; |
|
|
640 |
$value = round($value, $precision, $roundingMode); |
|
|
641 |
|
|
|
642 |
return $value; |
|
|
643 |
} |
|
|
644 |
|
|
|
645 |
/** |
|
|
646 |
* Formats a number. |
|
|
647 |
* |
|
|
648 |
* @param numeric $value The numeric value to format |
|
|
649 |
* @param int $precision The number of decimal digits to use |
|
|
650 |
* @return string The formatted number |
|
|
651 |
*/ |
|
|
652 |
private function formatNumber($value, $precision) |
|
|
653 |
{ |
|
|
654 |
$precision = $this->getUnitializedPrecision($value, $precision); |
|
|
655 |
|
|
|
656 |
return number_format($value, $precision, '.', $this->getAttribute(self::GROUPING_USED) ? ',' : ''); |
|
|
657 |
} |
|
|
658 |
|
|
|
659 |
/** |
|
|
660 |
* Returns the precision value if the the DECIMAL style is being used and the FRACTION_DIGITS attribute is unitialized. |
|
|
661 |
* |
|
|
662 |
* @param numeric $value The value to get the precision from if the FRACTION_DIGITS attribute is unitialized |
|
|
663 |
* @param int $precision The precision value to returns if the FRACTION_DIGITS attribute is initialized |
|
|
664 |
* @return int The precision value |
|
|
665 |
*/ |
|
|
666 |
private function getUnitializedPrecision($value, $precision) |
|
|
667 |
{ |
|
|
668 |
if ($this->style == self::CURRENCY) { |
|
|
669 |
return $precision; |
|
|
670 |
} |
|
|
671 |
|
|
|
672 |
if (!$this->isInitializedAttribute(self::FRACTION_DIGITS)) { |
|
|
673 |
preg_match('/.*\.(.*)/', (string) $value, $digits); |
|
|
674 |
if (isset($digits[1])) { |
|
|
675 |
$precision = strlen($digits[1]); |
|
|
676 |
} |
|
|
677 |
} |
|
|
678 |
|
|
|
679 |
return $precision; |
|
|
680 |
} |
|
|
681 |
|
|
|
682 |
/** |
|
|
683 |
* Check if the attribute is initialized (value set by client code). |
|
|
684 |
* |
|
|
685 |
* @param string $attr The attribute name |
|
|
686 |
* @return Boolean true if the value was set by client, false otherwise |
|
|
687 |
*/ |
|
|
688 |
private function isInitializedAttribute($attr) |
|
|
689 |
{ |
|
|
690 |
return isset($this->initializedAttributes[$attr]); |
|
|
691 |
} |
|
|
692 |
|
|
|
693 |
/** |
|
|
694 |
* Returns the numeric value using the $type to convert to the right data type. |
|
|
695 |
* |
|
|
696 |
* @param mixed $value The value to be converted |
|
|
697 |
* @param int $type The type to convert. Can be TYPE_DOUBLE (float) or TYPE_INT32 (int) |
|
|
698 |
* @return numeric The converted value |
|
|
699 |
*/ |
|
|
700 |
private function convertValueDataType($value, $type) |
|
|
701 |
{ |
|
|
702 |
if ($type == self::TYPE_DOUBLE) { |
|
|
703 |
$value = (float) $value; |
|
|
704 |
} |
|
|
705 |
elseif ($type == self::TYPE_INT32) { |
|
|
706 |
$value = $this->getIntValue($value); |
|
|
707 |
} |
|
|
708 |
|
|
|
709 |
return $value; |
|
|
710 |
} |
|
|
711 |
|
|
|
712 |
/** |
|
|
713 |
* Convert the value data type to int or returns false if the value is out of the integer value range. |
|
|
714 |
* |
|
|
715 |
* @param mixed $value The value to be converted |
|
|
716 |
* @return int The converted value |
|
|
717 |
*/ |
|
|
718 |
private function getIntValue($value) |
|
|
719 |
{ |
|
|
720 |
if ($value > self::$intRange['positive'] || $value < self::$intRange['negative']) { |
|
|
721 |
return false; |
|
|
722 |
} |
|
|
723 |
|
|
|
724 |
return (int) $value; |
|
|
725 |
} |
|
|
726 |
|
|
|
727 |
/** |
|
|
728 |
* Check if the rounding mode is invalid. |
|
|
729 |
* |
|
|
730 |
* @param int $value The rounding mode value to check |
|
|
731 |
* @return Boolean true if the rounding mode is invalid, false otherwise |
|
|
732 |
*/ |
|
|
733 |
private function isInvalidRoundingMode($value) |
|
|
734 |
{ |
|
|
735 |
if (in_array($value, self::$roundingModes, true)) { |
|
|
736 |
return false; |
|
|
737 |
} |
|
|
738 |
|
|
|
739 |
return true; |
|
|
740 |
} |
|
|
741 |
|
|
|
742 |
/** |
|
|
743 |
* Returns the normalized value for the GROUPING_USED attribute. Any value that can be converted to int will be |
|
|
744 |
* cast to Boolean and then to int again. This way, negative values are converted to 1 and string values to 0. |
|
|
745 |
* |
|
|
746 |
* @param mixed $value The value to be normalized |
|
|
747 |
* @return int The normalized value for the attribute (0 or 1) |
|
|
748 |
*/ |
|
|
749 |
private function normalizeGroupingUsedValue($value) { |
|
|
750 |
return (int) (Boolean) (int) $value; |
|
|
751 |
} |
|
|
752 |
|
|
|
753 |
/** |
|
|
754 |
* Returns the normalized value for the FRACTION_DIGITS attribute. The value is converted to int and if negative, |
|
|
755 |
* the returned value will be 0. |
|
|
756 |
* |
|
|
757 |
* @param mixed $value The value to be normalized |
|
|
758 |
* @return int The normalized value for the attribute |
|
|
759 |
*/ |
|
|
760 |
private function normalizeFractionDigitsValue($value) |
|
|
761 |
{ |
|
|
762 |
$value = (int) $value; |
|
|
763 |
|
|
|
764 |
return (0 > $value) ? 0 : $value; |
|
|
765 |
} |
|
|
766 |
} |