From e0299884fee00122ea8ad3bb52c7ab7d892192f7 Mon Sep 17 00:00:00 2001 From: Saleh Hashemi <81674631+salehhashemi1992@users.noreply.github.com> Date: Thu, 19 Oct 2023 20:57:52 +0330 Subject: [PATCH] Introduce createFormatter Static Method for Formatter Logic (#20014) --- framework/helpers/BaseFormatConverter.php | 54 ++++++++++++++--------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/framework/helpers/BaseFormatConverter.php b/framework/helpers/BaseFormatConverter.php index 1f2aeb4d673..00f37395ec7 100644 --- a/framework/helpers/BaseFormatConverter.php +++ b/framework/helpers/BaseFormatConverter.php @@ -97,22 +97,13 @@ class BaseFormatConverter * @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. * If not given, `Yii::$app->language` will be used. * @return string The converted date format pattern. + * @throws \Exception */ public static function convertDateIcuToPhp($pattern, $type = 'date', $locale = null) { if (isset(self::$_icuShortFormats[$pattern])) { if (extension_loaded('intl')) { - if ($locale === null) { - $locale = Yii::$app->language; - } - if ($type === 'date') { - $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE); - } elseif ($type === 'time') { - $formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]); - } else { - $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]); - } - $pattern = $formatter->getPattern(); + $pattern = static::createFormatter($locale, $type, $pattern); } else { return static::$phpFallbackDatePatterns[$pattern][$type]; } @@ -350,22 +341,13 @@ public static function convertDatePhpToIcu($pattern) * @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`. * If not given, `Yii::$app->language` will be used. * @return string The converted date format pattern. + * @throws \Exception */ public static function convertDateIcuToJui($pattern, $type = 'date', $locale = null) { if (isset(self::$_icuShortFormats[$pattern])) { if (extension_loaded('intl')) { - if ($locale === null) { - $locale = Yii::$app->language; - } - if ($type === 'date') { - $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE); - } elseif ($type === 'time') { - $formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]); - } else { - $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]); - } - $pattern = $formatter->getPattern(); + $pattern = static::createFormatter($locale, $type, $pattern); } else { return static::$juiFallbackDatePatterns[$pattern][$type]; } @@ -545,4 +527,32 @@ public static function convertDatePhpToJui($pattern) 'U' => '@', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) ]); } + + /** + * Creates a date/time formatter based on the given parameters. + * + * @param string|null $locale The locale to be used. If null, the application's current language will be used. + * @param string $type The type of formatter ('date', 'time', etc.) + * @param string $pattern The pattern for the IntlDateFormatter. + * + * @return string The resulting pattern after formatter creation. + * + * @throws \Exception If the 'intl' extension is not loaded. + */ + private static function createFormatter($locale, $type, $pattern) + { + if ($locale === null) { + $locale = Yii::$app->language; + } + + if ($type === 'date') { + $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE); + } elseif ($type === 'time') { + $formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]); + } else { + $formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]); + } + + return $formatter->getPattern(); + } }