]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
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\Exception\TransformationFailedException; | |
15 | ||
16 | /** | |
17 | * Transforms between an integer and a localized number with grouping | |
18 | * (each thousand) and comma separators. | |
19 | * | |
20 | * @author Bernhard Schussek <bschussek@gmail.com> | |
21 | */ | |
22 | class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransformer | |
23 | { | |
24 | /** | |
25 | * {@inheritDoc} | |
26 | */ | |
27 | public function reverseTransform($value) | |
28 | { | |
29 | if (!is_string($value)) { | |
30 | throw new TransformationFailedException('Expected a string.'); | |
31 | } | |
32 | ||
33 | if ('' === $value) { | |
34 | return null; | |
35 | } | |
36 | ||
37 | if ('NaN' === $value) { | |
38 | throw new TransformationFailedException('"NaN" is not a valid integer'); | |
39 | } | |
40 | ||
41 | $formatter = $this->getNumberFormatter(); | |
42 | $value = $formatter->parse( | |
43 | $value, | |
44 | PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32 | |
45 | ); | |
46 | ||
47 | if (intl_is_failure($formatter->getErrorCode())) { | |
48 | throw new TransformationFailedException($formatter->getErrorMessage()); | |
49 | } | |
50 | ||
51 | return $value; | |
52 | } | |
53 | } |