Browse Source

[Form] Fixed order of input and output timezone to a more natural one

Bernhard Schussek 14 năm trước cách đây
mục cha
commit
08a09240f6

+ 10 - 1
src/Symfony/Component/Form/DataTransformer/BaseDateTimeTransformer.php

@@ -13,7 +13,6 @@ namespace Symfony\Component\Form\DataTransformer;
 
 
 abstract class BaseDateTimeTransformer implements DataTransformerInterface
 abstract class BaseDateTimeTransformer implements DataTransformerInterface
 {
 {
-    
     protected static $formats = array(
     protected static $formats = array(
         \IntlDateFormatter::NONE,
         \IntlDateFormatter::NONE,
         \IntlDateFormatter::FULL,
         \IntlDateFormatter::FULL,
@@ -21,4 +20,14 @@ abstract class BaseDateTimeTransformer implements DataTransformerInterface
         \IntlDateFormatter::MEDIUM,
         \IntlDateFormatter::MEDIUM,
         \IntlDateFormatter::SHORT,
         \IntlDateFormatter::SHORT,
     );
     );
+
+    protected $inputTimezone;
+
+    protected $outputTimezone;
+
+    public function __construct($inputTimezone = null, $outputTimezone = null)
+    {
+        $this->inputTimezone = $inputTimezone ?: date_default_timezone_get();
+        $this->outputTimezone = $outputTimezone ?: date_default_timezone_get();
+    }
 }
 }

+ 2 - 15
src/Symfony/Component/Form/DataTransformer/DateTimeToArrayTransformer.php

