1 | <?php |
||
18 | class DataTable { |
||
19 | |||
20 | /** @var Request */ |
||
21 | private $_request; |
||
22 | |||
23 | /** @var Builder */ |
||
24 | private $_queryBuilder; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $_headerFormatters = []; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $_components = []; |
||
31 | |||
32 | /** @var Closure */ |
||
33 | private $_rowRenderer; // TODO: IColumnFormatter => DateColumnFormatter etc. |
||
34 | |||
35 | /** @var string */ |
||
36 | private $_classes; |
||
37 | |||
38 | /** @var array */ |
||
39 | private $_columns = []; |
||
40 | |||
41 | /** @var array */ |
||
42 | private $_relations = []; |
||
43 | |||
44 | /** |
||
45 | * DataTable constructor. |
||
46 | * @param Request $request |
||
47 | */ |
||
48 | 10 | public function __construct(Request $request) |
|
52 | |||
53 | /** |
||
54 | * Set the base model whose data is displayed in the table. |
||
55 | * |
||
56 | * @param string $modelName |
||
57 | * @param array $columns |
||
58 | * @return $this |
||
59 | * @throws \RuntimeException |
||
60 | */ |
||
61 | 10 | public function model(string $modelName, array $columns = []) |
|
78 | |||
79 | /** |
||
80 | * @return Builder |
||
81 | */ |
||
82 | 6 | public function query() |
|
86 | |||
87 | /** |
||
88 | * Displayed columns |
||
89 | * |
||
90 | * @param array $columns |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function columns(array $columns) |
||
99 | |||
100 | /** |
||
101 | * Manipulate each rendered row. |
||
102 | * |
||
103 | * @param Closure $customRowRenderer |
||
104 | * @return $this |
||
105 | */ |
||
106 | 1 | public function renderRow(Closure $customRowRenderer) |
|
112 | |||
113 | /** |
||
114 | * Add a component to the data table. |
||
115 | * For example a "Paginator" or a "Sorter". |
||
116 | * |
||
117 | * @param DataComponent $component |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | 4 | public function addComponent(DataComponent $component) |
|
128 | |||
129 | /** |
||
130 | * Add a formatter for the column headers. |
||
131 | * |
||
132 | * @param HeaderFormatter $headerFormatter |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function formatHeaders(HeaderFormatter $headerFormatter) |
||
141 | |||
142 | /** |
||
143 | * Get data which should be displayed in the table. |
||
144 | * |
||
145 | * @return \Illuminate\Database\Eloquent\Collection |
||
146 | * |
||
147 | * @throws \RuntimeException |
||
148 | */ |
||
149 | 9 | private function _getData() |
|
169 | |||
170 | /** |
||
171 | * Add classes to the table. |
||
172 | * |
||
173 | * @param string $classes |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | 1 | public function classes(string $classes) |
|
183 | |||
184 | /** |
||
185 | * Renders the column headers. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 7 | private function _renderHeaders() |
|
217 | |||
218 | /** |
||
219 | * Displays the table body. |
||
220 | * |
||
221 | * @param Collection $data |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 7 | private function _renderBody(Collection $data) |
|
235 | |||
236 | /** |
||
237 | * Get all the mutated attributes which are needed. |
||
238 | * |
||
239 | * @param Model $model |
||
240 | * @param array $columns |
||
241 | * @return array |
||
242 | */ |
||
243 | 7 | private function _getMutatedAttributes(Model $model, array $columns = []): array |
|
253 | |||
254 | /** |
||
255 | * Displays a single row. |
||
256 | * |
||
257 | * @param Model $rowModel |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 7 | private function _renderRow(Model $rowModel) |
|
279 | |||
280 | /** |
||
281 | * Add a relation to the table. |
||
282 | * |
||
283 | * @param array $relations |
||
284 | * @return $this |
||
285 | */ |
||
286 | public function with(array $relations) |
||
292 | |||
293 | /** |
||
294 | * Starts the table. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 7 | private function _open() |
|
302 | |||
303 | /** |
||
304 | * Closes the table. |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 7 | private function _close() |
|
312 | |||
313 | /** |
||
314 | * Renders the table. |
||
315 | * |
||
316 | * @param null|Closure $noDataView |
||
317 | * |
||
318 | * @return string |
||
319 | * @throws \RuntimeException |
||
320 | */ |
||
321 | 9 | public function render(Closure $noDataView = null) |
|
332 | } |