Skip to content

Commit eb975d2

Browse files
author
Oleksii Korshenko
committed
MAGETWO-56154: Sorted Object List in di.xml
- added support of sortOrder attribute to array object items
1 parent 1aef003 commit eb975d2

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

lib/internal/Magento/Framework/Data/Argument/Interpreter/ArrayType.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,40 @@ public function evaluate(array $data)
3939
throw new \InvalidArgumentException('Array items are expected.');
4040
}
4141
$result = [];
42+
$items = $this->sortItems($items);
4243
foreach ($items as $itemKey => $itemData) {
4344
$result[$itemKey] = $this->itemInterpreter->evaluate($itemData);
4445
}
4546
return $result;
4647
}
48+
49+
/**
50+
* Sort items by sort order attribute.
51+
*
52+
* @param array $items
53+
* @return array
54+
*/
55+
private function sortItems($items)
56+
{
57+
uasort(
58+
$items,
59+
function ($firstItem, $secondItem) {
60+
$firstValue = 0;
61+
$secondValue = 0;
62+
if (isset($firstItem['sortOrder'])) {
63+
$firstValue = intval($firstItem['sortOrder']);
64+
}
65+
66+
if (isset($secondItem['sortOrder'])) {
67+
$secondValue = intval($secondItem['sortOrder']);
68+
}
69+
70+
if ($firstValue == $secondValue) {
71+
return 0;
72+
}
73+
return $firstValue < $secondValue ? -1 : 1;
74+
}
75+
);
76+
return $items;
77+
}
4778
}

lib/internal/Magento/Framework/Data/Test/Unit/Argument/Interpreter/ArrayTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ public function evaluateDataProvider()
8787
'key3' => '-value 3-',
8888
],
8989
],
90+
'sorted array items' => [
91+
[
92+
'item' => [
93+
'key1' => ['value' => 'value 1', 'sortOrder' => 50],
94+
'key2' => ['value' => 'value 2'],
95+
'key3' => ['value' => 'value 3', 'sortOrder' => 10],
96+
'key4' => ['value' => 'value 4'],
97+
],
98+
],
99+
[
100+
'key2' => '-value 2-',
101+
'key4' => '-value 4-',
102+
'key3' => '-value 3-',
103+
'key1' => '-value 1-',
104+
],
105+
],
90106
];
91107
}
92108
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/_files/invalidConfigXmlArray.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,21 @@
155155
"Element 'argument': Duplicate key-sequence ['same_param_name'] in key identity-constraint"
156156
. " 'argumentName'.\nLine: 6\n"
157157
],
158-
]
158+
],
159+
'sorted_object_list_with_invalid_sortOrder_attribute_value' => [
160+
'<?xml version="1.0"?>
161+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
162+
<type name="Some_Name">
163+
<arguments>
164+
<argument name="sorted_object_list" xsi:type="array">
165+
<item name="someObject" xsi:type="object" sortOrder="false">Some_Class_Name</item>
166+
</argument>
167+
</arguments>
168+
</type>
169+
</config>',
170+
[
171+
"Element 'item', attribute 'sortOrder': 'false' is not a valid value of the atomic type 'xs:integer'." .
172+
"\nLine: 6\n"
173+
],
174+
],
159175
];

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/_files/valid_config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
</item>
5858
</argument>
5959
<argument name="baseController" xsi:type="string">some_value</argument>
60+
<argument name="sortedList" xsi:type="array">
61+
<item name="itemTwo" xsi:type="object" sortOrder="20">Instance_test_name_two</item>
62+
<item name="itemOne" xsi:type="object">Instance_test_name_one</item>
63+
<item name="itemThree" xsi:type="object" sortOrder="30">Instance_test_name_three</item>
64+
</argument>
6065
</arguments>
6166
</type>
6267
</config>

lib/internal/Magento/Framework/ObjectManager/etc/config.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<xs:complexContent>
2121
<xs:extension base="object">
2222
<xs:attribute name="shared" use="optional" type="xs:boolean"/>
23+
<xs:attribute name="sortOrder" use="optional" type="xs:integer"/>
2324
</xs:extension>
2425
</xs:complexContent>
2526
</xs:complexType>

0 commit comments

Comments
 (0)