Просмотр исходного кода

Merge pull request #416 from froschdesign/hotfix/365

Closes #365 - Fix for array to string conversion error in Zend_Validate_...
Rob Allen 11 лет назад
Родитель
Сommit
f63d1372f3
2 измененных файлов с 53 добавлено и 0 удалено
  1. 22 0
      library/Zend/Validate/Abstract.php
  2. 31 0
      tests/Zend/Validate/InArrayTest.php

+ 22 - 0
library/Zend/Validate/Abstract.php

@@ -229,6 +229,8 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
             } else {
                 $value = $value->__toString();
             }
+        } elseif (is_array($value)) {
+            $value = $this->_implodeRecursive($value);
         } else {
             $value = implode((array) $value);
         }
@@ -255,6 +257,26 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
     }
 
     /**
+     * Joins elements of a multidimensional array
+     *
+     * @param array $pieces
+     * @return string
+     */
+    protected function _implodeRecursive(array $pieces)
+    {
+        $values = '';
+        foreach ($pieces as $item) {
+            if (is_array($item)) {
+                $values[] = $this->_implodeRecursive($item);
+            } else {
+                $values[] = $item;
+            }
+        }
+
+        return implode(', ', $values);
+    }
+
+    /**
      * @param  string $messageKey
      * @param  string $value      OPTIONAL
      * @return void

+ 31 - 0
tests/Zend/Validate/InArrayTest.php

@@ -195,4 +195,35 @@ class Zend_Validate_InArrayTest extends PHPUnit_Framework_TestCase
         $validator->setRecursive(true);
         $this->assertTrue($validator->isValid('A'));
     }
+
+    /**
+     * @group GH-365
+     */
+    public function testMultidimensionalArrayNotFound()
+    {
+        $input = array(
+            array('x'),
+            array('y'),
+        );
+        $validator = new Zend_Validate_InArray(array('a'));
+        $this->assertFalse($validator->isValid($input));
+    }
+
+    /**
+     * @group GH-365
+     */
+    public function testErrorMessageWithArrayValue()
+    {
+        $input = array(
+            array('x'),
+            array('y'),
+        );
+        $validator = new Zend_Validate_InArray(array('a'));
+        $validator->isValid($input);
+        $messages  = $validator->getMessages();
+        $this->assertEquals(
+            "'x, y' was not found in the haystack",
+            current($messages)
+        );
+    }
 }