/** * Date format action (show pattern expected for current locale) * * @return JsonResponse */ public function dateAction() { $locale = $this->localeResolver->getCurrentLocale(); $dateFormatter = $this->dateFactory->create(['locale' => $locale]); $timeFormatter = $this->datetimeFactory->create(['locale' => $locale, 'timetype' => \IntlDateFormatter::SHORT]); return new JsonResponse(['date' => ['format' => $dateFormatter->getPattern(), 'defaultFormat' => LocalizerInterface::DEFAULT_DATE_FORMAT], 'time' => ['format' => $timeFormatter->getPattern(), 'defaultFormat' => LocalizerInterface::DEFAULT_DATETIME_FORMAT], 'language' => $locale]); }
/** * {@inheritdoc} */ public function localize($date, array $options = []) { if (null === $date || '' === $date) { return $date; } $options = $this->getOptions($options); $formatter = $this->factory->create($options); $datetime = new \DateTime($date); return $formatter->format($datetime); }
/** * {@inheritdoc} */ public function present($value, array $options = []) { if (null === $value || '' === $value) { return $value; } if (!$value instanceof \DateTime) { $value = new \DateTime($value); } $formatter = $this->dateFactory->create($options); return $formatter->format($value); }
/** * {@inheritdoc} */ public function validate($date, Constraint $constraint) { $formatter = $this->factory->create(['date_format' => $constraint->dateFormat]); $formatter->setLenient(false); $hasSameSeparators = $this->hasSameSeparators($date, $constraint->dateFormat); if (false === $formatter->parse($date) || !$hasSameSeparators) { $violation = $this->context->buildViolation($constraint->message, ['{{ date_format }}' => $constraint->dateFormat]); $violation->atPath($constraint->path); $violation->addViolation(); } }
/** * @return array */ public function getFormats() { $options = ['locale' => $this->getCurrentLocale()]; $decimalSeparator = $this->numberFactory->create($options)->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); return ['decimal_separator' => $decimalSeparator, 'date_format' => $this->dateFactory->create($options)->getPattern()]; }
/** * @return array */ public function getFormats() { $locale = $this->getCurrentLocale(); return ['decimal_separator' => $this->numberFormatProvider->getFormat($locale)['decimal_separator'], 'date_format' => $this->dateFactory->create(['locale' => $locale])->getPattern()]; }