bug #39653 [Form] fix passing null $pattern to IntlDateFormatter (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] fix passing null $pattern to IntlDateFormatter

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Commits
-------

52360c1e4e [Form] fix passing null $pattern to IntlDateFormatter
This commit is contained in:
Nicolas Grekas 2020-12-29 14:57:58 +01:00
commit 841ec99831
3 changed files with 10 additions and 10 deletions

View File

@ -172,7 +172,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
$calendar = $this->calendar; $calendar = $this->calendar;
$pattern = $this->pattern; $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 // new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/66323
if (!$intlDateFormatter) { if (!$intlDateFormatter) {

View File

@ -51,14 +51,14 @@ class DateType extends AbstractType
$dateFormat = \is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT; $dateFormat = \is_int($options['format']) ? $options['format'] : self::DEFAULT_FORMAT;
$timeFormat = \IntlDateFormatter::NONE; $timeFormat = \IntlDateFormatter::NONE;
$calendar = \IntlDateFormatter::GREGORIAN; $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)) { 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.'); 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 ('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)); 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 $pattern
)); ));
} else { } 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)); 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->datetype = null !== $datetype ? $datetype : self::FULL;
$this->timetype = null !== $timetype ? $timetype : self::FULL; $this->timetype = null !== $timetype ? $timetype : self::FULL;
if ('' === ($pattern ?? '')) {
$pattern = $this->getDefaultPattern();
}
$this->setPattern($pattern); $this->setPattern($pattern);
$this->setTimeZone($timezone); $this->setTimeZone($timezone);
} }
@ -485,7 +489,7 @@ abstract class IntlDateFormatter
/** /**
* Set the formatter's pattern. * 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 * @return bool true on success or false on failure
* *
@ -494,11 +498,7 @@ abstract class IntlDateFormatter
*/ */
public function setPattern($pattern) public function setPattern($pattern)
{ {
if (null === $pattern) { $this->pattern = (string) $pattern;
$pattern = $this->getDefaultPattern();
}
$this->pattern = $pattern;
return true; return true;
} }