Conditions | 5 |
Paths | 2 |
Total Lines | 20 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 5 |
Changes | 0 |
1 | <?php |
||
23 | 1 | public function apply($src) |
|
24 | { |
||
25 | /** @var SortOperation $operation */ |
||
26 | 1 | $operation = $this->operation; |
|
27 | 1 | $field = $operation->getField(); |
|
28 | 1 | $desc = $operation->getOrder() === SortOperation::DESC; |
|
29 | 1 | if (!is_array($src)) { |
|
30 | 1 | $src = iterator_to_array($src); |
|
31 | 1 | } |
|
32 | 1 | usort($src, function (RowInterface $row1, RowInterface $row2) use ($field, $desc) { |
|
33 | 1 | $val1 = $row1->get($field); |
|
34 | 1 | $val2 = $row2->get($field); |
|
35 | 1 | if ($val1 == $val2) { |
|
36 | 1 | return 0; |
|
37 | } |
||
38 | 1 | $res = $val1 < $val2 ? -1 : 1; |
|
39 | 1 | return $desc ? -$res : $res; |
|
40 | 1 | }); |
|
41 | 1 | return $src; |
|
42 | } |
||
43 | } |
||
44 |