Aggregate.php
3 years ago
CRUD.php
3 years ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
3 years ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
3 years ago
OffsetStatement.php
4 years ago
OrderByStatement.php
4 years ago
SelectStatement.php
4 years ago
TablePrefix.php
4 years ago
UnionOperator.php
4 years ago
WhereClause.php
3 years ago
OrderByStatement.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\QueryBuilder\Concerns; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\QueryBuilder\Clauses\OrderBy; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.19.0 |
| 10 | */ |
| 11 | trait OrderByStatement |
| 12 | { |
| 13 | /** |
| 14 | * @var OrderBy[] |
| 15 | */ |
| 16 | protected $orderBys = []; |
| 17 | |
| 18 | /** |
| 19 | * @param string $column |
| 20 | * @param string $direction ASC|DESC |
| 21 | * |
| 22 | * @return $this |
| 23 | */ |
| 24 | public function orderBy($column, $direction = 'ASC') |
| 25 | { |
| 26 | $this->orderBys[] = new OrderBy($column, $direction); |
| 27 | |
| 28 | return $this; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return array|string[] |
| 33 | */ |
| 34 | protected function getOrderBySQL() |
| 35 | { |
| 36 | if (empty($this->orderBys)) { |
| 37 | return []; |
| 38 | } |
| 39 | |
| 40 | $orderBys = implode( |
| 41 | ', ', |
| 42 | array_map(function (OrderBy $order) { |
| 43 | return DB::prepare('%1s %2s', $order->column, $order->direction); |
| 44 | }, $this->orderBys) |
| 45 | ); |
| 46 | |
| 47 | |
| 48 | return ['ORDER BY ' . $orderBys]; |
| 49 | } |
| 50 | } |
| 51 |