Total Complexity | 9 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
5 | class Order |
||
6 | { |
||
7 | /** |
||
8 | * Order constructor. |
||
9 | * |
||
10 | * @param $ctrl |
||
11 | */ |
||
12 | public function __construct($ctrl) |
||
13 | { |
||
14 | $this->ctrl = $ctrl; |
||
|
|||
15 | } |
||
16 | |||
17 | /** |
||
18 | * @param $result |
||
19 | * @param $table |
||
20 | * @param $index |
||
21 | */ |
||
22 | function handle($result, $table) |
||
23 | { |
||
24 | $orderby = $this->ctrl->orderby; |
||
25 | if (! $orderby) { |
||
26 | $result->orderby($table.'.'.$this->ctrl->primary_key, 'desc'); |
||
27 | return; |
||
28 | } |
||
29 | if (is_string($orderby)) { |
||
30 | $orderby = $this->normalizeOrderBy($orderby); |
||
31 | } |
||
32 | |||
33 | $this->orderRows($result, $table, $orderby); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param $result |
||
38 | * @param $table |
||
39 | * @param $orderby |
||
40 | */ |
||
41 | private function orderRows($result, $table, $orderby) |
||
42 | { |
||
43 | foreach ($orderby as $key => $value) { |
||
44 | if (strpos($key, '.')) { |
||
45 | $table = explode(".", $key)[0]; |
||
46 | } |
||
47 | $result->orderby($table.'.'.$key, $value); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param $orderby |
||
53 | * @return array |
||
54 | */ |
||
55 | private function normalizeOrderBy($orderby) |
||
64 | } |
||
65 | } |