PageRenderTime 88ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php

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