コード例 #1
0
 /**
  * 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]);
 }
コード例 #2
0
 /**
  * {@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);
 }
コード例 #3
0
 /**
  * {@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);
 }
コード例 #4
0
 /**
  * {@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();
     }
 }
コード例 #5
0
 /**
  * @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()];
 }
コード例 #6
0
 /**
  * @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()];
 }