From 6ded31a3b9fcdcfa4f33d1cc812b458cf02f563b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 31 Dec 2018 18:50:08 +0100 Subject: [PATCH] [Intl] handle null date and time types --- .../Intl/DateFormatter/IntlDateFormatter.php | 42 +++++++++---------- .../AbstractIntlDateFormatterTest.php | 14 +++++++ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php index fe46f3f2b2..be97f93593 100644 --- a/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php +++ b/src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php @@ -118,13 +118,13 @@ class IntlDateFormatter private $timeZoneId; /** - * @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en") - * @param int $datetype Type of date formatting, one of the format type constants - * @param int $timetype Type of time formatting, one of the format type constants - * @param mixed $timezone Timezone identifier - * @param int $calendar Calendar to use for formatting or parsing. The only currently - * supported value is IntlDateFormatter::GREGORIAN (or null using the default calendar, i.e. "GREGORIAN") - * @param string $pattern Optional pattern to use when formatting + * @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en") + * @param int|null $datetype Type of date formatting, one of the format type constants + * @param int|null $timetype Type of time formatting, one of the format type constants + * @param \IntlTimeZone|\DateTimeZone|string|null $timezone Timezone identifier + * @param int $calendar Calendar to use for formatting or parsing. The only currently + * supported value is IntlDateFormatter::GREGORIAN (or null using the default calendar, i.e. "GREGORIAN") + * @param string|null $pattern Optional pattern to use when formatting * * @see http://www.php.net/manual/en/intldateformatter.create.php * @see http://userguide.icu-project.org/formatparse/datetime @@ -142,8 +142,8 @@ class IntlDateFormatter throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported'); } - $this->datetype = $datetype; - $this->timetype = $timetype; + $this->datetype = null !== $datetype ? $datetype : self::FULL; + $this->timetype = null !== $timetype ? $timetype : self::FULL; $this->setPattern($pattern); $this->setTimeZone($timezone); @@ -152,13 +152,13 @@ class IntlDateFormatter /** * Static constructor. * - * @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en") - * @param int $datetype Type of date formatting, one of the format type constants - * @param int $timetype Type of time formatting, one of the format type constants - * @param string $timezone Timezone identifier - * @param int $calendar Calendar to use for formatting or parsing; default is Gregorian - * One of the calendar constants - * @param string $pattern Optional pattern to use when formatting + * @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en") + * @param int|null $datetype Type of date formatting, one of the format type constants + * @param int|null $timetype Type of time formatting, one of the format type constants + * @param \IntlTimeZone|\DateTimeZone|string|null $timezone Timezone identifier + * @param int $calendar Calendar to use for formatting or parsing; default is Gregorian + * One of the calendar constants + * @param string|null $pattern Optional pattern to use when formatting * * @return self * @@ -485,7 +485,7 @@ class IntlDateFormatter /** * Set the formatter's pattern. * - * @param string $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation + * @param string|null $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation * * @return bool true on success or false on failure * @@ -506,9 +506,9 @@ class IntlDateFormatter /** * Set the formatter's timezone identifier. * - * @param string $timeZoneId The time zone ID string of the time zone to use. - * If NULL or the empty string, the default time zone for the - * runtime is used. + * @param string|null $timeZoneId The time zone ID string of the time zone to use. + * If NULL or the empty string, the default time zone for the + * runtime is used. * * @return bool true on success or false on failure * @@ -552,7 +552,7 @@ class IntlDateFormatter /** * This method was added in PHP 5.5 as replacement for `setTimeZoneId()`. * - * @param mixed $timeZone + * @param \IntlTimeZone|\DateTimeZone|string|null $timeZone * * @return bool true on success or false on failure * diff --git a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php index 77d3c125c1..1359cc4222 100644 --- a/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php +++ b/src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php @@ -46,6 +46,20 @@ abstract class AbstractIntlDateFormatterTest extends TestCase ); } + public function testConstructorWithoutDateType() + { + $formatter = new IntlDateFormatter('en', null, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN); + + $this->assertSame('EEEE, LLLL d, y, h:mm a', $formatter->getPattern()); + } + + public function testConstructorWithoutTimeType() + { + $formatter = new IntlDateFormatter('en', IntlDateFormatter::SHORT, null, 'UTC', IntlDateFormatter::GREGORIAN); + + $this->assertSame('M/d/yy, h:mm:ss a zzzz', $formatter->getPattern()); + } + /** * @dataProvider formatProvider */