Permalink
Browse files

fixes zendframework/zendframework#4974

Conflicts:
	library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php
  • Loading branch information...
dpommeranz authored and stefanotorresi committed Oct 8, 2013
1 parent d547512 commit 468860b253704aa0c4987b5e766d7efeb120e503
@@ -17,6 +17,7 @@
use Zend\InputFilter\InputFilterInterface;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Session\Container;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorChain;
/**
@@ -65,7 +66,7 @@ protected function handlePostRequest(FormInterface $form, $redirect, $redirectTo
$postFiles = $request->getFiles()->toArray();
$postOther = $request->getPost()->toArray();
$post = array_merge_recursive($postOther, $postFiles);
$post = ArrayUtils::merge($postOther, $postFiles, true);
// Fill form with the data first, collections may alter the form/filter structure
$form->setData($post);
@@ -92,11 +93,12 @@ function ($input, $value) {
// Merge and replace previous files with new valid files
$prevFileData = $this->getEmptyUploadData($inputFilter, $previousFiles);
$newFileData = $this->getNonEmptyUploadData($inputFilter, $data);
$postFiles = array_merge_recursive(
$postFiles = ArrayUtils::merge(
$prevFileData ?: array(),
$newFileData ?: array()
$newFileData ?: array(),
true
);
$post = array_merge_recursive($postOther, $postFiles);
$post = ArrayUtils::merge($postOther, $postFiles, true);
// Save form data in session
$container->setExpirationHops(1, array('post', 'errors', 'isValid'));
@@ -245,23 +245,23 @@ public static function iteratorToArray($iterator, $recursive = true)
/**
* Merge two arrays together.
*
* If an integer key exists in both arrays, the value from the second array
* will be appended the the first array. If both values are arrays, they
* are merged together, else the value of the second array overwrites the
* one of the first array.
* If an integer key exists in both arrays and preserveNumericKeys is false, the value
* from the second array will be appended to the first array. If both values are arrays, they
* are merged together, else the value of the second array overwrites the one of the first array.
*
* @param array $a
* @param array $b
* @param bool $preserveNumericKeys
* @return array
*/
public static function merge(array $a, array $b)
public static function merge(array $a, array $b, $preserveNumericKeys = false)
{
foreach ($b as $key => $value) {
if (array_key_exists($key, $a)) {
if (is_int($key)) {
if (is_int($key) && !$preserveNumericKeys) {
$a[] = $value;
} elseif (is_array($value) && is_array($a[$key])) {
$a[$key] = static::merge($a[$key], $value);
$a[$key] = static::merge($a[$key], $value, $preserveNumericKeys);
} else {
$a[$key] = $value;
}
@@ -143,20 +143,67 @@ public static function invalidArrays()
public static function mergeArrays()
{
return array(
'merge-integer-and-string keys' => array(
'merge-integer-and-string-keys' => array(
array(
'foo',
3 => 'bar',
'baz' => 'baz'
3 => 'bar',
'baz' => 'baz',
4 => array(
'a',
1 => 'b',
'c',
),
),
array(
'baz',
4 => array(
'd' => 'd',
),
),
false,
array(
0 => 'foo',
3 => 'bar',
'baz' => 'baz',
4 => 'baz'
4 => array(
'a',
1 => 'b',
'c',
),
5 => 'baz',
6 => array(
'd' => 'd',
),
)
),
'merge-integer-and-string-keys-preserve-numeric' => array(
array(
'foo',
3 => 'bar',
'baz' => 'baz',
4 => array(
'a',
1 => 'b',
'c',
),
),
array(
'baz',
4 => array(
'd' => 'd',
),
),
true,
array(
0 => 'baz',
3 => 'bar',
'baz' => 'baz',
4 => array(
'a',
1 => 'b',
'c',
'd' => 'd',
),
)
),
'merge-arrays-recursively' => array(
@@ -170,6 +217,7 @@ public static function mergeArrays()
'baz'
)
),
false,
array(
'foo' => array(
0 => 'baz',
@@ -186,6 +234,7 @@ public static function mergeArrays()
'foo' => 'baz',
'bar' => 'bat'
),
false,
array(
'foo' => 'baz',
'bar' => 'bat'
@@ -340,9 +389,9 @@ public function testEmptyArrayReturnsFalse()
/**
* @dataProvider mergeArrays
*/
public function testMerge($a, $b, $expected)
public function testMerge($a, $b, $preserveNumericKeys, $expected)
{
$this->assertEquals($expected, ArrayUtils::merge($a, $b));
$this->assertEquals($expected, ArrayUtils::merge($a, $b, $preserveNumericKeys));
}
/**

0 comments on commit 468860b

Please sign in to comment.