/** * 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]); return new JsonResponse(['date' => ['format' => $dateFormatter->getPattern(), 'defaultFormat' => LocalizerInterface::DEFAULT_DATE_FORMAT], 'time' => ['format' => $timeFormatter->getPattern(), 'defaultFormat' => LocalizerInterface::DEFAULT_DATETIME_FORMAT], 'language' => $locale, '12_hour_format' => false !== strpos($timeFormatter->getPattern(), 'a')]); }
/** * {@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 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(); } }
/** * {@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 configureOptions(OptionsResolver $resolver) { $placeholderDefault = function (Options $options) { return $options['required'] ? null : ''; }; $constraint = new DateFormat(); $dateFormat = $this->dateFactory->create(['locale' => $this->localeResolver->getCurrentLocale()])->getPattern(); $resolver->setDefaults(['widget' => 'single_text', 'placeholder' => 'oro.form.click_here_to_select', 'invalid_message' => $constraint->message, 'invalid_message_parameters' => ['{{ date_format }}' => $dateFormat], 'format' => $dateFormat])->setNormalizer('placeholder', function (Options $options, $placeholder) use($placeholderDefault) { if (is_string($placeholder)) { return $placeholder; } elseif (is_array($placeholder)) { $default = $placeholderDefault($options); return array_merge(['year' => $default, 'month' => $default, 'day' => $default], $placeholder); } return ['year' => $placeholder, 'month' => $placeholder, 'day' => $placeholder]; }); }