1 | <?php |
||
19 | class DataTable { |
||
20 | |||
21 | /** @var Builder */ |
||
22 | private $_queryBuilder; |
||
23 | |||
24 | /** @var array */ |
||
25 | private $_headerFormatters = []; |
||
26 | |||
27 | /** @var array */ |
||
28 | private $_components = []; |
||
29 | |||
30 | /** @var string */ |
||
31 | private $_classes; |
||
32 | |||
33 | /** @var array */ |
||
34 | private $_columns = []; |
||
35 | |||
36 | /** @var Model */ |
||
37 | private $_model; |
||
38 | |||
39 | /** @var array */ |
||
40 | private $_relations = []; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $_noDataHtml = '<div>no data</div>'; |
||
44 | |||
45 | /** |
||
46 | * Set the base model whose data is displayed in the table. |
||
47 | * |
||
48 | * @param string $modelName |
||
49 | * @param array $columns |
||
50 | * @return $this |
||
51 | * @throws \RuntimeException |
||
52 | */ |
||
53 | 59 | public function model(string $modelName, array $columns = []) : DataTable |
|
54 | { |
||
55 | 59 | if (!\is_subclass_of($modelName, Model::class)) |
|
56 | { |
||
57 | 2 | throw new RuntimeException('Class "' . $modelName . '" is not an active record!'); |
|
58 | } |
||
59 | |||
60 | 57 | $this->_model = new $modelName; |
|
61 | 57 | $this->_queryBuilder = $this->_model->newQuery(); |
|
62 | 57 | $this->_columns = $this->_fetchColumns($columns); |
|
63 | |||
64 | 57 | return $this; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Returns an array of Column objects which may be bound to a formatter. |
||
69 | * |
||
70 | * @param array $columns |
||
71 | * @return array |
||
72 | */ |
||
73 | 58 | private function _fetchColumns(array $columns) : array |
|
74 | { |
||
75 | 58 | $columnModels = []; |
|
76 | 58 | foreach ($columns as $column => $formatter) |
|
77 | { |
||
78 | 45 | [$column, $formatter] = $this->_setColumnFormatter($column, $formatter); |
|
79 | 45 | $columnModels[] = new Column($column, $formatter, $this->_model); |
|
80 | } |
||
81 | |||
82 | 58 | return $columnModels; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $column |
||
87 | * @param $formatter |
||
88 | * @return array |
||
89 | */ |
||
90 | 45 | private function _setColumnFormatter($column, $formatter) : array |
|
91 | { |
||
92 | 45 | if (\is_int($column)) |
|
93 | { |
||
94 | 43 | $column = $formatter; |
|
95 | 43 | $formatter = null; |
|
96 | } |
||
97 | |||
98 | 45 | return [$column, $formatter]; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array |
||
103 | */ |
||
104 | 5 | public function getColumns() : array |
|
105 | { |
||
106 | 5 | return $this->_columns; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return Builder |
||
111 | */ |
||
112 | 39 | public function query() : Builder |
|
116 | |||
117 | /** |
||
118 | * Displayed columns |
||
119 | * |
||
120 | * @param array $columns |
||
121 | * @return $this |
||
122 | */ |
||
123 | 3 | public function columns(array $columns) : DataTable |
|
129 | |||
130 | /** |
||
131 | * Add a component to the data table. |
||
132 | * For example a "Paginator" or a "Sorter". |
||
133 | * |
||
134 | * @param DataComponent $component |
||
135 | * |
||
136 | * @param null|string $name |
||
137 | * @return DataTable |
||
138 | * @throws \hamburgscleanest\DataTables\Exceptions\MultipleComponentAssertionException |
||
139 | */ |
||
140 | 32 | public function addComponent(DataComponent $component, ? string $name = null) : DataTable |
|
152 | |||
153 | /** |
||
154 | * @param DataComponent $component |
||
155 | * @param null|string $name |
||
156 | * @return string |
||
157 | */ |
||
158 | 32 | private function _getComponentName(DataComponent $component, ? string $name = null) : string |
|
167 | |||
168 | /** |
||
169 | * Check whether a component exists for the given data table. |
||
170 | * @param string $componentName |
||
171 | * @return bool |
||
172 | */ |
||
173 | 32 | public function componentExists(string $componentName) : bool |
|
177 | |||
178 | /** |
||
179 | * Add a formatter for the column headers. |
||
180 | * |
||
181 | * @param HeaderFormatter $headerFormatter |
||
182 | * @return DataTable |
||
183 | */ |
||
184 | 12 | public function formatHeaders(HeaderFormatter $headerFormatter) : DataTable |
|
190 | |||
191 | /** |
||
192 | * Add a formatter for a column. |
||
193 | * |
||
194 | * @param string $columnName |
||
195 | * @param ColumnFormatter $columnFormatter |
||
196 | * @return DataTable |
||
197 | */ |
||
198 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter) : DataTable |
|
216 | |||
217 | /** |
||
218 | * Add classes to the table. |
||
219 | * |
||
220 | * @param string $classes |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | 1 | public function classes(string $classes) : DataTable |
|
230 | |||
231 | /** |
||
232 | * Add a relation to the table. |
||
233 | * |
||
234 | * @param array $relations |
||
235 | * @return DataTable |
||
236 | */ |
||
237 | 4 | public function with(array $relations) : DataTable |
|
243 | |||
244 | /** |
||
245 | * Set the HTML which should be displayed when the dataset is empty. |
||
246 | * |
||
247 | * @param string $html |
||
248 | * @return DataTable |
||
249 | */ |
||
250 | 1 | public function noDataHtml(string $html) : DataTable |
|
256 | |||
257 | /** |
||
258 | * Set a view which should be displayed when the dataset is empty. |
||
259 | * |
||
260 | * @param string $viewName |
||
261 | * @return DataTable |
||
262 | * @throws \Throwable |
||
263 | */ |
||
264 | 1 | public function noDataView(string $viewName) : DataTable |
|
270 | |||
271 | /** |
||
272 | * Renders the table. |
||
273 | * |
||
274 | * @return string |
||
275 | * @throws \RuntimeException |
||
276 | */ |
||
277 | 41 | public function render() : string |
|
291 | |||
292 | /** |
||
293 | * Get data which should be displayed in the table. |
||
294 | * |
||
295 | * @return Collection |
||
296 | * |
||
297 | * @throws \RuntimeException |
||
298 | */ |
||
299 | 41 | private function _getData() : Collection |
|
316 | |||
317 | 40 | private function _addRelations() : void |
|
332 | |||
333 | /** |
||
334 | * @return DataTable |
||
335 | */ |
||
336 | 40 | private function _setSelection() : DataTable |
|
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | 40 | private function _getColumnsForSelect() : array |
|
369 | |||
370 | /** |
||
371 | * @return array |
||
372 | */ |
||
373 | 36 | private function _fetchHeaders() : array |
|
383 | |||
384 | /** |
||
385 | * @param $name |
||
386 | * @return mixed |
||
387 | */ |
||
388 | 2 | public function __get($name) |
|
397 | } |