diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php | 149 |
1 files changed, 0 insertions, 149 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php deleted file mode 100644 index e099d436..00000000 --- a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php +++ /dev/null | |||
@@ -1,149 +0,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\Form\Extension\Core\DataTransformer; | ||
13 | |||
14 | use Symfony\Component\Form\DataTransformerInterface; | ||
15 | use Symfony\Component\Form\Exception\TransformationFailedException; | ||
16 | use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
17 | |||
18 | /** | ||
19 | * Transforms between a normalized format (integer or float) and a percentage value. | ||
20 | * | ||
21 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
22 | * @author Florian Eckerstorfer <florian@eckerstorfer.org> | ||
23 | */ | ||
24 | class PercentToLocalizedStringTransformer implements DataTransformerInterface | ||
25 | { | ||
26 | const FRACTIONAL = 'fractional'; | ||
27 | const INTEGER = 'integer'; | ||
28 | |||
29 | protected static $types = array( | ||
30 | self::FRACTIONAL, | ||
31 | self::INTEGER, | ||
32 | ); | ||
33 | |||
34 | private $type; | ||
35 | |||
36 | private $precision; | ||
37 | |||
38 | /** | ||
39 | * Constructor. | ||
40 | * | ||
41 | * @see self::$types for a list of supported types | ||
42 | * | ||
43 | * @param integer $precision The precision | ||
44 | * @param string $type One of the supported types | ||
45 | * | ||
46 | * @throws UnexpectedTypeException if the given value of type is unknown | ||
47 | */ | ||
48 | public function __construct($precision = null, $type = null) | ||
49 | { | ||
50 | if (null === $precision) { | ||
51 | $precision = 0; | ||
52 | } | ||
53 | |||
54 | if (null === $type) { | ||
55 | $type = self::FRACTIONAL; | ||
56 | } | ||
57 | |||
58 | if (!in_array($type, self::$types, true)) { | ||
59 | throw new UnexpectedTypeException($type, implode('", "', self::$types)); | ||
60 | } | ||
61 | |||
62 | $this->type = $type; | ||
63 | $this->precision = $precision; | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * Transforms between a normalized format (integer or float) into a percentage value. | ||
68 | * | ||
69 | * @param number $value Normalized value | ||
70 | * | ||
71 | * @return number Percentage value | ||
72 | * | ||
73 | * @throws TransformationFailedException If the given value is not numeric or | ||
74 | * if the value could not be transformed. | ||
75 | */ | ||
76 | public function transform($value) | ||
77 | { | ||
78 | if (null === $value) { | ||
79 | return ''; | ||
80 | } | ||
81 | |||
82 | if (!is_numeric($value)) { | ||
83 | throw new TransformationFailedException('Expected a numeric.'); | ||
84 | } | ||
85 | |||
86 | if (self::FRACTIONAL == $this->type) { | ||
87 | $value *= 100; | ||
88 | } | ||
89 | |||
90 | $formatter = $this->getNumberFormatter(); | ||
91 | $value = $formatter->format($value); | ||
92 | |||
93 | if (intl_is_failure($formatter->getErrorCode())) { | ||
94 | throw new TransformationFailedException($formatter->getErrorMessage()); | ||
95 | } | ||
96 | |||
97 | // replace the UTF-8 non break spaces | ||
98 | return $value; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Transforms between a percentage value into a normalized format (integer or float). | ||
103 | * | ||
104 | * @param number $value Percentage value. | ||
105 | * | ||
106 | * @return number Normalized value. | ||
107 | * | ||
108 | * @throws TransformationFailedException If the given value is not a string or | ||
109 | * if the value could not be transformed. | ||
110 | */ | ||
111 | public function reverseTransform($value) | ||
112 | { | ||
113 | if (!is_string($value)) { | ||
114 | throw new TransformationFailedException('Expected a string.'); | ||
115 | } | ||
116 | |||
117 | if ('' === $value) { | ||
118 | return null; | ||
119 | } | ||
120 | |||
121 | $formatter = $this->getNumberFormatter(); | ||
122 | // replace normal spaces so that the formatter can read them | ||
123 | $value = $formatter->parse(str_replace(' ', ' ', $value)); | ||
124 | |||
125 | if (intl_is_failure($formatter->getErrorCode())) { | ||
126 | throw new TransformationFailedException($formatter->getErrorMessage()); | ||
127 | } | ||
128 | |||
129 | if (self::FRACTIONAL == $this->type) { | ||
130 | $value /= 100; | ||
131 | } | ||
132 | |||
133 | return $value; | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * Returns a preconfigured \NumberFormatter instance | ||
138 | * | ||
139 | * @return \NumberFormatter | ||
140 | */ | ||
141 | protected function getNumberFormatter() | ||
142 | { | ||
143 | $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); | ||
144 | |||
145 | $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); | ||
146 | |||
147 | return $formatter; | ||
148 | } | ||
149 | } | ||