Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| OrderBy | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| orderBy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| orderByAsc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| orderByDesc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addOrderBy | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| renderOrderBy | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Database Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Database\Manipulation\Traits; |
| 11 | |
| 12 | use Closure; |
| 13 | |
| 14 | /** |
| 15 | * Trait OrderBy. |
| 16 | * |
| 17 | * @see https://mariadb.com/kb/en/order-by/ |
| 18 | * |
| 19 | * @package database |
| 20 | */ |
| 21 | trait OrderBy |
| 22 | { |
| 23 | /** |
| 24 | * Appends columns to the ORDER BY clause. |
| 25 | * |
| 26 | * @param Closure|string $column The column name or a subquery |
| 27 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 28 | * |
| 29 | * @return static |
| 30 | */ |
| 31 | public function orderBy(Closure | string $column, Closure | string ...$columns) : static |
| 32 | { |
| 33 | return $this->addOrderBy($column, $columns, null); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Appends columns with the ASC direction to the ORDER BY clause. |
| 38 | * |
| 39 | * @param Closure|string $column The column name or a subquery |
| 40 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 41 | * |
| 42 | * @return static |
| 43 | */ |
| 44 | public function orderByAsc(Closure | string $column, Closure | string ...$columns) : static |
| 45 | { |
| 46 | return $this->addOrderBy($column, $columns, 'ASC'); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Appends columns with the DESC direction to the ORDER BY clause. |
| 51 | * |
| 52 | * @param Closure|string $column The column name or a subquery |
| 53 | * @param Closure|string ...$columns Extra column names and/or subqueries |
| 54 | * |
| 55 | * @return static |
| 56 | */ |
| 57 | public function orderByDesc(Closure | string $column, Closure | string ...$columns) : static |
| 58 | { |
| 59 | return $this->addOrderBy($column, $columns, 'DESC'); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Adds a ORDER BY expression. |
| 64 | * |
| 65 | * @param Closure|string $column The column name or a subquery |
| 66 | * @param array<Closure|string> $columns Extra column names and/or subqueries |
| 67 | * @param string|null $direction `ASC`, `DESC` or null for none |
| 68 | * |
| 69 | * @return static |
| 70 | */ |
| 71 | private function addOrderBy(Closure | string $column, array $columns, ?string $direction) : static |
| 72 | { |
| 73 | foreach ([$column, ...$columns] as $column) { |
| 74 | $this->sql['order_by'][] = [ |
| 75 | 'column' => $column, |
| 76 | 'direction' => $direction, |
| 77 | ]; |
| 78 | } |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Renders the ORDER BY clause. |
| 84 | * |
| 85 | * @return string|null The ORDER BY clause or null if it was not set |
| 86 | */ |
| 87 | protected function renderOrderBy() : ?string |
| 88 | { |
| 89 | if ( ! isset($this->sql['order_by'])) { |
| 90 | return null; |
| 91 | } |
| 92 | $expressions = []; |
| 93 | foreach ($this->sql['order_by'] as $part) { |
| 94 | $expression = $this->renderIdentifier($part['column']); |
| 95 | if ($part['direction']) { |
| 96 | $expression .= " {$part['direction']}"; |
| 97 | } |
| 98 | $expressions[] = $expression; |
| 99 | } |
| 100 | $expressions = \implode(', ', $expressions); |
| 101 | return " ORDER BY {$expressions}"; |
| 102 | } |
| 103 | } |