1 | <?php |
||
19 | class DataTable { |
||
20 | |||
21 | /** @var Request */ |
||
22 | private $_request; |
||
23 | |||
24 | /** @var Builder */ |
||
25 | private $_queryBuilder; |
||
26 | |||
27 | /** @var array */ |
||
28 | private $_headerFormatters = []; |
||
29 | |||
30 | /** @var array */ |
||
31 | private $_columnFormatters = []; |
||
32 | |||
33 | /** @var array */ |
||
34 | private $_components = []; |
||
35 | |||
36 | /** @var string */ |
||
37 | private $_classes; |
||
38 | |||
39 | /** @var array */ |
||
40 | private $_columns = []; |
||
41 | |||
42 | /** @var array */ |
||
43 | private $_relations = []; |
||
44 | |||
45 | /** |
||
46 | * DataTable constructor. |
||
47 | * @param Request $request |
||
48 | */ |
||
49 | 34 | public function __construct(Request $request) |
|
53 | |||
54 | /** |
||
55 | * Set the base model whose data is displayed in the table. |
||
56 | * |
||
57 | * @param string $modelName |
||
58 | * @param array $columns |
||
59 | * @return $this |
||
60 | * @throws \RuntimeException |
||
61 | */ |
||
62 | 33 | public function model(string $modelName, array $columns = []): DataTable |
|
79 | |||
80 | /** |
||
81 | * Returns an array of Column objects which may be bound to a formatter. |
||
82 | * |
||
83 | * @param array $columns |
||
84 | * @return array |
||
85 | */ |
||
86 | 32 | private function _fetchColumns(array $columns): array |
|
101 | |||
102 | /** |
||
103 | * @return Builder |
||
104 | */ |
||
105 | 14 | public function query(): Builder |
|
109 | |||
110 | /** |
||
111 | * Displayed columns |
||
112 | * |
||
113 | * @param array $columns |
||
114 | * @return $this |
||
115 | */ |
||
116 | 1 | public function columns(array $columns): DataTable |
|
122 | |||
123 | /** |
||
124 | * Add a component to the data table. |
||
125 | * For example a "Paginator" or a "Sorter". |
||
126 | * |
||
127 | * @param DataComponent $component |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | 18 | public function addComponent(DataComponent $component): DataTable |
|
138 | |||
139 | /** |
||
140 | * Add a formatter for the column headers. |
||
141 | * |
||
142 | * @param HeaderFormatter $headerFormatter |
||
143 | * @return $this |
||
144 | */ |
||
145 | 6 | public function formatHeaders(HeaderFormatter $headerFormatter): DataTable |
|
151 | |||
152 | /** |
||
153 | * Add a formatter for a column. |
||
154 | * |
||
155 | * @param string $columnName |
||
156 | * @param ColumnFormatter $columnFormatter |
||
157 | * @return DataTable |
||
158 | */ |
||
159 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter): DataTable |
|
177 | |||
178 | /** |
||
179 | * Add classes to the table. |
||
180 | * |
||
181 | * @param string $classes |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 1 | public function classes(string $classes): DataTable |
|
191 | |||
192 | /** |
||
193 | * Add a relation to the table. |
||
194 | * |
||
195 | * @param array $relations |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function with(array $relations): DataTable |
||
204 | |||
205 | /** |
||
206 | * Renders the table. |
||
207 | * |
||
208 | * @param null|Closure $noDataView |
||
209 | * |
||
210 | * @return string |
||
211 | * @throws \RuntimeException |
||
212 | */ |
||
213 | 26 | public function render(Closure $noDataView = null): string |
|
224 | |||
225 | /** |
||
226 | * Get data which should be displayed in the table. |
||
227 | * |
||
228 | * @return Collection |
||
229 | * |
||
230 | * @throws \RuntimeException |
||
231 | */ |
||
232 | 26 | private function _getData(): Collection |
|
252 | |||
253 | /** |
||
254 | * Starts the table. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 22 | private function _open(): string |
|
262 | |||
263 | /** |
||
264 | * Renders the column headers. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 22 | private function _renderHeaders(): string |
|
296 | |||
297 | /** |
||
298 | * Displays the table body. |
||
299 | * |
||
300 | * @param Collection $data |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 22 | private function _renderBody(Collection $data): string |
|
314 | |||
315 | /** |
||
316 | * Displays a single row. |
||
317 | * |
||
318 | * @param Model $rowModel |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 22 | private function _renderRow(Model $rowModel): string |
|
336 | |||
337 | /** |
||
338 | * Get all the mutated attributes which are needed. |
||
339 | * |
||
340 | * @param Model $model |
||
341 | * @param array $columns |
||
342 | * @return array |
||
343 | */ |
||
344 | 22 | private function _getMutatedAttributes(Model $model, array $columns = []): array |
|
354 | |||
355 | /** |
||
356 | * Get all column names. |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | private function _getColumnNames(): array |
||
364 | |||
365 | /** |
||
366 | * Closes the table. |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | 22 | private function _close(): string |
|
374 | } |