Skip to content

Commit e029988

Browse files
Introduce createFormatter Static Method for Formatter Logic (#20014)
1 parent fd241de commit e029988

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

framework/helpers/BaseFormatConverter.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,13 @@ class BaseFormatConverter
9797
* @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
9898
* If not given, `Yii::$app->language` will be used.
9999
* @return string The converted date format pattern.
100+
* @throws \Exception
100101
*/
101102
public static function convertDateIcuToPhp($pattern, $type = 'date', $locale = null)
102103
{
103104
if (isset(self::$_icuShortFormats[$pattern])) {
104105
if (extension_loaded('intl')) {
105-
if ($locale === null) {
106-
$locale = Yii::$app->language;
107-
}
108-
if ($type === 'date') {
109-
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
110-
} elseif ($type === 'time') {
111-
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
112-
} else {
113-
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
114-
}
115-
$pattern = $formatter->getPattern();
106+
$pattern = static::createFormatter($locale, $type, $pattern);
116107
} else {
117108
return static::$phpFallbackDatePatterns[$pattern][$type];
118109
}
@@ -350,22 +341,13 @@ public static function convertDatePhpToIcu($pattern)
350341
* @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
351342
* If not given, `Yii::$app->language` will be used.
352343
* @return string The converted date format pattern.
344+
* @throws \Exception
353345
*/
354346
public static function convertDateIcuToJui($pattern, $type = 'date', $locale = null)
355347
{
356348
if (isset(self::$_icuShortFormats[$pattern])) {
357349
if (extension_loaded('intl')) {
358-
if ($locale === null) {
359-
$locale = Yii::$app->language;
360-
}
361-
if ($type === 'date') {
362-
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
363-
} elseif ($type === 'time') {
364-
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
365-
} else {
366-
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
367-
}
368-
$pattern = $formatter->getPattern();
350+
$pattern = static::createFormatter($locale, $type, $pattern);
369351
} else {
370352
return static::$juiFallbackDatePatterns[$pattern][$type];
371353
}
@@ -545,4 +527,32 @@ public static function convertDatePhpToJui($pattern)
545527
'U' => '@', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
546528
]);
547529
}
530+
531+
/**
532+
* Creates a date/time formatter based on the given parameters.
533+
*
534+
* @param string|null $locale The locale to be used. If null, the application's current language will be used.
535+
* @param string $type The type of formatter ('date', 'time', etc.)
536+
* @param string $pattern The pattern for the IntlDateFormatter.
537+
*
538+
* @return string The resulting pattern after formatter creation.
539+
*
540+
* @throws \Exception If the 'intl' extension is not loaded.
541+
*/
542+
private static function createFormatter($locale, $type, $pattern)
543+
{
544+
if ($locale === null) {
545+
$locale = Yii::$app->language;
546+
}
547+
548+
if ($type === 'date') {
549+
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], IntlDateFormatter::NONE);
550+
} elseif ($type === 'time') {
551+
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::NONE, self::$_icuShortFormats[$pattern]);
552+
} else {
553+
$formatter = new IntlDateFormatter($locale, self::$_icuShortFormats[$pattern], self::$_icuShortFormats[$pattern]);
554+
}
555+
556+
return $formatter->getPattern();
557+
}
548558
}

0 commit comments

Comments
 (0)