|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MacropaySolutions\LaravelCrudWizardClient; |
| 4 | + |
| 5 | +/** |
| 6 | + * @see ../README.md |
| 7 | + */ |
| 8 | +class QueryStringBuilder |
| 9 | +{ |
| 10 | + public bool $sqlDebug = false; |
| 11 | + private ?int $simplePaginate = null; |
| 12 | + private ?string $cursor = null; |
| 13 | + private int $page = 1; |
| 14 | + private int $limit = 10; |
| 15 | + private array $sort = []; |
| 16 | + private array $withRelations = []; |
| 17 | + private array $countRelations = []; |
| 18 | + private array $existRelations = []; |
| 19 | + private ResourceFilterBuilder $resourceFilters; |
| 20 | + private array $rawRequest = []; |
| 21 | + |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + $this->resourceFilters = new ResourceFilterBuilder(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * by using this, all other conditions set by using other methods will be ignored |
| 29 | + */ |
| 30 | + public function setRawRequest(array $request): self |
| 31 | + { |
| 32 | + $this->rawRequest = $request; |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + public function page(int $page): self |
| 38 | + { |
| 39 | + $this->page = $page; |
| 40 | + |
| 41 | + return $this; |
| 42 | + } |
| 43 | + |
| 44 | + public function limit(int $limit): self |
| 45 | + { |
| 46 | + $this->limit = $limit; |
| 47 | + |
| 48 | + return $this; |
| 49 | + } |
| 50 | + |
| 51 | + public function sort(string $by, string $dir = 'DESC'): self |
| 52 | + { |
| 53 | + $this->sort[] = ['by' => $by, 'dir' => $dir]; |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function equals(string $column, $is): self |
| 59 | + { |
| 60 | + $this->resourceFilters->equals($column, $is); |
| 61 | + |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param string|null $cursor use '1' for 1st page, null for non cursor pagination |
| 67 | + */ |
| 68 | + public function simplePaginate(?string $cursor = null): self |
| 69 | + { |
| 70 | + $this->simplePaginate = 1; |
| 71 | + $this->cursor = $cursor; |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + public function withRelation(string $relation): self |
| 77 | + { |
| 78 | + $this->withRelations[] = $relation; |
| 79 | + |
| 80 | + return $this; |
| 81 | + } |
| 82 | + |
| 83 | + public function withRelations(array $relations): self |
| 84 | + { |
| 85 | + $this->withRelations = \array_merge(\array_values($relations), $this->withRelations); |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + public function addCountRelation(string $countRelation): self |
| 91 | + { |
| 92 | + $this->countRelations[] = $countRelation; |
| 93 | + |
| 94 | + return $this; |
| 95 | + } |
| 96 | + |
| 97 | + public function addCountRelations(array $countRelations): self |
| 98 | + { |
| 99 | + $this->countRelations = \array_merge(\array_values($countRelations), $this->countRelations); |
| 100 | + |
| 101 | + return $this; |
| 102 | + } |
| 103 | + |
| 104 | + public function addExistRelation(string $existRelation): self |
| 105 | + { |
| 106 | + $this->existRelations[] = $existRelation; |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + public function addExistRelations(array $existRelations): self |
| 112 | + { |
| 113 | + $this->existRelations = \array_merge(\array_values($existRelations), $this->existRelations); |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + public function getUrlQueryString(array $overrides = []): string |
| 119 | + { |
| 120 | + if ($this->rawRequest !== []) { |
| 121 | + return \http_build_query(\array_merge( |
| 122 | + $this->rawRequest, |
| 123 | + $overrides |
| 124 | + ), '', '&', PHP_QUERY_RFC3986); |
| 125 | + } |
| 126 | + |
| 127 | + return \http_build_query(\array_merge( |
| 128 | + $this->getAllFilters(), |
| 129 | + $overrides |
| 130 | + ), '', '&', PHP_QUERY_RFC3986); |
| 131 | + } |
| 132 | + |
| 133 | + public function getAllFilters(): array |
| 134 | + { |
| 135 | + return \array_merge( |
| 136 | + $this->getFilters(), |
| 137 | + $this->sqlDebug ? ['sqlDebug' => 1] : [], |
| 138 | + $this->simplePaginate !== null ? ['simplePaginate' => $this->simplePaginate] : [], |
| 139 | + $this->cursor !== null ? ['cursor' => $this->cursor] : [], |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + private function getFilters(): array |
| 144 | + { |
| 145 | + return \array_merge($this->sqlDebug ? ['sqlDebug' => 1] : [], [ |
| 146 | + 'page' => $this->page, |
| 147 | + 'limit' => $this->limit, |
| 148 | + 'sort' => $this->sort, |
| 149 | + 'withRelations' => \array_values(\array_unique($this->withRelations)), |
| 150 | + 'withRelationsCount' => \array_values(\array_unique($this->countRelations)), |
| 151 | + 'withRelationsExistence' => \array_values(\array_unique($this->existRelations)), |
| 152 | + ], $this->resourceFilters->getFilters()); |
| 153 | + } |
| 154 | +} |
0 commit comments