Ver Fonte

[Locale] refactored hour manipulation

Eriksen Costa há 14 anos atrás
pai
commit
e78c6c2b1a

+ 4 - 25
src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

@@ -181,36 +181,15 @@ class FullTransformer
         $minute   = $datetime['minute'];
         $second   = $datetime['second'];
         $marker   = $datetime['marker'];
-        $hourType = $datetime['hourType'];
+        $hourInstance = $datetime['hourInstance'];
 
         // If month is false, return immediately
         if (false === $month) {
             return false;
         }
 
-        // If the AM/PM marker is AM or null, the hour is 12 (1-12) and the capture was 'h' or 'hh', the hour is 0
-        if ('1201' === $hourType && 'PM' !== $marker && 12 === $hour) {
-            $hour = 0;
-        }
-
-        // If PM and hour is not 12 (1-12), sum 12 hour
-        if ('1201' === $hourType && 'PM' === $marker && 12 !== $hour) {
-            $hour = $hour + 12;
-        }
-
-        // If PM, sum 12 hours when 12 hour (0-11)
-        if ('1200' === $hourType && 'PM' === $marker) {
-            $hour = $hour + 12;
-        }
-
-        // If 24 hours (0-23 or 1-24) and marker is set, hour is 0
-        if (('2400' === $hourType || '2401' === $hourType) && null !== $marker) {
-            $hour = 0;
-        }
-
-        // If 24 hours (1-24) and hour is 24, hour is 0
-        if ('2401' === $hourType && 24 === $hour) {
-            $hour = 0;
+        if ($hourInstance instanceof HourTransformer) {
+            $hour = $hourInstance->getMktimeHour($hour, $marker);
         }
 
         // Set the timezone
@@ -235,7 +214,7 @@ class FullTransformer
             'minute'   => isset($datetime['minute']) ? $datetime['minute'] : 0,
             'second'   => isset($datetime['second']) ? $datetime['second'] : 0,
             'marker'   => isset($datetime['marker']) ? $datetime['marker'] : null,
-            'hourType' => isset($datetime['hourType']) ? $datetime['hourType'] : null,
+            'hourInstance' => isset($datetime['hourInstance']) ? $datetime['hourInstance'] : null,
         );
     }
 }

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

@@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
  *
  * @author Igor Wiedler <igor@wiedler.ch>
  */
-class Hour1200Transformer extends Transformer
+class Hour1200Transformer extends HourTransformer
 {
     public function format(\DateTime $dateTime, $length)
     {
@@ -25,6 +25,15 @@ class Hour1200Transformer extends Transformer
         return $this->padLeft($hourOfDay, $length);
     }
 
+    public function getMktimeHour($hour, $marker = null)
+    {
+        if ('PM' === $marker) {
+            $hour += 12;
+        }
+
+        return $hour;
+    }
+
     public function getReverseMatchingRegExp($length)
     {
         return '\d{1,2}';
@@ -34,7 +43,7 @@ class Hour1200Transformer extends Transformer
     {
         return array(
             'hour' => (int) $matched,
-            'hourType' => '1200'
+            'hourInstance' => $this
         );
     }
 }

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

@@ -16,13 +16,25 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
  *
  * @author Igor Wiedler <igor@wiedler.ch>
  */
-class Hour1201Transformer extends Transformer
+class Hour1201Transformer extends HourTransformer
 {
     public function format(\DateTime $dateTime, $length)
     {
         return $this->padLeft($dateTime->format('g'), $length);
     }
 
+    public function getMktimeHour($hour, $marker = null)
+    {
+        if ('PM' !== $marker && 12 === $hour) {
+            $hour = 0;
+        } elseif ('PM' === $marker && 12 !== $hour) {
+            // If PM and hour is not 12 (1-12), sum 12 hour
+            $hour = $hour + 12;
+        }
+
+        return $hour;
+    }
+
     public function getReverseMatchingRegExp($length)
     {
         return '\d{1,2}';
@@ -32,7 +44,7 @@ class Hour1201Transformer extends Transformer
     {
         return array(
             'hour' => (int) $matched,
-            'hourType' => '1201'
+            'hourInstance' => $this
         );
     }
 }

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

@@ -16,13 +16,22 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
  *
  * @author Igor Wiedler <igor@wiedler.ch>
  */
-class Hour2400Transformer extends Transformer
+class Hour2400Transformer extends HourTransformer
 {
     public function format(\DateTime $dateTime, $length)
     {
         return $this->padLeft($dateTime->format('G'), $length);
     }
 
+    public function getMktimeHour($hour, $marker = null)
+    {
+        if (null !== $marker) {
+            $hour = 0;
+        }
+
+        return $hour;
+    }
+
     public function getReverseMatchingRegExp($length)
     {
         return '\d{1,2}';
@@ -32,7 +41,7 @@ class Hour2400Transformer extends Transformer
     {
         return array(
             'hour' => (int) $matched,
-            'hourType' => '2400'
+            'hourInstance' => $this
         );
     }
 }

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

@@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
  *
  * @author Igor Wiedler <igor@wiedler.ch>
  */
-class Hour2401Transformer extends Transformer
+class Hour2401Transformer extends HourTransformer
 {
     public function format(\DateTime $dateTime, $length)
     {
@@ -25,6 +25,15 @@ class Hour2401Transformer extends Transformer
         return $this->padLeft($hourOfDay, $length);
     }
 
+    public function getMktimeHour($hour, $marker = null)
+    {
+        if (null !== $marker || 24 === $hour) {
+            $hour = 0;
+        }
+
+        return $hour;
+    }
+
     public function getReverseMatchingRegExp($length)
     {
         return '\d{1,2}';
@@ -34,7 +43,7 @@ class Hour2401Transformer extends Transformer
     {
         return array(
             'hour' => (int) $matched,
-            'hourType' => '2401'
+            'hourInstance' => $this
         );
     }
 }

+ 20 - 0
src/Symfony/Component/Locale/Stub/DateFormat/HourTransformer.php

@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Locale\Stub\DateFormat;
+
+/**
+ * Base class for hour transformers.
+ */
+abstract class HourTransformer extends Transformer
+{
+    abstract public function getMktimeHour($hour, $marker = null);
+}