carno-php /
mysql
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Columns orders |
||
| 4 | * User: moyo |
||
| 5 | * Date: 25/12/2017 |
||
| 6 | * Time: 11:09 AM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Carno\Database\SQL\Builders; |
||
| 10 | |||
| 11 | use Carno\Database\SQL\Builder; |
||
| 12 | |||
| 13 | trait Order |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $bOrders = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @return string |
||
| 22 | */ |
||
| 23 | protected function gOrders() : string |
||
| 24 | { |
||
| 25 | return $this->bOrders ? sprintf(' ORDER BY %s', implode(',', $this->bOrders)) : ''; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array ...$orders |
||
| 30 | * @return Builder |
||
| 31 | */ |
||
| 32 | public function order(...$orders) : Builder |
||
| 33 | { |
||
| 34 | if (is_array($orders[0] ?? false)) { |
||
| 35 | // order([expr1], [expr2]) |
||
| 36 | foreach ($orders as $order) { |
||
| 37 | $this->order(...$order); |
||
| 38 | } |
||
| 39 | } else { |
||
| 40 | switch (count($orders)) { |
||
| 41 | case 1: |
||
| 42 | // order(expr) |
||
| 43 | $this->bOrders[] = $orders[0]; |
||
| 44 | break; |
||
| 45 | case 2: |
||
| 46 | // order('field', 'sort') |
||
| 47 | $this->bOrders[] = sprintf('`%s` %s', $orders[0], strtoupper($orders[1])); |
||
| 48 | break; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | return $this; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 53 | } |
||
| 54 | } |
||
| 55 |