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