@@ -97,22 +97,13 @@ class BaseFormatConverter
97
97
* @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
98
98
* If not given, `Yii::$app->language` will be used.
99
99
* @return string The converted date format pattern.
100
+ * @throws \Exception
100
101
*/
101
102
public static function convertDateIcuToPhp ($ pattern , $ type = 'date ' , $ locale = null )
102
103
{
103
104
if (isset (self ::$ _icuShortFormats [$ pattern ])) {
104
105
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 );
116
107
} else {
117
108
return static ::$ phpFallbackDatePatterns [$ pattern ][$ type ];
118
109
}
@@ -350,22 +341,13 @@ public static function convertDatePhpToIcu($pattern)
350
341
* @param string|null $locale the locale to use for converting ICU short patterns `short`, `medium`, `long` and `full`.
351
342
* If not given, `Yii::$app->language` will be used.
352
343
* @return string The converted date format pattern.
344
+ * @throws \Exception
353
345
*/
354
346
public static function convertDateIcuToJui ($ pattern , $ type = 'date ' , $ locale = null )
355
347
{
356
348
if (isset (self ::$ _icuShortFormats [$ pattern ])) {
357
349
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 );
369
351
} else {
370
352
return static ::$ juiFallbackDatePatterns [$ pattern ][$ type ];
371
353
}
@@ -545,4 +527,32 @@ public static function convertDatePhpToJui($pattern)
545
527
'U ' => '@ ' , // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
546
528
]);
547
529
}
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
+ }
548
558
}
0 commit comments