Pārlūkot izejas kodu

[Locale] refactor and simplify some parts of StubIntlDateFormatter

Igor Wiedler 14 gadi atpakaļ
vecāks
revīzija
bcca989c41

+ 9 - 11
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -88,13 +88,18 @@ class StubIntlDateFormatter
      * @param  string  $pattern  Optional pattern to use when formatting.
      * @see    http://userguide.icu-project.org/formatparse/datetime
      * @throws MethodArgumentValueNotImplementedException  When $locale different than 'en' is passed
+     * @throws MethodArgumentValueNotImplementedException  When $calendar different than GREGORIAN is passed
      */
-    public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
+    public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
     {
         if ('en' != $locale) {
             throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the \'en\' locale is supported');
         }
 
+        if (self::GREGORIAN != $calendar) {
+            throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported');
+        }
+
         $this->datetype = $datetype;
         $this->timetype = $timetype;
 
@@ -156,15 +161,8 @@ class StubIntlDateFormatter
                     break;
 
                 case 'y':
-                    $matchLengthMap = array(
-                        1   => 'Y',
-                        2   => 'y',
-                        3   => 'Y',
-                        4   => 'Y',
-                    );
-
-                    if (isset($matchLengthMap[$length])) {
-                       return $dateTime->format($matchLengthMap[$length]);
+                    if (2 == $length) {
+                       return $dateTime->format('y');
                     } else {
                         return str_pad($dateTime->format('Y'), $length, '0', STR_PAD_LEFT);
                     }
@@ -502,7 +500,7 @@ class StubIntlDateFormatter
      * @see    http://userguide.icu-project.org/formatparse/datetime
      * @throws MethodArgumentValueNotImplementedException  When $locale different than 'en' is passed
      */
-    static public function create($locale, $datetype, $timetype, $timezone = null, $calendar = null, $pattern = null)
+    static public function create($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
     {
         return new self($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
     }

+ 12 - 7
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -38,7 +38,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
     */
     public function testFormatStub($pattern, $timestamp, $expected)
     {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
+        $formatter = $this->createStubFormatter($pattern);
         $this->assertSame($expected, $formatter->format($timestamp));
     }
 
@@ -48,7 +48,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
     public function testFormatIntl($pattern, $timestamp, $expected)
     {
         $this->skipIfIntlExtensionIsNotLoaded();
-        $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
+        $formatter = $this->createIntlFormatter($pattern);
         $this->assertSame($expected, $formatter->format($timestamp));
     }
 
@@ -368,7 +368,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
 
     public function testGetPattern()
     {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, StubIntlDateFormatter::GREGORIAN, 'UTC', 'yyyy-MM-dd');
+        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'UTC', StubIntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
         $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
     }
 
@@ -459,7 +459,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
 
     public function testSetTimeZoneIdStub()
     {
-        $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC');
+        $formatter = $this->createStubFormatter();
         $this->assertEquals('UTC', $formatter->getTimeZoneId());
 
         $formatter->setTimeZoneId('Europe/Zurich');
@@ -469,7 +469,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
     public function testSetTimeZoneIdIntl()
     {
         $this->skipIfIntlExtensionIsNotLoaded();
-        $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');
+        $formatter = $this->createIntlFormatter();
         $this->assertEquals('UTC', $formatter->getTimeZoneId());
 
         $formatter->setTimeZoneId('Europe/Zurich');
@@ -482,8 +482,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase
         $this->assertInstanceOf('Symfony\Component\Locale\Stub\StubIntlDateFormatter', $formatter);
     }
 
-    protected function createStubFormatter()
+    protected function createStubFormatter($pattern = null)
+    {
+        return new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
+    }
+
+    protected function createIntlFormatter($pattern = null)
     {
-        return new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
+        return new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
     }
 }