فهرست منبع

Merge branch 'master' of http://200.50.168.30:10080/VendorSoftware/symfony

root 8 سال پیش
والد
کامیت
7b0a08b9c5
1فایلهای تغییر یافته به همراه63 افزوده شده و 9 حذف شده
  1. 63 9
      src/Symfony/Component/Form/Extension/Core/Type/DateType.php

+ 63 - 9
src/Symfony/Component/Form/Extension/Core/Type/DateType.php

@@ -77,23 +77,17 @@ class DateType extends AbstractType
 
                 // Only pass a subset of the options to children
                 $yearOptions = array(
-                    'choice_list' => new PaddedChoiceList(
-                        array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
-                    ),
+                    'choices' => $this->formatTimestamps($formatter, '/y+/', $this->listYears($options['years'])),
                     'empty_value' => $options['empty_value']['year'],
                     'required' => $options['required'],
                 );
                 $monthOptions = array(
-                    'choice_list' => new MonthChoiceList(
-                        $formatter, $options['months']
-                    ),
+                    'choices' => $this->formatTimestamps($formatter, '/[M|L]+/', $this->listMonths($options['months'])),
                     'empty_value' => $options['empty_value']['month'],
                     'required' => $options['required'],
                 );
                 $dayOptions = array(
-                    'choice_list' => new PaddedChoiceList(
-                        array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
-                    ),
+                    'choices' => $this->formatTimestamps($formatter, '/d+/', $this->listDays($options['days'])),
                     'empty_value' => $options['empty_value']['day'],
                     'required' => $options['required'],
                 );
@@ -208,4 +202,64 @@ class DateType extends AbstractType
     {
         return 'date';
     }
+    
+    private function formatTimestamps(\IntlDateFormatter $formatter, $regex, array $timestamps)
+    {
+        $pattern = $formatter->getPattern();
+        $timezone = $formatter->getTimezoneId();
+        // Use it, if exists.
+        $setTimeZone = method_exists($formatter, 'setTimeZone');
+        if ($setTimeZone) {
+            $formatter->setTimeZone('UTC');
+        } else {
+            $formatter->setTimeZoneId('UTC');
+        }
+        if (preg_match($regex, $pattern, $matches)) {
+            $formatter->setPattern($matches[0]);
+            foreach ($timestamps as $key => $timestamp) {
+                $timestamps[$key] = $formatter->format($timestamp);
+            }
+            // I'd like to clone the formatter above, but then we get a
+            // segmentation fault, so let's restore the old state instead
+            $formatter->setPattern($pattern);
+        }
+        if ($setTimeZone) {
+            $formatter->setTimeZone($timezone);
+        } else {
+            $formatter->setTimeZoneId($timezone);
+        }
+        
+        return $timestamps;
+    }
+
+    private function listYears(array $years)
+    {
+        $result = array();
+        foreach ($years as $year) {
+            $result[$year] = gmmktime(0, 0, 0, 6, 15, $year);
+        }
+        
+        return $result;
+    }
+
+    private function listMonths(array $months)
+    {
+        $result = array();
+        foreach ($months as $month) {
+            $result[$month] = gmmktime(0, 0, 0, $month, 15);
+        }
+        
+        return $result;
+    }
+
+    private function listDays(array $days)
+    {
+        $result = array();
+        foreach ($days as $day) {
+            $result[$day] = gmmktime(0, 0, 0, 5, $day);
+        }
+        
+        return $result;
+    }
+
 }