PageRenderTime 87ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

https://bitbucket.org/gencer/symfony
PHP | 285 lines | 135 code | 46 blank | 104 comment | 26 complexity | cef44b66cd11d55e27e0b14ceee87d09 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\Form\Extension\Core\DataTransformer;
  11. use Symfony\Component\Form\DataTransformerInterface;
  12. use Symfony\Component\Form\Exception\TransformationFailedException;
  13. /**
  14. * Transforms between a number type and a localized number with grouping
  15. * (each thousand) and comma separators.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  19. */
  20. class NumberToLocalizedStringTransformer implements DataTransformerInterface
  21. {
  22. /**
  23. * Rounds a number towards positive infinity.
  24. *
  25. * Rounds 1.4 to 2 and -1.4 to -1.
  26. */
  27. const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;
  28. /**
  29. * Rounds a number towards negative infinity.
  30. *
  31. * Rounds 1.4 to 1 and -1.4 to -2.
  32. */
  33. const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;
  34. /**
  35. * Rounds a number away from zero.
  36. *
  37. * Rounds 1.4 to 2 and -1.4 to -2.
  38. */
  39. const ROUND_UP = \NumberFormatter::ROUND_UP;
  40. /**
  41. * Rounds a number towards zero.
  42. *
  43. * Rounds 1.4 to 1 and -1.4 to -1.
  44. */
  45. const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;
  46. /**
  47. * Rounds to the nearest number and halves to the next even number.
  48. *
  49. * Rounds 2.5, 1.6 and 1.5 to 2 and 1.4 to 1.
  50. */
  51. const ROUND_HALF_EVEN = \NumberFormatter::ROUND_HALFEVEN;
  52. /**
  53. * Rounds to the nearest number and halves away from zero.
  54. *
  55. * Rounds 2.5 to 3, 1.6 and 1.5 to 2 and 1.4 to 1.
  56. */
  57. const ROUND_HALF_UP = \NumberFormatter::ROUND_HALFUP;
  58. /**
  59. * Rounds to the nearest number and halves towards zero.
  60. *
  61. * Rounds 2.5 and 1.6 to 2, 1.5 and 1.4 to 1.
  62. */
  63. const ROUND_HALF_DOWN = \NumberFormatter::ROUND_HALFDOWN;
  64. /**
  65. * Alias for {@link self::ROUND_HALF_EVEN}.
  66. *
  67. * @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0.
  68. */
  69. const ROUND_HALFEVEN = self::ROUND_HALF_EVEN;
  70. /**
  71. * Alias for {@link self::ROUND_HALF_UP}.
  72. *
  73. * @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0.
  74. */
  75. const ROUND_HALFUP = self::ROUND_HALF_UP;
  76. /**
  77. * Alias for {@link self::ROUND_HALF_DOWN}.
  78. *
  79. * @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0.
  80. */
  81. const ROUND_HALFDOWN = self::ROUND_HALF_DOWN;
  82. protected $precision;
  83. protected $grouping;
  84. protected $roundingMode;
  85. public function __construct($precision = null, $grouping = false, $roundingMode = self::ROUND_HALF_UP)
  86. {
  87. if (null === $grouping) {
  88. $grouping = false;
  89. }
  90. if (null === $roundingMode) {
  91. $roundingMode = self::ROUND_HALF_UP;
  92. }
  93. $this->precision = $precision;
  94. $this->grouping = $grouping;
  95. $this->roundingMode = $roundingMode;
  96. }
  97. /**
  98. * Transforms a number type into localized number.
  99. *
  100. * @param int|float $value Number value.
  101. *
  102. * @return string Localized value.
  103. *
  104. * @throws TransformationFailedException If the given value is not numeric
  105. * or if the value can not be transformed.
  106. */
  107. public function transform($value)
  108. {
  109. if (null === $value) {
  110. return '';
  111. }
  112. if (!is_numeric($value)) {
  113. throw new TransformationFailedException('Expected a numeric.');
  114. }
  115. $formatter = $this->getNumberFormatter();
  116. $value = $formatter->format($value);
  117. if (intl_is_failure($formatter->getErrorCode())) {
  118. throw new TransformationFailedException($formatter->getErrorMessage());
  119. }
  120. // Convert fixed spaces to normal ones
  121. $value = str_replace("\xc2\xa0", ' ', $value);
  122. return $value;
  123. }
  124. /**
  125. * Transforms a localized number into an integer or float
  126. *
  127. * @param string $value The localized value
  128. *
  129. * @return int|float The numeric value
  130. *
  131. * @throws TransformationFailedException If the given value is not a string
  132. * or if the value can not be transformed.
  133. */
  134. public function reverseTransform($value)
  135. {
  136. if (!is_string($value)) {
  137. throw new TransformationFailedException('Expected a string.');
  138. }
  139. if ('' === $value) {
  140. return;
  141. }
  142. if ('NaN' === $value) {
  143. throw new TransformationFailedException('"NaN" is not a valid number');
  144. }
  145. $position = 0;
  146. $formatter = $this->getNumberFormatter();
  147. $groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
  148. $decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
  149. if ('.' !== $decSep && (!$this->grouping || '.' !== $groupSep)) {
  150. $value = str_replace('.', $decSep, $value);
  151. }
  152. if (',' !== $decSep && (!$this->grouping || ',' !== $groupSep)) {
  153. $value = str_replace(',', $decSep, $value);
  154. }
  155. $result = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
  156. if (intl_is_failure($formatter->getErrorCode())) {
  157. throw new TransformationFailedException($formatter->getErrorMessage());
  158. }
  159. if ($result >= PHP_INT_MAX || $result <= -PHP_INT_MAX) {
  160. throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
  161. }
  162. if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value)) {
  163. $length = mb_strlen($value, $encoding);
  164. $remainder = mb_substr($value, $position, $length, $encoding);
  165. } else {
  166. $length = strlen($value);
  167. $remainder = substr($value, $position, $length);
  168. }
  169. // After parsing, position holds the index of the character where the
  170. // parsing stopped
  171. if ($position < $length) {
  172. // Check if there are unrecognized characters at the end of the
  173. // number (excluding whitespace characters)
  174. $remainder = trim($remainder, " \t\n\r\0\x0b\xc2\xa0");
  175. if ('' !== $remainder) {
  176. throw new TransformationFailedException(
  177. sprintf('The number contains unrecognized characters: "%s"', $remainder)
  178. );
  179. }
  180. }
  181. // NumberFormatter::parse() does not round
  182. return $this->round($result);
  183. }
  184. /**
  185. * Returns a preconfigured \NumberFormatter instance
  186. *
  187. * @return \NumberFormatter
  188. */
  189. protected function getNumberFormatter()
  190. {
  191. $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
  192. if (null !== $this->precision) {
  193. $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
  194. $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
  195. }
  196. $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping);
  197. return $formatter;
  198. }
  199. /**
  200. * Rounds a number according to the configured precision and rounding mode.
  201. *
  202. * @param int|float $number A number.
  203. *
  204. * @return int|float The rounded number.
  205. */
  206. private function round($number)
  207. {
  208. if (null !== $this->precision && null !== $this->roundingMode) {
  209. // shift number to maintain the correct precision during rounding
  210. $roundingCoef = pow(10, $this->precision);
  211. $number *= $roundingCoef;
  212. switch ($this->roundingMode) {
  213. case self::ROUND_CEILING:
  214. $number = ceil($number);
  215. break;
  216. case self::ROUND_FLOOR:
  217. $number = floor($number);
  218. break;
  219. case self::ROUND_UP:
  220. $number = $number > 0 ? ceil($number) : floor($number);
  221. break;
  222. case self::ROUND_DOWN:
  223. $number = $number > 0 ? floor($number) : ceil($number);
  224. break;
  225. case self::ROUND_HALF_EVEN:
  226. $number = round($number, 0, PHP_ROUND_HALF_EVEN);
  227. break;
  228. case self::ROUND_HALF_UP:
  229. $number = round($number, 0, PHP_ROUND_HALF_UP);
  230. break;
  231. case self::ROUND_HALF_DOWN:
  232. $number = round($number, 0, PHP_ROUND_HALF_DOWN);
  233. break;
  234. }
  235. $number /= $roundingCoef;
  236. }
  237. return $number;
  238. }
  239. }