From 3b28b792b85c31b9144ad919ca02792029c1ac24 Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Sun, 27 Oct 2024 04:43:14 +0000 Subject: [PATCH] Adjust for SortQueryString --- .../Core/QueryStrings/HasQueryString.php | 61 ++++++++ .../QueryStrings/HasQueryStringForSearch.php | 43 ++---- .../QueryStrings/HasQueryStringForSort.php | 143 ++++++++---------- 3 files changed, 141 insertions(+), 106 deletions(-) create mode 100644 src/Traits/Core/QueryStrings/HasQueryString.php diff --git a/src/Traits/Core/QueryStrings/HasQueryString.php b/src/Traits/Core/QueryStrings/HasQueryString.php new file mode 100644 index 000000000..58c018be0 --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryString.php @@ -0,0 +1,61 @@ + [], + 'filter' => [], + 'search' => [], + 'sorts' => [], + ]; + + protected function getQueryStringConfig(string $type): array + { + return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); + } + + protected function hasQueryStringConfigStatus(string $type): bool + { + return isset($this->getQueryStringConfig($type)['status']); + } + + protected function getQueryStringConfigStatus(string $type): bool + { + return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); + } + + protected function hasQueryStringConfigAlias(string $type): bool + { + return isset($this->getQueryStringConfig($type)['alias']); + } + + protected function getQueryStringConfigAlias(string $type): string + { + return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias()."-".$type; + } + + + protected function setQueryStringConfig(string $type, array $config): self + { + $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); + + return $this; + } + + protected function setQueryStringConfigStatus(string $type, bool $status): self + { + return $this->setQueryStringConfig($type, ['status' => $status]); + + } + + protected function setQueryStringConfigAlias(string $type, string $alias): self + { + return $this->setQueryStringConfig($type,['alias' => $alias]); + } + +} \ No newline at end of file diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php index 10cac7fb9..6737a178f 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSearch.php @@ -2,39 +2,32 @@ namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings; -use Livewire\Attributes\Locked; - trait HasQueryStringForSearch { - #[Locked] - public ?bool $queryStringStatusForSearch; - - protected ?string $queryStringAliasForSearch; - protected function queryStringHasQueryStringForSearch(): array { return ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) ? ['search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()]] : []; } - public function setupQueryStringStatusForSearch(): void + protected function setupQueryStringStatusForSearch(): void { if (! $this->hasQueryStringStatusForSearch()) { $this->setQueryStringForSearchEnabled(); } } - public function hasQueryStringStatusForSearch(): bool + protected function hasQueryStringStatusForSearch(): bool { - return isset($this->queryStringStatusForSearch); + return $this->hasQueryStringConfigStatus('search'); } - public function getQueryStringStatusForSearch(): bool + protected function getQueryStringStatusForSearch(): bool { - return $this->queryStringStatusForSearch ?? true; + return $this->getQueryStringConfigStatus("search"); } - public function queryStringForSearchEnabled(): bool + protected function queryStringForSearchEnabled(): bool { $this->setupQueryStringStatusForSearch(); @@ -43,39 +36,31 @@ public function queryStringForSearchEnabled(): bool public function setQueryStringStatusForSearch(bool $status): self { - $this->queryStringStatusForSearch = $status; - - return $this; + return $this->setQueryStringConfigStatus("search", $status); } public function setQueryStringForSearchEnabled(): self { - $this->setQueryStringStatusForSearch(true); - - return $this; + return $this->setQueryStringStatusForSearch(true); } public function setQueryStringForSearchDisabled(): self { - $this->setQueryStringStatusForSearch(false); - - return $this; + return $this->setQueryStringStatusForSearch(false); } - public function hasQueryStringAliasForSearch(): bool + protected function hasQueryStringAliasForSearch(): bool { - return isset($this->queryStringAliasForSearch); + return $this->hasQueryStringConfigAlias('search'); } - public function getQueryStringAliasForSearch(): string + protected function getQueryStringAliasForSearch(): string { - return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search'; + return $this->getQueryStringConfigAlias("search"); } public function setQueryStringAliasForSearch(string $alias): self { - $this->queryStringAliasForSearch = $alias; - - return $this; + return $this->setQueryStringConfigAlias("search", $alias); } } diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php index 9bb544f44..6a5a29ec6 100644 --- a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -1,77 +1,66 @@ -queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; - - } - - public function setupQueryStringStatusForSort(): void - { - if (! $this->hasQueryStringStatusForSort()) { - $this->setQueryStringForSortEnabled(); - } - } - - public function hasQueryStringStatusForSort(): bool - { - return isset($this->queryStringStatusForSort); - } - - public function getQueryStringStatusForSort(): bool - { - return $this->queryStringStatusForSort ?? true; - } - - public function queryStringForSortEnabled(): bool - { - $this->setupQueryStringStatusForSort(); - - return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); - } - - public function setQueryStringStatusForSort(bool $status): self - { - $this->queryStringStatusForSort = $status; - - return $this; - } - - public function setQueryStringForSortEnabled(): self - { - return $this->setQueryStringStatusForSort(true); - } - - public function setQueryStringForSortDisabled(): self - { - return $this->setQueryStringStatusForSort(false); - } - - public function hasQueryStringAliasForSort(): bool - { - return isset($this->queryStringAliasForSort); - } - - public function getQueryStringAliasForSort(): string - { - return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; - } - - public function setQueryStringAliasForSort(string $alias): self - { - $this->queryStringAliasForSort = $alias; - - return $this; - } -} +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + protected function hasQueryStringStatusForSort(): bool + { + return $this->hasQueryStringConfigStatus('sorts'); + } + + protected function getQueryStringStatusForSort(): bool + { + return $this->getQueryStringConfigStatus("sorts"); + } + + protected function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + return $this->setQueryStringConfigStatus("sorts", $status); + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + protected function hasQueryStringAliasForSort(): bool + { + return $this->hasQueryStringConfigAlias('sorts'); + } + + protected function getQueryStringAliasForSort(): string + { + return $this->getQueryStringConfigAlias("sorts"); + } + + public function setQueryStringAliasForSort(string $alias): self + { + return $this->setQueryStringConfigAlias("sorts", $alias); + } +}