예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (null === $value || '' === $value) {
         return;
     }
     if (!is_scalar($value)) {
         throw new UnexpectedTypeException($value, 'string');
     }
     $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
     $position = 0;
     $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
     if (intl_is_failure($formatter->getErrorCode()) || $position < strlen($value)) {
         /** @var Decimal $constraint */
         $this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]);
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function format($value, array $options = array())
 {
     if (null === $value) {
         return $options['null_value'];
     }
     if (!is_numeric($value)) {
         throw FormatterException::invalidType($this, $value, 'numeric value');
     }
     $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
     if (null !== $options['precision']) {
         $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']);
         $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $options['rounding_mode']);
     }
     $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $options['grouping']);
     $value = $formatter->format($value);
     if (intl_is_failure($formatter->getErrorCode())) {
         throw FormatterException::intlError($this, $formatter->getErrorMessage());
     }
     return $value;
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (null === $value || '' === $value) {
         return;
     }
     if (!is_scalar($value)) {
         throw new UnexpectedTypeException($value, 'string');
     }
     $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
     $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN);
     $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0);
     $formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0);
     $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
     $decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
     $position = 0;
     $formatter->parse($value, PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32, $position);
     if (intl_is_failure($formatter->getErrorCode()) || strpos($value, $decimalSeparator) !== false || $position < strlen($value)) {
         /** @var Integer $constraint */
         $this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]);
     }
 }
예제 #4
0
 public function getDisplayedValue($value)
 {
     if ($value !== null && $value !== '') {
         $formatter = new \NumberFormatter($this->locale, $this->style);
         if ($this->precision !== null) {
             $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
             $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
         }
         if ($this->ruleSet !== null) {
             $formatter->setTextAttribute(\NumberFormatter::DEFAULT_RULESET, $this->ruleSet);
         }
         $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping);
         if ($this->style === \NumberFormatter::PERCENT && !$this->fractional) {
             $value /= 100;
         }
         if ($this->style === \NumberFormatter::CURRENCY) {
             if ($this->currencyCode === null) {
                 $this->currencyCode = $formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
             }
             if (strlen($this->currencyCode) !== 3) {
                 throw new TransformationFailedException('Your locale definition is not complete, you have to define a language and a country. (.e.g en_US, fr_FR)');
             }
             $value = $formatter->formatCurrency($value, $this->currencyCode);
         } else {
             $value = $formatter->format($value);
         }
         if (intl_is_failure($formatter->getErrorCode())) {
             throw new TransformationFailedException($formatter->getErrorMessage());
         }
         if (key_exists((string) $value, $this->values)) {
             $value = $this->values[$value];
         }
         return $value;
     }
     return '';
 }
 /**
  * Throws an Exception when number cannot be formatted
  * @param IntlNumberFormatter $intlFormatter
  * @param int|string|float $number
  * @throws Exception\RuntimeException
  */
 protected function throwNumberFormatterException(IntlNumberFormatter $intlFormatter, $number)
 {
     $error_code = $intlFormatter->getErrorCode();
     if (is_scalar($number)) {
         $val = (string) $number;
     } else {
         $val = 'type: ' . gettype($number);
     }
     throw new Exception\RuntimeException(__METHOD__ . " Cannot format value '{$val}', Intl/NumberFormatter error code: {$error_code}.");
 }