Bläddra i källkod

Merge remote branch 'vicb/form-padded-choice-list'

* vicb/form-padded-choice-list:
  [Form] Allow for arbitrary keys in PaddedChoiceList
Fabien Potencier 14 år sedan
förälder
incheckning
1ae5ef6849

+ 2 - 3
src/Symfony/Component/Form/Extension/Core/ChoiceList/MonthChoiceList.php

@@ -23,8 +23,7 @@ class MonthChoiceList extends PaddedChoiceList
      */
     public function __construct(\IntlDateFormatter $formatter, array $months)
     {
-        parent::__construct($months, 2, '0', STR_PAD_LEFT);
-
+        parent::__construct(array_combine($months, $months), 2, '0', STR_PAD_LEFT);
         $this->formatter = $formatter;
     }
 
@@ -47,7 +46,7 @@ class MonthChoiceList extends PaddedChoiceList
 
             foreach ($this->choices as $choice => $value) {
                 // It's important to specify the first day of the month here!
-                $this->choices[$choice] = $this->formatter->format(gmmktime(0, 0, 0, $choice, 1));
+                $this->choices[$choice] = $this->formatter->format(gmmktime(0, 0, 0, $value, 1));
             }
 
             // I'd like to clone the formatter above, but then we get a

+ 2 - 5
src/Symfony/Component/Form/Extension/Core/ChoiceList/PaddedChoiceList.php

@@ -52,11 +52,8 @@ class PaddedChoiceList extends ArrayChoiceList
     {
         parent::load();
 
-        $choices = $this->choices;
-        $this->choices = array();
-
-        foreach ($choices as $value) {
-            $this->choices[$value] = str_pad($value, $this->padLength, $this->padString, $this->padType);
+        foreach ($this->choices as $key => $choice) {
+            $this->choices[$key] = str_pad($choice, $this->padLength, $this->padString, $this->padType);
         }
     }
 }

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

@@ -41,7 +41,7 @@ class DateType extends AbstractType
             // Only pass a subset of the options to children
             $yearOptions = array(
                 'choice_list' => new PaddedChoiceList(
-                    $options['years'], 4, '0', STR_PAD_LEFT
+                    array_combine($options['years'], $options['years']), 4, '0', STR_PAD_LEFT
                 ),
             );
             $monthOptions = array(
@@ -51,7 +51,7 @@ class DateType extends AbstractType
             );
             $dayOptions = array(
                 'choice_list' => new PaddedChoiceList(
-                    $options['days'], 2, '0', STR_PAD_LEFT
+                    array_combine($options['days'], $options['days']), 2, '0', STR_PAD_LEFT
                 ),
             );
 

+ 3 - 3
src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

@@ -30,15 +30,15 @@ class TimeType extends AbstractType
 
         if ($options['widget'] === 'choice') {
             $hourOptions['choice_list'] =  new PaddedChoiceList(
-                $options['hours'], 2, '0', STR_PAD_LEFT
+                array_combine($options['hours'], $options['hours']), 2, '0', STR_PAD_LEFT
             );
             $minuteOptions['choice_list'] = new PaddedChoiceList(
-                $options['minutes'], 2, '0', STR_PAD_LEFT
+                array_combine($options['minutes'], $options['minutes']), 2, '0', STR_PAD_LEFT
             );
 
             if ($options['with_seconds']) {
                 $secondOptions['choice_list'] = new PaddedChoiceList(
-                    $options['seconds'], 2, '0', STR_PAD_LEFT
+                    array_combine($options['seconds'], $options['seconds']), 2, '0', STR_PAD_LEFT
                 );
             }
         }

+ 54 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/ChoiceList/PaddedChoiceListTest.php

@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Form\Extension\Core\ChoiceList;
+
+use Symfony\Component\Form\Extension\Core\ChoiceList\PaddedChoiceList;
+
+class PaddedChoiceListTest extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
+     */
+    public function testConstructorExpectsArrayOrClosure()
+    {
+        $list = new PaddedChoiceList('foobar', 3, '-', STR_PAD_RIGHT);
+    }
+
+    public function testPaddingDirections()
+    {
+        $list = new PaddedChoiceList(array('a' => 'C', 'b' => 'D'), 3, '-', STR_PAD_RIGHT);
+        $this->assertSame(array('a' => 'C--', 'b' => 'D--'), $list->getChoices());
+        $list = new PaddedChoiceList(array('a' => 'C', 'b' => 'D'), 3, '-', STR_PAD_LEFT);
+        $this->assertSame(array('a' => '--C', 'b' => '--D'), $list->getChoices());
+        $list = new PaddedChoiceList(array('a' => 'C', 'b' => 'D'), 3, '-', STR_PAD_BOTH);
+        $this->assertSame(array('a' => '-C-', 'b' => '-D-'), $list->getChoices());
+    }
+
+    public function testGetChoicesFromClosure()
+    {
+        $closure = function () { return array('a' => 'C', 'b' => 'D'); };
+        $list = new PaddedChoiceList($closure, 3, '-', STR_PAD_RIGHT);
+
+        $this->assertSame(array('a' => 'C--', 'b' => 'D--'), $list->getChoices());
+    }
+
+    /**
+     * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
+     */
+    public function testClosureShouldReturnArray()
+    {
+        $closure = function () { return 'foobar'; };
+        $list = new PaddedChoiceList($closure, 3, '-', STR_PAD_RIGHT);
+
+        $list->getChoices();
+    }
+}