From c279056a36e7527425784d7c849c387bd4df5ea1 Mon Sep 17 00:00:00 2001 From: salehhashemi1992 <81674631+salehhashemi1992@users.noreply.github.com> Date: Thu, 19 Oct 2023 17:09:35 +0330 Subject: [PATCH 1/2] Extract formatter logic into createFormatter method --- framework/helpers/BaseFormatConverter.php | 58 ++++++++++++++--------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/framework/helpers/BaseFormatConverter.php b/framework/helpers/BaseFormatConverter.php index 1f2aeb4d673..81b89735137 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,36 @@ 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. + */ + public static function createFormatter($locale, $type, $pattern) + { + if (!extension_loaded('intl')) { + throw new \Exception("The 'intl' extension is required but not loaded."); + } + + 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(); + } } From a1dba1d10d8ad3dfb6c2be8762c9b764d5f54d6d Mon Sep 17 00:00:00 2001 From: salehhashemi1992 <81674631+salehhashemi1992@users.noreply.github.com> Date: Thu, 19 Oct 2023 20:40:52 +0330 Subject: [PATCH 2/2] make createFormatter method private --- framework/helpers/BaseFormatConverter.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/framework/helpers/BaseFormatConverter.php b/framework/helpers/BaseFormatConverter.php index 81b89735137..00f37395ec7 100644 --- a/framework/helpers/BaseFormatConverter.php +++ b/framework/helpers/BaseFormatConverter.php @@ -539,12 +539,8 @@ public static function convertDatePhpToJui($pattern) * * @throws \Exception If the 'intl' extension is not loaded. */ - public static function createFormatter($locale, $type, $pattern) + private static function createFormatter($locale, $type, $pattern) { - if (!extension_loaded('intl')) { - throw new \Exception("The 'intl' extension is required but not loaded."); - } - if ($locale === null) { $locale = Yii::$app->language; }