Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] fix passing null $pattern to IntlDateFormatter #39653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function getIntlDateFormatter($ignoreTimezone = false)
$calendar = $this->calendar;
$pattern = $this->pattern;

$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern ?? '');

// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/66323
if (!$intlDateFormatter) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$dateFormat = \is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT;
$timeFormat = \IntlDateFormatter::NONE;
$calendar = \IntlDateFormatter::GREGORIAN;
$pattern = \is_string($options['format']) ? $options['format'] : null;
$pattern = \is_string($options['format']) ? $options['format'] : '';

if (!\in_array($dateFormat, self::$acceptedFormats, true)) {
throw new InvalidOptionsException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format.');
}

if ('single_text' === $options['widget']) {
if (null !== $pattern && false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd')) {
if ('' !== $pattern && false === strpos($pattern, 'y') && false === strpos($pattern, 'M') && false === strpos($pattern, 'd')) {
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s".', $pattern));
}

Expand All @@ -71,7 +71,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$pattern
));
} else {
if (null !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
if ('' !== $pattern && (false === strpos($pattern, 'y') || false === strpos($pattern, 'M') || false === strpos($pattern, 'd'))) {
throw new InvalidOptionsException(sprintf('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s".', $pattern));
}

Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function __construct(?string $locale, ?int $datetype, ?int $timetype, $ti
$this->datetype = null !== $datetype ? $datetype : self::FULL;
$this->timetype = null !== $timetype ? $timetype : self::FULL;

if ('' === ($pattern ?? '')) {
$pattern = $this->getDefaultPattern();
}

$this->setPattern($pattern);
$this->setTimeZone($timezone);
}
Expand Down Expand Up @@ -485,7 +489,7 @@ public function setLenient($lenient)
/**
* Set the formatter's pattern.
*
* @param string|null $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation
* @param string $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation
*
* @return bool true on success or false on failure
*
Expand All @@ -494,11 +498,7 @@ public function setLenient($lenient)
*/
public function setPattern($pattern)
{
if (null === $pattern) {
$pattern = $this->getDefaultPattern();
}

$this->pattern = $pattern;
$this->pattern = (string) $pattern;

return true;
}
Expand Down