Explorar o código

ZF-10016: made Zend_Form setDefaults() accept ArrayAccess

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22461 44c647ce-9c0f-0410-b52a-842ac1e357ba
alab %!s(int64=15) %!d(string=hai) anos
pai
achega
6e68493f23
Modificáronse 2 ficheiros con 27 adicións e 4 borrados
  1. 9 4
      library/Zend/Form.php
  2. 18 0
      tests/Zend/Form/FormTest.php

+ 9 - 4
library/Zend/Form.php

@@ -1248,11 +1248,16 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
      *
      * Sets values for all elements specified in the array of $defaults.
      *
-     * @param  array $defaults
+     * @param mixed $defaults array|ArrayAccess
      * @return Zend_Form
      */
-    public function setDefaults(array $defaults)
+    public function setDefaults($defaults)
     {
+        if (!is_array($defaults) && !($defaults instanceof ArrayAccess)) {
+            throw new Zend_Form_Exception(__METHOD__ .
+                                          ' expects param $defaults to be an array or an ArrayAccess Object');
+        }
+
         $eBelongTo = null;
 
         if ($this->isArray()) {
@@ -2017,10 +2022,10 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
      *
      * Proxies to {@link setDefaults()}
      *
-     * @param  array $values
+     * @param mixed $values array|ArrayAccess
      * @return Zend_Form
      */
-    public function populate(array $values)
+    public function populate($values)
     {
         return $this->setDefaults($values);
     }

+ 18 - 0
tests/Zend/Form/FormTest.php

@@ -4310,6 +4310,22 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-10016
+     */
+    public function testSetDefaultsAcceptsArrayAccessObject()
+    {
+        $val = new ArrayObject(array('foo' => 'val'));
+        $this->assertTrue($val instanceof ArrayAccess);
+
+        try {
+            $this->form->addElement('text', 'foo')
+                       ->setDefaults($val);
+        } catch (Zend_Form_Exception $e) {}
+        
+        $this->assertSame('val', $this->form->foo->getValue());
+    }
+
+    /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete
      *
      * @link   http://framework.zend.com/issues/browse/ZF-2794
@@ -4332,6 +4348,8 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
     {
         $this->assertSame($this->form, $this->form->loadDefaultDecorators());
     }
+
+
 }
 
 class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup