Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit 468860b

Browse files
dpommeranzStefano Torresi
authored andcommitted
fixes #4974
Conflicts: library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php
1 parent d547512 commit 468860b

3 files changed

Lines changed: 68 additions & 17 deletions

File tree

library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Zend\InputFilter\InputFilterInterface;
1818
use Zend\Mvc\Exception\RuntimeException;
1919
use Zend\Session\Container;
20+
use Zend\Stdlib\ArrayUtils;
2021
use Zend\Validator\ValidatorChain;
2122

2223
/**
@@ -65,7 +66,7 @@ protected function handlePostRequest(FormInterface $form, $redirect, $redirectTo
6566

6667
$postFiles = $request->getFiles()->toArray();
6768
$postOther = $request->getPost()->toArray();
68-
$post = array_merge_recursive($postOther, $postFiles);
69+
$post = ArrayUtils::merge($postOther, $postFiles, true);
6970

7071
// Fill form with the data first, collections may alter the form/filter structure
7172
$form->setData($post);
@@ -92,11 +93,12 @@ function ($input, $value) {
9293
// Merge and replace previous files with new valid files
9394
$prevFileData = $this->getEmptyUploadData($inputFilter, $previousFiles);
9495
$newFileData = $this->getNonEmptyUploadData($inputFilter, $data);
95-
$postFiles = array_merge_recursive(
96+
$postFiles = ArrayUtils::merge(
9697
$prevFileData ?: array(),
97-
$newFileData ?: array()
98+
$newFileData ?: array(),
99+
true
98100
);
99-
$post = array_merge_recursive($postOther, $postFiles);
101+
$post = ArrayUtils::merge($postOther, $postFiles, true);
100102

101103
// Save form data in session
102104
$container->setExpirationHops(1, array('post', 'errors', 'isValid'));

library/Zend/Stdlib/ArrayUtils.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,23 @@ public static function iteratorToArray($iterator, $recursive = true)
245245
/**
246246
* Merge two arrays together.
247247
*
248-
* If an integer key exists in both arrays, the value from the second array
249-
* will be appended the the first array. If both values are arrays, they
250-
* are merged together, else the value of the second array overwrites the
251-
* one of the first array.
248+
* If an integer key exists in both arrays and preserveNumericKeys is false, the value
249+
* from the second array will be appended to the first array. If both values are arrays, they
250+
* are merged together, else the value of the second array overwrites the one of the first array.
252251
*
253252
* @param array $a
254253
* @param array $b
254+
* @param bool $preserveNumericKeys
255255
* @return array
256256
*/
257-
public static function merge(array $a, array $b)
257+
public static function merge(array $a, array $b, $preserveNumericKeys = false)
258258
{
259259
foreach ($b as $key => $value) {
260260
if (array_key_exists($key, $a)) {
261-
if (is_int($key)) {
261+
if (is_int($key) && !$preserveNumericKeys) {
262262
$a[] = $value;
263263
} elseif (is_array($value) && is_array($a[$key])) {
264-
$a[$key] = static::merge($a[$key], $value);
264+
$a[$key] = static::merge($a[$key], $value, $preserveNumericKeys);
265265
} else {
266266
$a[$key] = $value;
267267
}

tests/ZendTest/Stdlib/ArrayUtilsTest.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,67 @@ public static function invalidArrays()
143143
public static function mergeArrays()
144144
{
145145
return array(
146-
'merge-integer-and-string keys' => array(
146+
'merge-integer-and-string-keys' => array(
147147
array(
148148
'foo',
149-
3 => 'bar',
150-
'baz' => 'baz'
149+
3 => 'bar',
150+
'baz' => 'baz',
151+
4 => array(
152+
'a',
153+
1 => 'b',
154+
'c',
155+
),
151156
),
152157
array(
153158
'baz',
159+
4 => array(
160+
'd' => 'd',
161+
),
154162
),
163+
false,
155164
array(
156165
0 => 'foo',
157166
3 => 'bar',
158167
'baz' => 'baz',
159-
4 => 'baz'
168+
4 => array(
169+
'a',
170+
1 => 'b',
171+
'c',
172+
),
173+
5 => 'baz',
174+
6 => array(
175+
'd' => 'd',
176+
),
177+
)
178+
),
179+
'merge-integer-and-string-keys-preserve-numeric' => array(
180+
array(
181+
'foo',
182+
3 => 'bar',
183+
'baz' => 'baz',
184+
4 => array(
185+
'a',
186+
1 => 'b',
187+
'c',
188+
),
189+
),
190+
array(
191+
'baz',
192+
4 => array(
193+
'd' => 'd',
194+
),
195+
),
196+
true,
197+
array(
198+
0 => 'baz',
199+
3 => 'bar',
200+
'baz' => 'baz',
201+
4 => array(
202+
'a',
203+
1 => 'b',
204+
'c',
205+
'd' => 'd',
206+
),
160207
)
161208
),
162209
'merge-arrays-recursively' => array(
@@ -170,6 +217,7 @@ public static function mergeArrays()
170217
'baz'
171218
)
172219
),
220+
false,
173221
array(
174222
'foo' => array(
175223
0 => 'baz',
@@ -186,6 +234,7 @@ public static function mergeArrays()
186234
'foo' => 'baz',
187235
'bar' => 'bat'
188236
),
237+
false,
189238
array(
190239
'foo' => 'baz',
191240
'bar' => 'bat'
@@ -340,9 +389,9 @@ public function testEmptyArrayReturnsFalse()
340389
/**
341390
* @dataProvider mergeArrays
342391
*/
343-
public function testMerge($a, $b, $expected)
392+
public function testMerge($a, $b, $preserveNumericKeys, $expected)
344393
{
345-
$this->assertEquals($expected, ArrayUtils::merge($a, $b));
394+
$this->assertEquals($expected, ArrayUtils::merge($a, $b, $preserveNumericKeys));
346395
}
347396

348397
/**

0 commit comments

Comments
 (0)