Procházet zdrojové kódy

[Locale] refactoring to separate concerns

Eriksen Costa před 14 roky
rodič
revize
348dd7ba24

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/AmPmTransformer.php

@@ -25,7 +25,7 @@ class AmPmTransformer extends Transformer
 
     public function getReverseMatchingRegExp($length)
     {
-        return $this->addNamedCapture("AM|PM", 1);
+        return 'AM|PM';
     }
 
     public function extractDateOptions($matched, $length)

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/DayTransformer.php

@@ -31,7 +31,7 @@ class DayTransformer extends Transformer
             $regExp = '\d{'.$length.'}';
         }
 
-        return $this->addNamedCapture($regExp, 1);
+        return $regExp;
     }
 
     public function extractDateOptions($matched, $length)

+ 19 - 18
src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

@@ -40,23 +40,23 @@ class FullTransformer
         $this->regExp = "/($this->quoteMatch|$implementedCharsMatch|$notImplementedCharsMatch)/";
 
         $this->transformers = array(
-            'M' => new MonthTransformer('M'),
-            'L' => new MonthTransformer('L'),
-            'y' => new YearTransformer('y'),
-            'd' => new DayTransformer('d'),
-            'G' => new EraTransformer('G'),
-            'q' => new QuarterTransformer('q'),
-            'Q' => new QuarterTransformer('Q'),
-            'h' => new Hour1201Transformer('h'),
-            'D' => new DayOfYearTransformer('D'),
-            'E' => new DayOfWeekTransformer('E'),
-            'a' => new AmPmTransformer('a'),
-            'H' => new Hour2400Transformer('H'),
-            'K' => new Hour1200Transformer('K'),
-            'k' => new Hour2401Transformer('k'),
-            'm' => new MinuteTransformer('m'),
-            's' => new SecondTransformer('s'),
-            'z' => new TimeZoneTransformer('z'),
+            'M' => new MonthTransformer(),
+            'L' => new MonthTransformer(),
+            'y' => new YearTransformer(),
+            'd' => new DayTransformer(),
+            'G' => new EraTransformer(),
+            'q' => new QuarterTransformer(),
+            'Q' => new QuarterTransformer(),
+            'h' => new Hour1201Transformer(),
+            'D' => new DayOfYearTransformer(),
+            'E' => new DayOfWeekTransformer(),
+            'a' => new AmPmTransformer(),
+            'H' => new Hour2400Transformer(),
+            'K' => new Hour1200Transformer(),
+            'k' => new Hour2401Transformer(),
+            'm' => new MinuteTransformer(),
+            's' => new SecondTransformer(),
+            'z' => new TimeZoneTransformer(),
         );
     }
 
@@ -110,7 +110,8 @@ class FullTransformer
 
             if (isset($transformers[$transformerIndex])) {
                 $transformer = $transformers[$transformerIndex];
-                return '(' . $transformer->getReverseMatchingRegExp($length) . ')';
+                $captureName = str_repeat($transformerIndex, $length);
+                return "(?P<$captureName>" . $transformer->getReverseMatchingRegExp($length) . ')';
             }
         }, $pattern);
 

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/Hour1200Transformer.php

@@ -27,7 +27,7 @@ class Hour1200Transformer extends Transformer
 
     public function getReverseMatchingRegExp($length)
     {
-        return $this->addNamedCapture('\d{1,2}', $length);
+        return '\d{1,2}';
     }
 
     public function extractDateOptions($matched, $length)

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/Hour1201Transformer.php

@@ -25,7 +25,7 @@ class Hour1201Transformer extends Transformer
 
     public function getReverseMatchingRegExp($length)
     {
-        return $this->addNamedCapture('\d{1,2}', $length);
+        return '\d{1,2}';
     }
 
     public function extractDateOptions($matched, $length)

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/Hour2400Transformer.php

@@ -25,7 +25,7 @@ class Hour2400Transformer extends Transformer
 
     public function getReverseMatchingRegExp($length)
     {
-        return $this->addNamedCapture('\d{1,2}', $length);
+        return '\d{1,2}';
     }
 
     public function extractDateOptions($matched, $length)

+ 1 - 1
src/Symfony/Component/Locale/Stub/DateFormat/Hour2401Transformer.php

@@ -27,7 +27,7 @@ class Hour2401Transformer extends Transformer
 
     public function getReverseMatchingRegExp($length)
     {
-        return $this->addNamedCapture('\d{1,2}', $length);
+        return '\d{1,2}';
     }
 
     public function extractDateOptions($matched, $length)

+ 2 - 4
src/Symfony/Component/Locale/Stub/DateFormat/MonthTransformer.php

@@ -41,7 +41,7 @@ class MonthTransformer extends Transformer
 
     private static $flippedShortMonths = array();
 
-    public function __construct($namedCapture)
+    public function __construct()
     {
         if (0 == count(self::$shortMonths)) {
             self::$shortMonths = array_map(function($month) {
@@ -51,8 +51,6 @@ class MonthTransformer extends Transformer
             self::$flippedMonths = array_flip(self::$months);
             self::$flippedShortMonths = array_flip(self::$shortMonths);
         }
-
-        parent::__construct($namedCapture);
     }
 
     public function format(\DateTime $dateTime, $length)
@@ -93,7 +91,7 @@ class MonthTransformer extends Transformer
                 break;
         }
 
-        return $this->addNamedCapture($regExp, $length);
+        return $regExp;
     }
 
     public function extractDateOptions($matched, $length)

+ 0 - 19
src/Symfony/Component/Locale/Stub/DateFormat/Transformer.php

@@ -18,25 +18,6 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
  */
 abstract class Transformer
 {
-    protected $namedCapture;
-
-    public function __construct($namedCapture)
-    {
-        $this->namedCapture = $namedCapture;
-    }
-
-    public function getNamedCapture()
-    {
-        return $this->namedCapture;
-    }
-
-    public function addNamedCapture($regExp, $lenght = 1)
-    {
-        $namedCapture = $this->getNamedCapture();
-        $namedCapture = str_repeat($namedCapture, $lenght);
-        return '?P<'.$namedCapture.'>' . $regExp;
-    }
-
     abstract public function format(\DateTime $dateTime, $length);
     abstract public function getReverseMatchingRegExp($length);
     abstract public function extractDateOptions($matched, $length);

+ 2 - 2
src/Symfony/Component/Locale/Stub/DateFormat/YearTransformer.php

@@ -35,10 +35,10 @@ class YearTransformer extends Transformer
             $regExp = '\d{4}';
 
             // The named capture in this case will be always y (that equals to yyyy)
-            $length = 1;
+            //$length = 1;
         }
 
-        return $this->addNamedCapture($regExp, $length);
+        return $regExp;
     }
 
     public function extractDateOptions($matched, $length)