diff options
author | Sam Light <samlight1994@gmail.com> | 2025-04-26 12:25:03 +0100 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2025-04-26 12:25:03 +0100 |
commit | 88f71aeba1a1649661d713297d3a75454636b2d1 (patch) | |
tree | 9f37fb5bbcd22930dd5e7686958614abf2a970d0 /src | |
parent | a1ba375df4978a606d519430ce8f7c7f0d170eed (diff) |
Larastan fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/TableComponent.php | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/src/TableComponent.php b/src/TableComponent.php index 14d631b..84ca518 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -3,7 +3,7 @@ namespace Lightscale\LaralightTables; use Lightscale\LaralightTables\Columns\Column; - +use Lightscale\LaralightTables\Toolbar\Filter; use Lightscale\LaralightAssets\Facades\Assets; use Livewire\Component; @@ -12,18 +12,34 @@ use Livewire\Attributes\Url; use Illuminate\Pagination\Paginator; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use Illuminate\Support\Collection; +use Illuminate\View\View; use Exception; + +/** + * @template TModel of Model + */ abstract class TableComponent extends Component { use WithPagination; // Config + + /** + * @var string + */ protected $paginationTheme = 'bootstrap'; + + + /** + * @var ?class-string<TModel> + */ protected $model = null; + protected int $defaultPageSize = 10; // Properties @@ -33,8 +49,14 @@ abstract class TableComponent extends Component #[Url] public int $pageSize; + /** + * @var array<string> + */ public array $activeColumns = []; + /** + * @var array<string, string> + */ #[Url] public array $filters = []; @@ -104,13 +126,22 @@ abstract class TableComponent extends Component } } + /** + * @return array<Toolbar> + */ protected function toolbars(): array { return []; } + /** + * @var Collection<int, Toolbar> + */ private Collection $toolbarsCache; + /** + * @return Collection<int, Toolbar> + */ protected function getToolbars(): Collection { if (!isset($this->toolbarsCache)) { @@ -119,19 +150,33 @@ abstract class TableComponent extends Component return $this->toolbarsCache; } - protected function query() : Builder + /** + * @return Builder<TModel> + */ + protected function query(): Builder { if($this->model === null) { throw new Exception('Requires $model to be set or query() method to be overridden'); } + \PHPStan\dumpType((new $this->model)->newQuery()); return $this->model::query(); } - abstract protected function columns() : array; + /** + * @return array<Column> + */ + abstract protected function columns(): array; + /** + * @param Builder<TModel> $builder + * @param string $search + */ protected function search(Builder $builder, string $search) : void {} + /** + * @return Collection<int, Filter> + */ protected function getFilters(): Collection { return $this->getToolbars() @@ -145,6 +190,9 @@ abstract class TableComponent extends Component return ($name = $this->order) === null ? null : $this->getColumns()[$name] ?? null; } + /** + * @param Builder<TModel> $query + */ protected function applyOrder(Builder $query): void { $column = $this->getOrderColumn(); @@ -153,6 +201,9 @@ abstract class TableComponent extends Component } } + /** + * @return Builder<TModel> + */ protected function buildQuery() : Builder { $query = $this->query(); @@ -170,7 +221,14 @@ abstract class TableComponent extends Component return $query; } + /** + * @var ?Collection<int, Column> + */ private ?Collection $columnsCache = null; + + /** + * @return Collection<int, Column> + */ public function getColumns(): Collection { if($this->columnsCache === null) { @@ -182,7 +240,7 @@ abstract class TableComponent extends Component return $this->columnsCache; } - public function render() + public function render(): View { $data = $this->buildQuery()->paginate($this->pageSize); $allColumns = $this->getColumns(); |