@@ -27,31 +27,18 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
  */
  */
 class DateTimeToArrayTransformer extends BaseDateTimeTransformer
 class DateTimeToArrayTransformer extends BaseDateTimeTransformer
 {
 {
-
-    private $inputTimezone;
-
-    private $outputTimezone;
-
     private $pad;
     private $pad;
 
 
     private $fields;
     private $fields;
 
 
-    public function __construct($outputTimezone = null, $inputTimezone = null, $fields = null, $pad = false)
+    public function __construct($inputTimezone = null, $outputTimezone = null, $fields = null, $pad = false)
     {
     {
-        if (is_null($inputTimezone)) {
-            $inputTimezone = date_default_timezone_get();
-        }
-
-        if (is_null($outputTimezone)) {
-            $outputTimezone = date_default_timezone_get();
-        }
+        parent::__construct($inputTimezone, $outputTimezone);
 
 
         if (is_null($fields)) {
         if (is_null($fields)) {
             $fields = array('year', 'month', 'day', 'hour', 'minute', 'second');
             $fields = array('year', 'month', 'day', 'hour', 'minute', 'second');
         }
         }
 
 
-        $this->inputTimezone = $inputTimezone;
-        $this->outputTimezone = $outputTimezone;
         $this->fields = $fields;
         $this->fields = $fields;
         $this->pad =$pad;
         $this->pad =$pad;
     }
     }

+ 3 - 16
src/Symfony/Component/Form/DataTransformer/DateTimeToLocalizedStringTransformer.php

@@ -27,24 +27,13 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
  */
  */
 class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
 class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
 {
 {
-
-    private $inputTimezone;
-
-    private $outputTimezone;
-    
     private $dateFormat;
     private $dateFormat;
 
 
     private $timeFormat;
     private $timeFormat;
 
 
-    public function __construct($outputTimezone = null, $inputTimezone = null, $dateFormat = null, $timeFormat = null)
+    public function __construct($inputTimezone = null, $outputTimezone = null, $dateFormat = null, $timeFormat = null)
     {
     {
-        if (is_null($inputTimezone)) {
-            $inputTimezone = date_default_timezone_get();
-        }
-
-        if (is_null($outputTimezone)) {
-            $outputTimezone = date_default_timezone_get();
-        }
+        parent::__construct($inputTimezone, $outputTimezone);
 
 
         if (is_null($dateFormat)) {
         if (is_null($dateFormat)) {
             $dateFormat = \IntlDateFormatter::MEDIUM;
             $dateFormat = \IntlDateFormatter::MEDIUM;
@@ -61,9 +50,7 @@ class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
         if (!in_array($timeFormat, self::$formats, true)) {
         if (!in_array($timeFormat, self::$formats, true)) {
             throw new \InvalidArgumentException(sprintf('The value $timeFormat is expected to be one of "%s". Is "%s"', implode('", "', self::$formats), $timeFormat));
             throw new \InvalidArgumentException(sprintf('The value $timeFormat is expected to be one of "%s". Is "%s"', implode('", "', self::$formats), $timeFormat));
         }
         }
-        
-        $this->inputTimezone = $inputTimezone;
-        $this->outputTimezone = $outputTimezone;
+
         $this->dateFormat = $dateFormat;
         $this->dateFormat = $dateFormat;
         $this->timeFormat = $timeFormat;
         $this->timeFormat = $timeFormat;
     }
     }

+ 4 - 17
src/Symfony/Component/Form/DataTransformer/DateTimeToStringTransformer.php

@@ -19,28 +19,15 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  */
  */
-class DateTimeToStringTransformer implements DataTransformerInterface
+class DateTimeToStringTransformer extends BaseDateTimeTransformer
 {
 {
-
-    private $inputTimezone;
-
-    private $outputTimezone;
-
     private $format;
     private $format;
-    
-    public function __construct($outputTimezone = null, $format = 'Y-m-d H:i:s', $inputTimezone = null)
+
+    public function __construct($inputTimezone = null, $outputTimezone = null, $format = 'Y-m-d H:i:s')
     {
     {
-        if (is_null($inputTimezone)) {
-            $inputTimezone = date_default_timezone_get();
-        }
-        
-        if (is_null($outputTimezone)) {
-            $outputTimezone = date_default_timezone_get();
-        }
+        parent::__construct($inputTimezone, $outputTimezone);
 
 
         $this->format = $format;
         $this->format = $format;
-        $this->inputTimezone = $inputTimezone;
-        $this->outputTimezone = $outputTimezone;
     }
     }
 
 
     /**
     /**

+ 1 - 19
src/Symfony/Component/Form/DataTransformer/DateTimeToTimestampTransformer.php

@@ -19,26 +19,8 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  */
  */
-class DateTimeToTimestampTransformer implements DataTransformerInterface
+class DateTimeToTimestampTransformer extends BaseDateTimeTransformer
 {
 {
-    private $inputTimezone;
-
-    private $outputTimezone;
-
-    public function __construct($outputTimezone = null, $inputTimezone = null)
-    {
-        if (is_null($inputTimezone)) {
-            $inputTimezone = date_default_timezone_get();
-        }
-
-        if (is_null($outputTimezone)) {
-            $outputTimezone = date_default_timezone_get();
-        }
-
-        $this->inputTimezone = $inputTimezone;
-        $this->outputTimezone = $outputTimezone;
-    }
-
     /**
     /**
      * Transforms a DateTime object into a timestamp in the configured timezone
      * Transforms a DateTime object into a timestamp in the configured timezone
      *
      *

+ 4 - 4
src/Symfony/Component/Form/Type/DateTimeType.php

@@ -69,7 +69,7 @@ class DateTimeType extends AbstractType
         }
         }
 
 
         $builder->setClientTransformer(new DataTransformerChain(array(
         $builder->setClientTransformer(new DataTransformerChain(array(
-                new DateTimeToArrayTransformer($options['user_timezone'], $options['data_timezone'], $parts),
+                new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], $parts),
                 new ArrayToPartsTransformer(array(
                 new ArrayToPartsTransformer(array(
                     'date' => array('year', 'month', 'day'),
                     'date' => array('year', 'month', 'day'),
                     'time' => $timeParts,
                     'time' => $timeParts,
@@ -80,7 +80,7 @@ class DateTimeType extends AbstractType
 
 
         if ($options['input'] === 'string') {
         if ($options['input'] === 'string') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
-                new DateTimeToStringTransformer($options['data_timezone'], 'Y-m-d H:i:s', $options['data_timezone'])
+                new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d H:i:s')
             ));
             ));
         } else if ($options['input'] === 'timestamp') {
         } else if ($options['input'] === 'timestamp') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
@@ -99,8 +99,8 @@ class DateTimeType extends AbstractType
             'template' => 'datetime',
             'template' => 'datetime',
             'input' => 'datetime',
             'input' => 'datetime',
             'with_seconds' => false,
             'with_seconds' => false,
-            'data_timezone' => date_default_timezone_get(),
-            'user_timezone' => date_default_timezone_get(),
+            'data_timezone' => null,
+            'user_timezone' => null,
             // Don't modify \DateTime classes by reference, we treat
             // Don't modify \DateTime classes by reference, we treat
             // them like immutable value objects
             // them like immutable value objects
             'by_reference' => false,
             'by_reference' => false,

+ 5 - 5
src/Symfony/Component/Form/Type/DateType.php

@@ -32,7 +32,7 @@ class DateType extends AbstractType
         );
         );
 
 
         if ($options['widget'] === 'text') {
         if ($options['widget'] === 'text') {
-            $builder->setClientTransformer(new DateTimeToLocalizedStringTransformer($options['user_timezone'], $options['data_timezone'], $options['format'], \IntlDateFormatter::NONE));
+            $builder->setClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $options['format'], \IntlDateFormatter::NONE));
         } else {
         } else {
             // Only pass a subset of the options to children
             // Only pass a subset of the options to children
             $yearOptions = array(
             $yearOptions = array(
@@ -54,13 +54,13 @@ class DateType extends AbstractType
             $builder->add('year', 'choice', $yearOptions)
             $builder->add('year', 'choice', $yearOptions)
                 ->add('month', 'choice', $monthOptions)
                 ->add('month', 'choice', $monthOptions)
                 ->add('day', 'choice', $dayOptions)
                 ->add('day', 'choice', $dayOptions)
-                ->setClientTransformer(new DateTimeToArrayTransformer($options['user_timezone'], $options['data_timezone'], array('year', 'month', 'day')))
+                ->setClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], array('year', 'month', 'day')))
                 ->addRendererPlugin(new DatePatternPlugin($formatter));
                 ->addRendererPlugin(new DatePatternPlugin($formatter));
         }
         }
 
 
         if ($options['input'] === 'string') {
         if ($options['input'] === 'string') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
-                new DateTimeToStringTransformer($options['data_timezone'], 'Y-m-d', $options['data_timezone'])
+                new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d')
             ));
             ));
         } else if ($options['input'] === 'timestamp') {
         } else if ($options['input'] === 'timestamp') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
@@ -86,8 +86,8 @@ class DateType extends AbstractType
             'input' => 'datetime',
             'input' => 'datetime',
             'pattern' => null,
             'pattern' => null,
             'format' => \IntlDateFormatter::MEDIUM,
             'format' => \IntlDateFormatter::MEDIUM,
-            'data_timezone' => date_default_timezone_get(),
-            'user_timezone' => date_default_timezone_get(),
+            'data_timezone' => null,
+            'user_timezone' => null,
             'csrf_protection' => false,
             'csrf_protection' => false,
             // Don't modify \DateTime classes by reference, we treat
             // Don't modify \DateTime classes by reference, we treat
             // them like immutable value objects
             // them like immutable value objects

+ 4 - 4
src/Symfony/Component/Form/Type/TimeType.php

@@ -51,7 +51,7 @@ class TimeType extends AbstractType
 
 
         if ($options['input'] === 'string') {
         if ($options['input'] === 'string') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
-                new DateTimeToStringTransformer($options['data_timezone'], 'H:i:s', $options['data_timezone'])
+                new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'H:i:s')
             ));
             ));
         } else if ($options['input'] === 'timestamp') {
         } else if ($options['input'] === 'timestamp') {
             $builder->setNormTransformer(new ReversedTransformer(
             $builder->setNormTransformer(new ReversedTransformer(
@@ -64,7 +64,7 @@ class TimeType extends AbstractType
         }
         }
 
 
         $builder
         $builder
-            ->setClientTransformer(new DateTimeToArrayTransformer($options['user_timezone'], $options['data_timezone'], $parts, $options['widget'] === 'text'))
+            ->setClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], $parts, $options['widget'] === 'text'))
             ->setRendererVar('widget', $options['widget'])
             ->setRendererVar('widget', $options['widget'])
             ->setRendererVar('with_seconds', $options['with_seconds']);
             ->setRendererVar('with_seconds', $options['with_seconds']);
     }
     }
@@ -80,8 +80,8 @@ class TimeType extends AbstractType
             'input' => 'datetime',
             'input' => 'datetime',
             'with_seconds' => false,
             'with_seconds' => false,
             'pattern' => null,
             'pattern' => null,
-            'data_timezone' => date_default_timezone_get(),
-            'user_timezone' => date_default_timezone_get(),
+            'data_timezone' => null,
+            'user_timezone' => null,
             'csrf_protection' => false,
             'csrf_protection' => false,
             // Don't modify \DateTime classes by reference, we treat
             // Don't modify \DateTime classes by reference, we treat
             // them like immutable value objects
             // them like immutable value objects

+ 3 - 3
tests/Symfony/Tests/Component/Form/DataTransformer/DateTimeToArrayTransformerTest.php

@@ -100,7 +100,7 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
 
 
     public function testTransform_differentTimezones()
     public function testTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToArrayTransformer('Asia/Hong_Kong', 'America/New_York');
+        $transformer = new DateTimeToArrayTransformer('America/New_York', 'Asia/Hong_Kong');
 
 
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
 
 
@@ -273,7 +273,7 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransform_differentTimezones()
     public function testReverseTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToArrayTransformer('Asia/Hong_Kong', 'America/New_York');
+        $transformer = new DateTimeToArrayTransformer('America/New_York', 'Asia/Hong_Kong');
 
 
         $input = array(
         $input = array(
             'year' => '2010',
             'year' => '2010',
@@ -292,7 +292,7 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransformToDifferentTimezone()
     public function testReverseTransformToDifferentTimezone()
     {
     {
-        $transformer = new DateTimeToArrayTransformer('UTC', 'Asia/Hong_Kong');
+        $transformer = new DateTimeToArrayTransformer('Asia/Hong_Kong', 'UTC');
 
 
         $input = array(
         $input = array(
             'year' => '2010',
             'year' => '2010',

+ 2 - 2
tests/Symfony/Tests/Component/Form/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

@@ -113,7 +113,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
 
 
     public function testTransform_differentTimezones()
     public function testTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToLocalizedStringTransformer('Asia/Hong_Kong', 'America/New_York');
+        $transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
 
 
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
 
 
@@ -210,7 +210,7 @@ class DateTimeToLocalizedStringTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransform_differentTimezones()
     public function testReverseTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToLocalizedStringTransformer('Asia/Hong_Kong', 'America/New_York');
+        $transformer = new DateTimeToLocalizedStringTransformer('America/New_York', 'Asia/Hong_Kong');
 
 
         $dateTime = new \DateTime('2010-02-03 04:05:00 Asia/Hong_Kong');
         $dateTime = new \DateTime('2010-02-03 04:05:00 Asia/Hong_Kong');
         $dateTime->setTimezone(new \DateTimeZone('America/New_York'));
         $dateTime->setTimezone(new \DateTimeZone('America/New_York'));

+ 4 - 4
tests/Symfony/Tests/Component/Form/DataTransformer/DateTimeToStringTransformerTest.php

@@ -19,7 +19,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
 {
 {
     public function testTransform()
     public function testTransform()
     {
     {
-        $transformer = new DateTimeToStringTransformer('UTC', 'Y-m-d H:i:s', 'UTC');
+        $transformer = new DateTimeToStringTransformer('UTC', 'UTC', 'Y-m-d H:i:s');
 
 
         $input = new \DateTime('2010-02-03 04:05:06 UTC');
         $input = new \DateTime('2010-02-03 04:05:06 UTC');
         $output = clone $input;
         $output = clone $input;
@@ -38,7 +38,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
 
 
     public function testTransform_differentTimezones()
     public function testTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToStringTransformer('America/New_York', 'Y-m-d H:i:s', 'Asia/Hong_Kong');
+        $transformer = new DateTimeToStringTransformer('Asia/Hong_Kong', 'America/New_York', 'Y-m-d H:i:s');
 
 
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $output = $input->format('Y-m-d H:i:s');
         $output = $input->format('Y-m-d H:i:s');
@@ -58,7 +58,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransform()
     public function testReverseTransform()
     {
     {
-        $reverseTransformer = new DateTimeToStringTransformer('UTC', 'Y-m-d H:i:s', 'UTC');
+        $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', 'Y-m-d H:i:s');
 
 
         $output = new \DateTime('2010-02-03 04:05:06 UTC');
         $output = new \DateTime('2010-02-03 04:05:06 UTC');
         $input = $output->format('Y-m-d H:i:s');
         $input = $output->format('Y-m-d H:i:s');
@@ -75,7 +75,7 @@ class DateTimeToStringTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransform_differentTimezones()
     public function testReverseTransform_differentTimezones()
     {
     {
-        $reverseTransformer = new DateTimeToStringTransformer('Asia/Hong_Kong', 'Y-m-d H:i:s', 'America/New_York');
+        $reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s');
 
 
         $output = new \DateTime('2010-02-03 04:05:06 Asia/Hong_Kong');
         $output = new \DateTime('2010-02-03 04:05:06 Asia/Hong_Kong');
         $input = $output->format('Y-m-d H:i:s');
         $input = $output->format('Y-m-d H:i:s');

+ 2 - 2
tests/Symfony/Tests/Component/Form/DataTransformer/DateTimeToTimestampTransformerTest.php

@@ -36,7 +36,7 @@ class DateTimeToTimestampTransformerTest extends DateTimeTestCase
 
 
     public function testTransform_differentTimezones()
     public function testTransform_differentTimezones()
     {
     {
-        $transformer = new DateTimeToTimestampTransformer('America/New_York', 'Asia/Hong_Kong');
+        $transformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York');
 
 
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $input = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $output = $input->format('U');
         $output = $input->format('U');
@@ -86,7 +86,7 @@ class DateTimeToTimestampTransformerTest extends DateTimeTestCase
 
 
     public function testReverseTransform_differentTimezones()
     public function testReverseTransform_differentTimezones()
     {
     {
-        $reverseTransformer = new DateTimeToTimestampTransformer('America/New_York', 'Asia/Hong_Kong');
+        $reverseTransformer = new DateTimeToTimestampTransformer('Asia/Hong_Kong', 'America/New_York');
 
 
         $output = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $output = new \DateTime('2010-02-03 04:05:06 America/New_York');
         $input = $output->format('U');
         $input = $output->format('U');