Total Complexity | 7 |
Total Lines | 43 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php declare(strict_types=1); |
||
9 | trait OrderByTrait |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $orderBy = []; |
||
15 | |||
16 | /** |
||
17 | * @param string|array $orderBy |
||
18 | * @param string $keyword |
||
19 | * |
||
20 | * @throws QueryBuilderException |
||
21 | * @return $this |
||
22 | */ |
||
23 | public function orderBy($orderBy, string $keyword = KeywordEnum::ASC) |
||
24 | { |
||
25 | if (empty($orderBy)) { |
||
26 | throw new QueryBuilderException('You must pass $orderBy to orderBy method!'); |
||
27 | } |
||
28 | |||
29 | if (!\in_array($keyword, [KeywordEnum::ASC, KeywordEnum::DESC])) { |
||
30 | throw new QueryBuilderException(\sprintf('Invalid $keyword "%s" for orderBy!', $keyword)); |
||
31 | } |
||
32 | |||
33 | if (!\is_array($orderBy)) { |
||
34 | $orderBy = [$orderBy]; |
||
35 | } |
||
36 | |||
37 | foreach ($orderBy as $orderByColumn) { |
||
38 | $this->orderBy[] = \sprintf('%s %s', $orderByColumn, $keyword); |
||
39 | } |
||
40 | |||
41 | return $this; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return null|string |
||
46 | */ |
||
47 | protected function buildOrderByQueryPart(): ?string |
||
52 | ; |
||
53 | } |
||
55 |