Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
36 / 42 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
| QuerySearch | |
85.71% |
36 / 42 |
|
90.00% |
9 / 10 |
22.29 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getApieContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
71.43% |
15 / 21 |
|
0.00% |
0 / 1 |
8.14 | |||
| toHttpQuery | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| withPageIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getItemsPerPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTextSearch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSearches | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOrderBy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Datalayers\Search; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Lists\StringHashmap; |
| 6 | use Apie\Core\ValueObjects\Utils; |
| 7 | use Apie\DoctrineEntityDatalayer\Enums\SortingOrder; |
| 8 | |
| 9 | final class QuerySearch |
| 10 | { |
| 11 | private ?string $textSearch; |
| 12 | |
| 13 | private StringHashmap $searches; |
| 14 | |
| 15 | private StringHashmap $orderBy; |
| 16 | |
| 17 | private ApieContext $apieContext; |
| 18 | |
| 19 | public function __construct( |
| 20 | private int $pageIndex, |
| 21 | private int $itemsPerPage = 20, |
| 22 | ?string $textSearch = null, |
| 23 | ?StringHashmap $searches = null, |
| 24 | ?StringHashmap $orderBy = null, |
| 25 | ?ApieContext $apieContext = null, |
| 26 | ) { |
| 27 | $this->textSearch = $textSearch; |
| 28 | $this->searches = $searches ?? new StringHashmap(); |
| 29 | $this->orderBy = $orderBy ?? new StringHashmap(); |
| 30 | $this->apieContext = $apieContext ?? new ApieContext(); |
| 31 | } |
| 32 | |
| 33 | public function getApieContext(): ApieContext |
| 34 | { |
| 35 | return $this->apieContext; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param array<string, string|int|array<string, mixed>> $input |
| 40 | */ |
| 41 | public static function fromArray(array $input, ?ApieContext $apieContext = new ApieContext()): self |
| 42 | { |
| 43 | $pageIndex = $input['page'] ?? 0; |
| 44 | $itemsPerPage = max($input['items_per_page'] ?? 20, 1); |
| 45 | $data = is_array($input['query'] ?? '') ? $input['query'] : []; |
| 46 | $orderBy = $input['order_by'] ?? []; |
| 47 | if (!is_array($orderBy) && !empty($orderBy)) { |
| 48 | $orderBy = explode(',', Utils::toString($orderBy)); |
| 49 | } |
| 50 | $constructedOrderBy = []; |
| 51 | foreach ($orderBy as $column) { |
| 52 | if (str_starts_with($column, '+')) { |
| 53 | $constructedOrderBy[substr($column, 1)] = SortingOrder::ASCENDING->value; |
| 54 | } elseif (str_starts_with($column, '-')) { |
| 55 | $constructedOrderBy[substr($column, 1)] = SortingOrder::DESCENDING->value; |
| 56 | } else { |
| 57 | $constructedOrderBy[$column] = SortingOrder::ASCENDING->value; |
| 58 | } |
| 59 | } |
| 60 | return new QuerySearch( |
| 61 | $pageIndex, |
| 62 | $itemsPerPage, |
| 63 | $input['search'] ?? null, |
| 64 | new StringHashmap($data), |
| 65 | new StringHashmap($constructedOrderBy), |
| 66 | $apieContext |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | public function toHttpQuery(): string |
| 71 | { |
| 72 | $query = []; |
| 73 | if ($this->pageIndex > 0) { |
| 74 | $query['page'] = $this->pageIndex; |
| 75 | } |
| 76 | if ($this->itemsPerPage !== 20) { |
| 77 | $query['items_per_page'] = $this->itemsPerPage; |
| 78 | } |
| 79 | if ($this->textSearch !== null) { |
| 80 | $query['search'] = $this->textSearch; |
| 81 | } |
| 82 | if (0 !== $this->searches->count()) { |
| 83 | $query['query'] = $this->searches->toArray(); |
| 84 | } |
| 85 | return empty($query) ? '' : '?' . http_build_query($query); |
| 86 | } |
| 87 | |
| 88 | public function withPageIndex(int $pageIndex): self |
| 89 | { |
| 90 | return new self($pageIndex, $this->itemsPerPage, $this->textSearch, $this->searches); |
| 91 | } |
| 92 | |
| 93 | public function getPageIndex(): int |
| 94 | { |
| 95 | return $this->pageIndex; |
| 96 | } |
| 97 | |
| 98 | public function getItemsPerPage(): int |
| 99 | { |
| 100 | return $this->itemsPerPage; |
| 101 | } |
| 102 | |
| 103 | public function getTextSearch(): ?string |
| 104 | { |
| 105 | return $this->textSearch; |
| 106 | } |
| 107 | |
| 108 | public function getSearches(): StringHashmap |
| 109 | { |
| 110 | return $this->searches; |
| 111 | } |
| 112 | |
| 113 | public function getOrderBy(): StringHashmap |
| 114 | { |
| 115 | return $this->orderBy; |
| 116 | } |
| 117 | } |