8000 Merge branch '4.4' into 5.2 · symfony/symfony@174efe8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 174efe8

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: [Messenger] Fix merging PrototypedArrayNode associative values
2 parents 5e19343 + 53d9b10 commit 174efe8

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"symfony/polyfill-mbstring": "~1.0",
5353
"symfony/polyfill-php73": "^1.11",
5454
"symfony/polyfill-php80": "^1.15",
55+
"symfony/polyfill-php81": "^1.22",
5556
"symfony/polyfill-uuid": "^1.15"
5657
},
5758
"replace": {

src/Symfony/Component/Config/Definition/PrototypedArrayNode.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ protected function normalizeValue($value)
215215

216216
$value = $this->remapXml($value);
217217

218-
$isAssoc = array_keys($value) !== range(0, \count($value) - 1);
218+
$isList = array_is_list($value);
219219
$normalized = [];
220220
foreach ($value as $k => $v) {
221221
if (null !== $this->keyAttribute && \is_array($v)) {
222-
if (!isset($v[$this->keyAttribute]) && \is_int($k) && !$isAssoc) {
222+
if (!isset($v[$this->keyAttribute]) && \is_int($k) && $isList) {
223223
$ex = new InvalidConfigurationException(sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath()));
224224
$ex->setPath($this->getPath());
225225

@@ -261,7 +261,7 @@ protected function normalizeValue($value)
261261
}
262262

263263
$prototype = $this->getPrototypeForChild($k);
264-
if (null !== $this->keyAttribute || $isAssoc) {
264+
if (null !== $this->keyAttribute || !$isList) {
265265
$normalized[$k] = $prototype->normalize($v);
266266
} else {
267267
$normalized[] = $prototype->normalize($v);
@@ -294,9 +294,10 @@ protected function mergeValues($leftSide, $rightSide)
294294
return $rightSide;
295295
}
296296

297+
$isList = array_is_list($rightSide);
297298
foreach ($rightSide as $k => $v) {
298-
// prototype, and key is irrelevant, append the element
299-
if (null === $this->keyAttribute) {
299+
// prototype, and key is irrelevant there are no named keys, append the element
300+
if (null === $this->keyAttribute && $isList) {
300301
$leftSide[] = $v;
301302
continue;
302303
}

src/Symfony/Component/Config/Tests/Definition/PrototypedArrayNodeTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,56 @@ public function getDataForKeyRemovedLeftValueOnly()
338338
],
339339
];
340340
}
341+
342+
/**
343+
* @dataProvider getPrototypedArrayNodeDataToMerge
344+
*/
345+
public function testPrototypedArrayNodeMerge($left, $right, $expected)
346+
{
347+
$node = new PrototypedArrayNode('options');
348+
$node->setNormalizeKeys(false);
349+
$node->setPrototype(new VariableNode('value'));
350+
$node->setDefaultValue([]);
351+
352+
$result = $node->merge($left, $right);
353+
354+
self::assertSame($result, $expected);
355+
}
356+
357+
public function getPrototypedArrayNodeDataToMerge()
358+
{
359+
return [
360+
// data to merged is a plain array
361+
[
362+
['foo', 'bar'],
363+
['foo', 'baz', 'qux'],
364+
['foo', 'bar', 'foo', 'baz', 'qux'],
365+
],
366+
// data to be merged is an associative array
367+
[
368+
['option1' => true, 'option2' => 'foo'],
369+
[
370+
'option2' => 'bar',
371+
'option3' => 42,
372+
'option4' => [
373+
'option41' => 'baz',
374+
'option42' => [
375+
'option423' => 'qux',
376+
],
377+
],
378+
],
379+
[
380+
'option1' => true,
381+
'option2' => 'bar',
382+
'option3' => 42,
383+
'option4' => [
384+
'option41' => 'baz',
385+
'option42' => [
386+
'option423' => 'qux',
387+
],
388+
],
389+
],
390+
],
391+
];
392+
}
341393
}

src/Symfony/Component/Config/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"symfony/deprecation-contracts": "^2.1",
2121
"symfony/filesystem": "^4.4|^5.0",
2222
"symfony/polyfill-ctype": "~1.8",
23-
"symfony/polyfill-php80": "^1.15"
23+
"symfony/polyfill-php80": "^1.15",
24+
"symfony/polyfill-php81": &quo 4817 t;^1.22"
2425
},
2526
"require-dev": {
2627
"symfony/event-dispatcher": "^4.4|^5.0",

0 commit comments

Comments
 (0)
0