diff options
author | Sam Light <samlight1994@gmail.com> | 2025-04-26 13:05:25 +0100 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2025-04-26 13:05:25 +0100 |
commit | 6d26688fded015141a45a88b90b2d70a0ac88420 (patch) | |
tree | 33324531f6e9595c7eb190b89bcdb5a7b55d1b5f /src | |
parent | 88f71aeba1a1649661d713297d3a75454636b2d1 (diff) |
Larastanning
Diffstat (limited to 'src')
-rw-r--r-- | src/Columns/ButtonColumn.php | 2 | ||||
-rw-r--r-- | src/Columns/Column.php | 20 | ||||
-rw-r--r-- | src/Columns/ElementColumn.php | 5 | ||||
-rw-r--r-- | src/Concerns/WithAttributes.php | 6 | ||||
-rw-r--r-- | src/TableComponent.php | 6 | ||||
-rw-r--r-- | src/Toolbar.php | 32 | ||||
-rw-r--r-- | src/Toolbar/Filter.php | 9 | ||||
-rw-r--r-- | src/Toolbar/Item.php | 4 | ||||
-rw-r--r-- | src/Toolbar/PageSize.php | 3 | ||||
-rw-r--r-- | src/Toolbar/SelectFilter.php | 6 |
10 files changed, 84 insertions, 9 deletions
diff --git a/src/Columns/ButtonColumn.php b/src/Columns/ButtonColumn.php index 794b175..2124a37 100644 --- a/src/Columns/ButtonColumn.php +++ b/src/Columns/ButtonColumn.php @@ -8,7 +8,7 @@ class ButtonColumn extends ElementColumn { protected string $element = 'button'; - protected function getElemAttributes(Model $row) : array + protected function getElemAttributes(Model $row): array { return parent::getElemAttributes($row) + [ 'type' => 'button' diff --git a/src/Columns/Column.php b/src/Columns/Column.php index 46a03ba..ad3a787 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -15,6 +15,10 @@ use Closure; class Column { use Makable; + + /** + * @var TableComponent<Model> + */ private TableComponent $table; private bool $showInSelect; private bool $shouldEscape = true; @@ -32,6 +36,10 @@ class Column { $this->showInSelect = $this->title !== null; } + /** + * @template TModel of Model + * @param TableComponent<TModel> $table + */ public function setTable(TableComponent $table): void { $this->table = $table; @@ -55,7 +63,9 @@ class Column { public function sortable(?callable $fn): static { - $this->sortFn = $fn; + if ($fn !== null) { + $this->sortFn = Closure::fromCallable($fn); + } return $this; } @@ -64,7 +74,11 @@ class Column { return $this->sortFn !== null; } - public function applySort(Builder $query, string $dir): void + /** + * @template TModel of Model + * @param Builder<TModel> $query + */ + public function applySort(Builder $query, ?string $dir): void { if ($this->sortFn !== null) { ($this->sortFn)($query, $dir); @@ -88,7 +102,7 @@ class Column { return $this; } - public function showInSelect($show = true): static + public function showInSelect(bool $show = true): static { $this->showInSelect = $show; return $this; diff --git a/src/Columns/ElementColumn.php b/src/Columns/ElementColumn.php index 71f0750..0e98d4a 100644 --- a/src/Columns/ElementColumn.php +++ b/src/Columns/ElementColumn.php @@ -20,7 +20,10 @@ abstract class ElementColumn extends Column return $this; } - protected function getElemAttributes(Model $row) : array + /** + * @return array<string, string> + */ + protected function getElemAttributes(Model $row): array { return $this->elemAttributesFn?->call($this, $row) ?? []; } diff --git a/src/Concerns/WithAttributes.php b/src/Concerns/WithAttributes.php index 5b77fa5..9072145 100644 --- a/src/Concerns/WithAttributes.php +++ b/src/Concerns/WithAttributes.php @@ -6,8 +6,14 @@ use Illuminate\View\ComponentAttributeBag; trait WithAttributes { + /** + * @var array<string, string> + */ protected ?array $attributes = null; + /** + * @param array<string, string> $attributes + */ public function attributes(array $attributes): static { $this->attributes = $attributes; diff --git a/src/TableComponent.php b/src/TableComponent.php index 84ca518..affb8fd 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -152,6 +152,7 @@ abstract class TableComponent extends Component /** * @return Builder<TModel> + * @phpstan-return Builder<TModel> */ protected function query(): Builder { @@ -159,8 +160,9 @@ abstract class TableComponent extends Component throw new Exception('Requires $model to be set or query() method to be overridden'); } - \PHPStan\dumpType((new $this->model)->newQuery()); - return $this->model::query(); + /** @var Builder<TModel> */ + $query = $this->model::query(); + return $query; } /** diff --git a/src/Toolbar.php b/src/Toolbar.php index 10082ae..80f093b 100644 --- a/src/Toolbar.php +++ b/src/Toolbar.php @@ -10,6 +10,7 @@ use Lightscale\LaralightTables\Concerns\Makable; use Illuminate\View\View; use Illuminate\Support\Collection; +use Illuminate\Database\Eloquent\Model; class Toolbar { @@ -18,11 +19,30 @@ class Toolbar protected ?SearchItem $searchItem = null; protected ?PageSizeItem $pageSizeItem = null; + /** + * @var Collection<int, ToolbarItem> + */ protected Collection $startItems; + + /** + * @var Collection<int, ToolbarItem> + */ protected Collection $midItems; + + /** + * @var Collection<int, ToolbarItem> + */ protected Collection $endItems; + + /** + * @var Collection<int, FilterItem> + */ protected Collection $filterItems; + /** + * @template TModel of Model + * @param TableComponent<TModel> $table + */ public function __construct( private TableComponent $table ) @@ -48,6 +68,9 @@ class Toolbar } } + /** + * @param Collection<int, ToolbarItem> $list + */ private function prependItem(Collection $list, ToolbarItem $item): static { $this->processItem($item); @@ -55,6 +78,9 @@ class Toolbar return $this; } + /** + * @param Collection<int, ToolbarItem> $list + */ private function appendItem(Collection $list, ToolbarItem $item): static { $this->processItem($item); @@ -92,6 +118,9 @@ class Toolbar return $this->appendItem($this->endItems, $item); } + /** + * @return TableComponent<Model> + */ public function getTable(): TableComponent { return $this->table; @@ -107,6 +136,9 @@ class Toolbar return $this->pageSizeItem; } + /** + * @return Collection<int, FilterItem> + */ public function getFilters(): Collection { return $this->filterItems; diff --git a/src/Toolbar/Filter.php b/src/Toolbar/Filter.php index 51e0d9a..f6777ce 100644 --- a/src/Toolbar/Filter.php +++ b/src/Toolbar/Filter.php @@ -2,6 +2,7 @@ namespace Lightscale\LaralightTables\Toolbar; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Closure; @@ -26,12 +27,16 @@ abstract class Filter extends Item return "filter_{$this->key}"; } - public function filter(callable $filterCB): static + public function filter(callable $fn): static { - $this->filterCallback = $filterCB; + $this->filterCallback = Closure::fromCallable($fn);; return $this; } + /** + * @template TModel of Model + * @param Builder<TModel> $query + */ public function applyFilter(Builder $query): void { if ($this->filterCallback !== null) { diff --git a/src/Toolbar/Item.php b/src/Toolbar/Item.php index e91698f..e161fb9 100644 --- a/src/Toolbar/Item.php +++ b/src/Toolbar/Item.php @@ -8,6 +8,7 @@ use Lightscale\LaralightTables\Concerns\Makable; use Illuminate\View\View; use Illuminate\Support\HtmlString; +use Illuminate\Database\Eloquent\Model; abstract class Item { @@ -25,6 +26,9 @@ abstract class Item return $this->toolbar; } + /** + * @return TableComponent<Model> + */ public function getTable(): TableComponent { return $this->getToolbar()->getTable(); diff --git a/src/Toolbar/PageSize.php b/src/Toolbar/PageSize.php index 9c2821a..79234a8 100644 --- a/src/Toolbar/PageSize.php +++ b/src/Toolbar/PageSize.php @@ -6,6 +6,9 @@ use Illuminate\View\View; class PageSize extends Item { + /** + * @param array<int> $pageSizes + */ public function __construct( private array $pageSizes = [10, 25, 50], ) {} diff --git a/src/Toolbar/SelectFilter.php b/src/Toolbar/SelectFilter.php index aa1c21a..7adc47a 100644 --- a/src/Toolbar/SelectFilter.php +++ b/src/Toolbar/SelectFilter.php @@ -10,6 +10,9 @@ class SelectFilter extends Filter { use WithAttributes; + /** + * @var iterable<string, string> + */ protected iterable $options; protected ?string $placeholder = null; @@ -19,6 +22,9 @@ class SelectFilter extends Filter return $this; } + /** + * @param iterable<string, string> $options + */ public function options(iterable $options): static { $this->options = $options; |