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 $_components = []; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $_classes; |
||
35 | |||
36 | /** @var array */ |
||
37 | private $_columns = []; |
||
38 | |||
39 | /** @var array */ |
||
40 | private $_relations = []; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $_noDataHtml = '<div>no data</div>'; |
||
44 | |||
45 | /** |
||
46 | * DataTable constructor. |
||
47 | * @param Request $request |
||
48 | */ |
||
49 | 46 | 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 | 45 | public function model(string $modelName, array $columns = []) : DataTable |
|
74 | |||
75 | /** |
||
76 | * Returns an array of Column objects which may be bound to a formatter. |
||
77 | * |
||
78 | * @param array $columns |
||
79 | * @return array |
||
80 | */ |
||
81 | 44 | private function _fetchColumns(array $columns) : array |
|
92 | |||
93 | /** |
||
94 | * @param $column |
||
95 | * @param $formatter |
||
96 | * @return array |
||
97 | */ |
||
98 | 38 | private function _setColumnFormatter($column, $formatter) : array |
|
108 | |||
109 | /** |
||
110 | * @return Builder |
||
111 | */ |
||
112 | 19 | public function query() : Builder |
|
116 | |||
117 | /** |
||
118 | * Displayed columns |
||
119 | * |
||
120 | * @param array $columns |
||
121 | * @return $this |
||
122 | */ |
||
123 | 2 | 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 | * @return $this |
||
137 | */ |
||
138 | 23 | public function addComponent(DataComponent $component) : DataTable |
|
145 | |||
146 | /** |
||
147 | * Add a formatter for the column headers. |
||
148 | * |
||
149 | * @param HeaderFormatter $headerFormatter |
||
150 | * @return $this |
||
151 | */ |
||
152 | 8 | public function formatHeaders(HeaderFormatter $headerFormatter) : DataTable |
|
158 | |||
159 | /** |
||
160 | * Add a formatter for a column. |
||
161 | * |
||
162 | * @param string $columnName |
||
163 | * @param ColumnFormatter $columnFormatter |
||
164 | * @return DataTable |
||
165 | */ |
||
166 | 2 | public function formatColumn(string $columnName, ColumnFormatter $columnFormatter) : DataTable |
|
185 | |||
186 | /** |
||
187 | * Add classes to the table. |
||
188 | * |
||
189 | * @param string $classes |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | 1 | public function classes(string $classes) : DataTable |
|
199 | |||
200 | /** |
||
201 | * Add a relation to the table. |
||
202 | * |
||
203 | * @param array $relations |
||
204 | * @return $this |
||
205 | */ |
||
206 | 3 | public function with(array $relations) : DataTable |
|
212 | |||
213 | /** |
||
214 | * Set the HTML which should be displayed when the dataset is empty. |
||
215 | * |
||
216 | * @param string $html |
||
217 | * @return DataTable |
||
218 | */ |
||
219 | 1 | public function noDataHtml(string $html) : DataTable |
|
225 | |||
226 | /** |
||
227 | * Set a view which should be displayed when the dataset is empty. |
||
228 | * |
||
229 | * @param string $viewName |
||
230 | * @return DataTable |
||
231 | */ |
||
232 | 1 | public function noDataView(string $viewName) : DataTable |
|
238 | |||
239 | /** |
||
240 | * Renders the table. |
||
241 | * |
||
242 | * @return string |
||
243 | * @throws \RuntimeException |
||
244 | */ |
||
245 | 34 | public function render() : string |
|
261 | |||
262 | /** |
||
263 | * Get data which should be displayed in the table. |
||
264 | * |
||
265 | * @return Collection |
||
266 | * |
||
267 | * @throws \RuntimeException |
||
268 | */ |
||
269 | 34 | private function _getData() : Collection |
|
286 | |||
287 | 33 | private function _addRelations() |
|
294 | |||
295 | 29 | private function _initColumns() |
|
302 | |||
303 | /** |
||
304 | * @return array |
||
305 | */ |
||
306 | 29 | private function _fetchHeaders() : array |
|
317 | } |