[Form] fix passing null $pattern to IntlDateFormatter

This commit is contained in:
Nicolas Grekas 2020-12-29 14:41:13 +01:00
parent 40677a014e
commit 52360c1e4e
3 changed files with 10 additions and 10 deletions

View File

@ -172,7 +172,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$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) {

View File

@ -51,14 +51,14 @@ class DateType extends AbstractType
$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));
}
@ -71,7 +71,7 @@ class DateType extends AbstractType
$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));
}

View File

@ -145,6 +145,10 @@ abstract class IntlDateFormatter
$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);
}
@ -485,7 +489,7 @@ abstract class IntlDateFormatter
/**
* 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
*
@ -494,11 +498,7 @@ abstract class IntlDateFormatter
*/
public function setPattern($pattern)
{
if (null === $pattern) {
$pattern = $this->getDefaultPattern();
}
$this->pattern = $pattern;
$this->pattern = (string) $pattern;
return true;
}