Selaa lähdekoodia

[Locale] add support for escaping, give specifics on implementation used in tests

Igor Wiedler 14 vuotta sitten
vanhempi
commit
8c1146974f

+ 7 - 1
src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

@@ -40,10 +40,16 @@ class StubIntlDateFormatter
 
     public function format($timestamp)
     {
+        $regExp = "/('(M+|y+|d+|[^Myd])|M+|y+|d+)/";
+
         $callback = function($matches) use ($timestamp) {
             $pattern = $matches[0];
             $length = strlen($pattern);
 
+            if ("'" === $pattern[0]) {
+                return substr($pattern, 1);
+            }
+
             switch ($pattern[0]) {
                 case 'M':
                     $matchLengthMap = array(
@@ -83,7 +89,7 @@ class StubIntlDateFormatter
             }  
         };
 
-        $formatted = preg_replace_callback('/(M+|y+|d+)/', $callback, $this->getPattern());
+        $formatted = preg_replace_callback($regExp, $callback, $this->getPattern());
 
         return $formatted;
     }

+ 10 - 2
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -21,6 +21,14 @@ class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
         return array(
             array('y-M-d', 0, '1970-1-1'),
 
+            /* escaping */
+            array("'M", 0, 'M'),
+            array("'y-'M-'d", 0, 'y-M-d'),
+            array("'yy", 0, 'yy'),
+            array("'''yy", 0, "'yy"),
+            array("''y", 0, "'1970"),
+            array("''yy", 0, "'70"),
+
             /* months */
             array('M', 0, '1'),
             array('MM', 0, '01'),
@@ -65,11 +73,11 @@ class StubIntlDateFormatterTest extends \PHPUnit_Framework_TestCase
     public function testFormat($pattern, $timestamp, $expected)
     {
         $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
-        $this->assertEquals($expected, $formatter->format($timestamp));
+        $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
 
         if (extension_loaded('intl')) {
             $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
-            $this->assertEquals($expected, $formatter->format($timestamp));
+            $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
         }
     }