File tree Expand file tree Collapse file tree 5 files changed +70
-1
lines changed
lib/internal/Magento/Framework
Test/Unit/Argument/Interpreter Expand file tree Collapse file tree 5 files changed +70
-1
lines changed Original file line number Diff line number Diff line change @@ -39,9 +39,40 @@ public function evaluate(array $data)
39
39
throw new \InvalidArgumentException ('Array items are expected. ' );
40
40
}
41
41
$ result = [];
42
+ $ items = $ this ->sortItems ($ items );
42
43
foreach ($ items as $ itemKey => $ itemData ) {
43
44
$ result [$ itemKey ] = $ this ->itemInterpreter ->evaluate ($ itemData );
44
45
}
45
46
return $ result ;
46
47
}
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
+ }
47
78
}
Original file line number Diff line number Diff line change @@ -87,6 +87,22 @@ public function evaluateDataProvider()
87
87
'key3 ' => '-value 3- ' ,
88
88
],
89
89
],
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
+ ],
90
106
];
91
107
}
92
108
}
Original file line number Diff line number Diff line change 155
155
"Element 'argument': Duplicate key-sequence ['same_param_name'] in key identity-constraint "
156
156
. " 'argumentName'. \nLine: 6 \n"
157
157
],
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
+ ],
159
175
];
Original file line number Diff line number Diff line change 57
57
</item >
58
58
</argument >
59
59
<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 >
60
65
</arguments >
61
66
</type >
62
67
</config >
Original file line number Diff line number Diff line change 20
20
<xs : complexContent >
21
21
<xs : extension base =" object" >
22
22
<xs : attribute name =" shared" use =" optional" type =" xs:boolean" />
23
+ <xs : attribute name =" sortOrder" use =" optional" type =" xs:integer" />
23
24
</xs : extension >
24
25
</xs : complexContent >
25
26
</xs : complexType >
You can’t perform that action at this time.
0 commit comments