vendor/symfony/form/Extension/Core/DataTransformer/IntegerToLocalizedStringTransformer.php line 39

Open in your IDE?
  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\Exception\TransformationFailedException;
  12. /**
  13.  * Transforms between an integer and a localized number with grouping
  14.  * (each thousand) and comma separators.
  15.  *
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransformer
  19. {
  20.     /**
  21.      * Constructs a transformer.
  22.      *
  23.      * @param bool        $grouping     Whether thousands should be grouped
  24.      * @param int         $roundingMode One of the ROUND_ constants in this class
  25.      * @param string|null $locale       locale used for transforming
  26.      */
  27.     public function __construct(?bool $grouping false, ?int $roundingMode \NumberFormatter::ROUND_DOWNstring $locale null)
  28.     {
  29.         parent::__construct(0$grouping$roundingMode$locale);
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function reverseTransform($value)
  35.     {
  36.         $decimalSeparator $this->getNumberFormatter()->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
  37.         if (\is_string($value) && str_contains($value$decimalSeparator)) {
  38.             throw new TransformationFailedException(sprintf('The value "%s" is not a valid integer.'$value));
  39.         }
  40.         $result parent::reverseTransform($value);
  41.         return null !== $result ? (int) $result null;
  42.     }
  43.     /**
  44.      * @internal
  45.      */
  46.     protected function castParsedValue($value)
  47.     {
  48.         return $value;
  49.     }
  50. }