| Total Complexity | 10 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | trait WithSorts |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param array<(string|\Zing\QueryBuilder\Sort)> $sorts |
||
| 14 | * |
||
| 15 | * @return $this |
||
| 16 | */ |
||
| 17 | 6 | public function enableSorts(array $sorts) |
|
| 18 | { |
||
| 19 | 6 | $this->formatSorts($sorts) |
|
| 20 | 6 | ->each( |
|
| 21 | 6 | function (Sort $sort): void { |
|
| 22 | 6 | $thisIsRequestedSort = $this->isRequestedSort($sort); |
|
| 23 | 6 | if ($thisIsRequestedSort) { |
|
| 24 | 6 | $sort->sort($this->getBuilder(), $this->getSortValue($sort)); |
|
|
|
|||
| 25 | |||
| 26 | 6 | return; |
|
| 27 | } |
||
| 28 | |||
| 29 | 1 | if ($sort->hasDefaultDirection()) { |
|
| 30 | 1 | $sort->sort($this->getBuilder(), $sort->getDefaultDirection()); |
|
| 31 | } |
||
| 32 | } |
||
| 33 | ); |
||
| 34 | |||
| 35 | 6 | return $this; |
|
| 36 | } |
||
| 37 | |||
| 38 | 6 | protected function isRequestedSort(Sort $sort): bool |
|
| 39 | { |
||
| 40 | 6 | if ($this->request->input('asc') === $sort->getProperty()) { |
|
| 41 | 3 | return true; |
|
| 42 | } |
||
| 43 | |||
| 44 | 4 | return $this->request->input('desc') === $sort->getProperty(); |
|
| 45 | } |
||
| 46 | |||
| 47 | 6 | protected function getSortValue(Sort $sort): string |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @phpstan-param array<(string|\Zing\QueryBuilder\Sort)> $sorts |
||
| 58 | * |
||
| 59 | * @param mixed $sorts |
||
| 60 | */ |
||
| 61 | 6 | protected function formatSorts($sorts): Collection |
|
| 74 | } |
||
| 75 | ); |
||
| 76 | } |
||
| 78 |