1 | <?php |
||
24 | class DataTable { |
||
25 | |||
26 | /** @var Builder */ |
||
27 | private $_queryBuilder; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $_headerFormatters = []; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $_components = []; |
||
34 | |||
35 | /** @var string */ |
||
36 | private $_classes; |
||
37 | |||
38 | /** @var ColumnCollection */ |
||
39 | private $_columns; |
||
40 | |||
41 | /** @var Model */ |
||
42 | private $_model; |
||
43 | |||
44 | /** @var array */ |
||
45 | private $_relations = []; |
||
46 | |||
47 | /** @var string */ |
||
48 | private $_noDataHtml = '<div>no data</div>'; |
||
49 | |||
50 | /** @var Cache */ |
||
51 | private $_cache; |
||
52 | |||
53 | /** |
||
54 | * Set the base model whose data is displayed in the table. |
||
55 | * |
||
56 | * @param string $modelName |
||
57 | * @param array $columns |
||
58 | * @param Cache|null $cache |
||
59 | * @return DataTable |
||
60 | * @throws NotAnActiveRecordException |
||
61 | */ |
||
62 | 72 | public function model(string $modelName, array $columns = [], Cache $cache = null) : DataTable |
|
63 | { |
||
64 | 72 | if (!\is_subclass_of($modelName, Model::class)) |
|
65 | { |
||
66 | 2 | throw new NotAnActiveRecordException($modelName); |
|
67 | } |
||
68 | |||
69 | 70 | $this->_model = new $modelName; |
|
70 | 70 | $this->_queryBuilder = $this->_model->newQuery(); |
|
71 | 70 | $this->_columns = new ColumnCollection($columns, $this->_model); |
|
72 | 70 | $this->_cache = $cache ?? new NoCache(); |
|
73 | |||
74 | 70 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param Cache $cache |
||
79 | * @return DataTable |
||
80 | */ |
||
81 | 1 | public function cache(Cache $cache) : DataTable |
|
82 | { |
||
83 | 1 | $this->_cache = $cache; |
|
84 | |||
85 | 1 | return $this; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return ColumnCollection |
||
90 | */ |
||
91 | 5 | public function getColumns() : ColumnCollection |
|
92 | { |
||
93 | 5 | return $this->_columns; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return Builder |
||
98 | */ |
||
99 | 51 | public function query() : Builder |
|
103 | |||
104 | /** |
||
105 | * Displayed columns |
||
106 | * |
||
107 | * @param array $columns |
||
108 | * @return $this |
||
109 | * @throws UnknownBaseModelException |
||
110 | */ |
||
111 | 3 | 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 | * @param null|string $name |
||
130 | * @return DataTable |
||
131 | * @throws MultipleComponentAssertionException |
||
132 | */ |
||
133 | 33 | public function addComponent(DataComponent $component, ? string $name = null) : DataTable |
|
145 | |||
146 | /** |
||
147 | * @param DataComponent $component |
||
148 | * @param null|string $name |
||
149 | * @return string |
||
150 | */ |
||
151 | 33 | private function _getComponentName(DataComponent $component, string $name = null) : string |
|
160 | |||
161 | /** |
||
162 | * Check whether a component exists for the given data table. |
||
163 | * @param string $componentName |
||
164 | * @return bool |
||
165 | */ |
||
166 | 33 | public function componentExists(string $componentName) : bool |
|
170 | |||
171 | /** |
||
172 | * Add a formatter for the column headers. |
||
173 | * |
||
174 | * @param HeaderFormatter $headerFormatter |
||
175 | * @return DataTable |
||
176 | */ |
||
177 | 12 | public function formatHeaders(HeaderFormatter $headerFormatter) : DataTable |
|
183 | |||
184 | /** |
||
185 | * Add a formatter for a column. |
||
186 | * |
||
187 | * @param string $columnName |
||
188 | * @param ColumnFormatter $columnFormatter |
||
189 | * @return DataTable |
||
190 | * @throws ColumnNotFoundException |
||
191 | */ |
||
192 | 11 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter) : DataTable |
|
209 | |||
210 | /** |
||
211 | * Add classes to the table. |
||
212 | * |
||
213 | * @param string $classes |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | 1 | public function classes(string $classes) : DataTable |
|
223 | |||
224 | /** |
||
225 | * Add a relation to the table. |
||
226 | * |
||
227 | * @param array $relations |
||
228 | * @return DataTable |
||
229 | */ |
||
230 | 4 | public function with(array $relations) : DataTable |
|
236 | |||
237 | /** |
||
238 | * Set the HTML which should be displayed when the dataset is empty. |
||
239 | * |
||
240 | * @param string $html |
||
241 | * @return DataTable |
||
242 | */ |
||
243 | 1 | public function noDataHtml(string $html) : DataTable |
|
249 | |||
250 | /** |
||
251 | * Set a view which should be displayed when the dataset is empty. |
||
252 | * |
||
253 | * @param string $viewName |
||
254 | * @return DataTable |
||
255 | * @throws \Throwable |
||
256 | */ |
||
257 | 1 | public function noDataView(string $viewName) : DataTable |
|
263 | |||
264 | /** |
||
265 | * Renders the table. |
||
266 | * |
||
267 | * @return string |
||
268 | * @throws UnknownBaseModelException |
||
269 | */ |
||
270 | 51 | public function render() : string |
|
292 | |||
293 | /** |
||
294 | * Get data which should be displayed in the table. |
||
295 | * |
||
296 | * @return Collection |
||
297 | */ |
||
298 | 51 | private function _getData() : Collection |
|
310 | |||
311 | 51 | private function _addRelations() : void |
|
326 | |||
327 | /** |
||
328 | * @return DataTable |
||
329 | */ |
||
330 | 51 | private function _setSelection() : DataTable |
|
349 | |||
350 | /** |
||
351 | * @param $name |
||
352 | * @return mixed |
||
353 | */ |
||
354 | 2 | public function __get($name) |
|
363 | } |