/** * {@inheritdoc} */ public function configureOptions(OptionsResolver $resolver) { $options = ['locale' => $this->localeResolver->getCurrentLocale()]; $decimalSeparator = $this->numberFactory->create($options)->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); $constraint = new NumberFormat(); $constraint->decimalSeparator = $decimalSeparator; $message = $this->formatValidator->getMessage($constraint); $resolver->setDefaults(['decimals_allowed' => true, 'invalid_message' => $message, 'invalid_message_parameters' => ['{{ decimal_separator }}' => $decimalSeparator], 'locale_options' => $options]); }
/** * {@inheritdoc} */ public function present($value, array $options = []) { if (!is_numeric($value)) { return $value; } $numberFormatter = $this->numberFactory->create($options); if (floor($value) != $value) { $numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); $numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 4); } return $numberFormatter->format($value); }
/** * {@inheritdoc} */ public function validate($number, $attributeCode, array $options = []) { if (null === $number || '' === $number || is_int($number) || is_float($number)) { return null; } $options = $this->getOptions($options); if (isset($options['locale']) && !isset($options['decimal_separator'])) { $numberFormatter = $this->numberFactory->create($options); $options['decimal_separator'] = $numberFormatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); } $constraint = new NumberFormat(); $constraint->decimalSeparator = $options['decimal_separator']; $constraint->path = $attributeCode; return $this->validator->validate($number, $constraint); }