yaroslawww /
laravel-inertia-dashboard
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace InertiaDashboardKit\Adapt; |
||||
| 4 | |||||
| 5 | use Illuminate\Http\Request; |
||||
| 6 | use InertiaDashboardKit\Adapt\Resources\InertiaPaginatedResource; |
||||
| 7 | |||||
| 8 | class IndexData |
||||
| 9 | { |
||||
| 10 | protected ?string $useResource = null; |
||||
| 11 | protected array $mapOptions = []; |
||||
| 12 | protected array $bulkActions = []; |
||||
| 13 | protected ?int $perPage = null; |
||||
| 14 | protected ?array $links = null; |
||||
| 15 | protected ?array $columns = []; |
||||
| 16 | |||||
| 17 | protected bool $withoutFiltration = false; |
||||
| 18 | |||||
| 19 | public function __construct( |
||||
| 20 | protected Request $request, |
||||
| 21 | protected string $entityType, |
||||
| 22 | protected $query, |
||||
| 23 | protected array $options = [] |
||||
| 24 | ) { |
||||
| 25 | } |
||||
| 26 | |||||
| 27 | public static function make(...$args) |
||||
| 28 | { |
||||
| 29 | return new static(...$args); |
||||
| 30 | } |
||||
| 31 | |||||
| 32 | public function toResponseArray(): array |
||||
| 33 | { |
||||
| 34 | return [ |
||||
| 35 | 'entityType' => $this->entityType, |
||||
| 36 | 'entities' => (new InertiaPaginatedResource( |
||||
| 37 | $this->preparedQuery() |
||||
| 38 | ->paginate($this->perPage, page: ($this->withoutFiltration ? 1 : null)) |
||||
| 39 | ->appends($this->withoutFiltration ? [] : $this->request->all()), |
||||
| 40 | $this->useResource, |
||||
| 41 | $this->mapOptions, |
||||
| 42 | ))->toArray($this->request), |
||||
| 43 | 'bulkActions' => collect($this->bulkActions) |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 44 | ->filter(fn ($action) => $action->allowedForRequest($this->request)) |
||||
| 45 | ->mapWithKeys(fn ($action) => [$action::name() => $action->toArray()]) |
||||
| 46 | ->toArray(), |
||||
| 47 | 'links' => $this->links, |
||||
| 48 | 'columns' => array_map(fn ($column) => $column->toArray(), $this->columns), |
||||
|
0 ignored issues
–
show
It seems like
$this->columns can also be of type null; however, parameter $array of array_map() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 49 | 'filtration' => http_build_query($this->withoutFiltration ? [] : $this->request->all()), |
||||
| 50 | ]; |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | public function useResource(?string $class = null, array $mapOptions = []): static |
||||
| 54 | { |
||||
| 55 | $this->useResource = $class; |
||||
| 56 | $this->mapOptions = $mapOptions; |
||||
| 57 | |||||
| 58 | return $this; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | public function bulkActions(array $bulkActions): static |
||||
| 62 | { |
||||
| 63 | $this->bulkActions = $bulkActions; |
||||
| 64 | |||||
| 65 | return $this; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | public function perPage(?int $perPage = null): static |
||||
| 69 | { |
||||
| 70 | $this->perPage = $perPage; |
||||
| 71 | |||||
| 72 | return $this; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | protected function preparedQuery() |
||||
| 76 | { |
||||
| 77 | if ($this->withoutFiltration) { |
||||
| 78 | return $this->query; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | if ( |
||||
| 82 | ($search = $this->request->input('search')) |
||||
| 83 | && $this->query->hasNamedScope('searchByText') |
||||
| 84 | ) { |
||||
| 85 | $this->query->searchByText($search, $this->options['search_tag'] ?? null); |
||||
| 86 | } |
||||
| 87 | |||||
| 88 | if ( |
||||
| 89 | ($order = (string) $this->request->input('order')) |
||||
| 90 | && ($orderDirection = (string) $this->request->input('order-direction')) |
||||
| 91 | && $this->query->hasNamedScope('orderIndex') |
||||
| 92 | ) { |
||||
| 93 | $this->query->orderIndex($order, $orderDirection); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | return $this->query; |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | public function links(?array $links = null): static |
||||
| 100 | { |
||||
| 101 | $this->links = $links; |
||||
| 102 | |||||
| 103 | return $this; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | public function columns(array $columns = []): static |
||||
| 107 | { |
||||
| 108 | $this->columns = $columns; |
||||
| 109 | |||||
| 110 | return $this; |
||||
| 111 | } |
||||
| 112 | |||||
| 113 | public function withoutFiltration(bool $withoutFiltration = true): static |
||||
| 114 | { |
||||
| 115 | $this->withoutFiltration = $withoutFiltration; |
||||
| 116 | |||||
| 117 | return $this; |
||||
| 118 | } |
||||
| 119 | } |
||||
| 120 |