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

Commit de2bff5

Browse files
committed
Merge pull request #55 from Opifer/valueorder
Added ability to change value order
2 parents f58e8a8 + e8f4cda commit de2bff5

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Model/ValueSet.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,23 @@ public function normalizeValues()
160160
* We use this to render a whole set of form types, which should be displayed
161161
* in some order. When you want to be able to get a form field by it's name
162162
* and place it on a custom place, use the getNamedValues() method.
163+
*
164+
* @param string $order
163165
*
164166
* @return array
165167
*/
166-
public function getSortedValues()
168+
public function getSortedValues($order = 'asc')
167169
{
168170
$values = $this->values->toArray();
169-
usort($values, function ($value1, $value2) {
170-
return ($value1->getAttribute()->getSort() < $value2->getAttribute()->getSort()) ? 1 : -1;
171+
usort($values, function ($a, $b) use ($order) {
172+
$left = $a->getAttribute()->getSort();
173+
$right = $b->getAttribute()->getSort();
174+
175+
if ($order == 'desc') {
176+
return ($left < $right) ? 1 : -1;
177+
}
178+
179+
return ($left > $right) ? 1 : -1;
171180
});
172181

173182
return $values;
@@ -186,9 +195,9 @@ public function getSortedValues()
186195
*
187196
* @return array
188197
*/
189-
public function getNamedValues()
198+
public function getNamedValues($order = 'asc')
190199
{
191-
$values = $this->getSortedValues();
200+
$values = $this->getSortedValues($order);
192201

193202
$valueArray = [];
194203
foreach ($values as $value) {

0 commit comments

Comments
 (0)