Total Complexity | 17 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Coverage | 70.37% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | class SortByColumn extends Formatter |
||
9 | { |
||
10 | |||
11 | 3 | public function format($items) |
|
12 | { |
||
13 | 3 | $column = $this->config('column', 'id'); |
|
14 | |||
15 | usort($items, function($a, $b) use ($column) { |
||
16 | 3 | $aVal = $a->getItem($column); |
|
17 | 3 | $bVal = $b->getItem($column); |
|
18 | 3 | if(is_null($aVal) && is_null($bVal)) { |
|
19 | return 0; |
||
20 | } |
||
21 | 3 | if(is_null($aVal) && !is_null($bVal)) { |
|
22 | return 1; |
||
23 | } |
||
24 | 3 | if(!is_null($aVal) && is_null($bVal)) { |
|
25 | 1 | return -1; |
|
26 | } |
||
27 | 3 | if(is_string($aVal) || is_string($bVal)) { |
|
28 | 1 | return $this->compareStrings($aVal, $bVal); |
|
29 | } |
||
30 | 2 | if(is_int($aVal) || is_int($bVal)) { |
|
31 | 2 | return $this->compareInts($aVal, $bVal); |
|
32 | } |
||
33 | return 0; |
||
34 | 3 | }); |
|
35 | 3 | return $items; |
|
36 | } |
||
37 | |||
38 | 1 | private function compareStrings(string $a, string $b): int |
|
41 | } |
||
42 | |||
43 | 2 | public function compareInts(int $a, int $b): int |
|
44 | { |
||
45 | 2 | if ($a == $b) { |
|
46 | return 0; |
||
47 | } |
||
48 | 2 | return ($a < $b) ? -1 : 1; |
|
49 | } |
||
50 | |||
51 | public function formatItem(FormattedItem $formattedItem): FormattedItem |
||
52 | { |
||
53 | return $formattedItem; |
||
54 | } |
||
55 | |||
56 | public function handles(): string |
||
59 | } |
||
60 | } |