1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Bluz Framework Component |
||
5 | * |
||
6 | * @copyright Bluz PHP Team |
||
7 | * @link https://github.com/bluzphp/framework |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Bluz\Grid; |
||
13 | |||
14 | use Bluz\Common\Exception\CommonException; |
||
15 | use Bluz\Common\Helper; |
||
16 | use Bluz\Common\Options; |
||
17 | use Bluz\Proxy\Request; |
||
18 | use Bluz\Proxy\Router; |
||
19 | use Exception; |
||
20 | |||
21 | /** |
||
22 | * Grid |
||
23 | * |
||
24 | * @package Bluz\Grid |
||
25 | * @author Anton Shevchuk |
||
26 | * @link https://github.com/bluzphp/framework/wiki/Grid |
||
27 | * |
||
28 | * @method string filter($column, $filter, $value, $reset = true) |
||
29 | * @method string first() |
||
30 | * @method string last() |
||
31 | * @method string limit($limit = 25) |
||
32 | * @method string next() |
||
33 | * @method string order($column, $order = null, $defaultOrder = Grid::ORDER_ASC, $reset = true) |
||
34 | * @method string page($page = 1) |
||
35 | * @method string pages() |
||
36 | * @method string prev() |
||
37 | * @method string reset() |
||
38 | * @method string total() |
||
39 | */ |
||
40 | abstract class Grid |
||
41 | { |
||
42 | use Options; |
||
43 | use Helper; |
||
44 | |||
45 | public const ORDER_ASC = 'asc'; |
||
46 | public const ORDER_DESC = 'desc'; |
||
47 | |||
48 | public const FILTER_LIKE = 'like'; // like |
||
49 | public const FILTER_ENUM = 'enum'; // one from .., .., .. |
||
50 | |||
51 | public const FILTER_EQ = 'eq'; // equal to .. |
||
52 | public const FILTER_NE = 'ne'; // not equal to .. |
||
53 | public const FILTER_GT = 'gt'; // greater than .. |
||
54 | public const FILTER_GE = 'ge'; // greater than .. or equal |
||
55 | public const FILTER_LT = 'lt'; // less than .. |
||
56 | public const FILTER_LE = 'le'; // less than .. or equal |
||
57 | |||
58 | /** |
||
59 | * @var Source\AbstractSource instance of Source |
||
60 | */ |
||
61 | protected $adapter; |
||
62 | |||
63 | /** |
||
64 | * @var Data instance of Data |
||
65 | */ |
||
66 | protected $data; |
||
67 | |||
68 | /** |
||
69 | * @var string unique identification of grid |
||
70 | */ |
||
71 | protected $uid; |
||
72 | |||
73 | /** |
||
74 | * @var string unique prefix of grid |
||
75 | */ |
||
76 | protected $prefix = ''; |
||
77 | |||
78 | /** |
||
79 | * @var string location of Grid |
||
80 | */ |
||
81 | protected $module; |
||
82 | |||
83 | /** |
||
84 | * @var string location of Grid |
||
85 | */ |
||
86 | protected $controller; |
||
87 | |||
88 | /** |
||
89 | * @var array custom array params |
||
90 | */ |
||
91 | protected $params = []; |
||
92 | |||
93 | /** |
||
94 | * @var integer start from first page |
||
95 | */ |
||
96 | protected $page = 1; |
||
97 | |||
98 | /** |
||
99 | * @var integer limit per page |
||
100 | */ |
||
101 | protected $limit = 25; |
||
102 | |||
103 | /** |
||
104 | * @var integer default value of page limit |
||
105 | * @see Grid::$limit |
||
106 | */ |
||
107 | protected $defaultLimit = 25; |
||
108 | |||
109 | /** |
||
110 | * List of orders |
||
111 | * |
||
112 | * Example |
||
113 | * 'first' => 'ASC', |
||
114 | * 'last' => 'ASC' |
||
115 | * |
||
116 | * @var array |
||
117 | */ |
||
118 | protected $orders = []; |
||
119 | |||
120 | /** |
||
121 | * @var array default order |
||
122 | * @see Grid::$orders |
||
123 | */ |
||
124 | protected $defaultOrder = []; |
||
125 | |||
126 | /** |
||
127 | * @var array list of allow orders |
||
128 | * @see Grid::$orders |
||
129 | */ |
||
130 | protected $allowOrders = []; |
||
131 | |||
132 | /** |
||
133 | * @var array list of filters |
||
134 | */ |
||
135 | protected $filters = []; |
||
136 | |||
137 | /** |
||
138 | * List of allow filters |
||
139 | * |
||
140 | * Example |
||
141 | * ['id', 'status' => ['active', 'disable']] |
||
142 | * |
||
143 | * @var array |
||
144 | * @see Grid::$filters |
||
145 | */ |
||
146 | protected $allowFilters = []; |
||
147 | |||
148 | /** |
||
149 | * List of allow filter names |
||
150 | * |
||
151 | * @var array |
||
152 | * @see Grid::$filters |
||
153 | */ |
||
154 | protected $allowFilterNames = [ |
||
155 | self::FILTER_LIKE, |
||
156 | self::FILTER_ENUM, |
||
157 | self::FILTER_EQ, |
||
158 | self::FILTER_NE, |
||
159 | self::FILTER_GT, |
||
160 | self::FILTER_GE, |
||
161 | self::FILTER_LT, |
||
162 | self::FILTER_LE |
||
163 | ]; |
||
164 | |||
165 | /** |
||
166 | * List of aliases for columns in DB |
||
167 | * |
||
168 | * @var array |
||
169 | */ |
||
170 | protected $aliases = []; |
||
171 | |||
172 | /** |
||
173 | * Grid constructor |
||
174 | * |
||
175 | * @param array|null $options |
||
176 | * |
||
177 | * @throws CommonException |
||
178 | */ |
||
179 | 32 | public function __construct(array $options = null) |
|
180 | { |
||
181 | // initial default helper path |
||
182 | 32 | $this->addHelperPath(__DIR__ . '/Helper/'); |
|
183 | |||
184 | 32 | if ($options) { |
|
185 | $this->setOptions($options); |
||
186 | } |
||
187 | |||
188 | 32 | if ($this->getUid()) { |
|
189 | 32 | $this->prefix = $this->getUid() . '-'; |
|
190 | } |
||
191 | |||
192 | 32 | $this->init(); |
|
193 | |||
194 | 32 | $this->processRequest(); |
|
195 | 32 | } |
|
196 | |||
197 | /** |
||
198 | * Initialize Grid |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | abstract public function init(): void; |
||
203 | |||
204 | /** |
||
205 | * Set source adapter |
||
206 | * |
||
207 | * @param Source\AbstractSource $adapter |
||
208 | * |
||
209 | * @return void |
||
210 | */ |
||
211 | 32 | public function setAdapter(Source\AbstractSource $adapter): void |
|
212 | { |
||
213 | 32 | $this->adapter = $adapter; |
|
214 | 32 | } |
|
215 | |||
216 | /** |
||
217 | * Get source adapter |
||
218 | * |
||
219 | * @return Source\AbstractSource |
||
220 | * @throws GridException |
||
221 | */ |
||
222 | 15 | public function getAdapter(): Source\AbstractSource |
|
223 | { |
||
224 | 15 | if (null === $this->adapter) { |
|
225 | throw new GridException('Grid adapter is not initialized'); |
||
226 | } |
||
227 | 15 | return $this->adapter; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get unique Grid Id |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 32 | public function getUid(): string |
|
236 | { |
||
237 | 32 | return $this->uid; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Get prefix |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 1 | public function getPrefix(): string |
|
246 | { |
||
247 | 1 | return $this->prefix; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Set module |
||
252 | * |
||
253 | * @param string $module |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | 1 | public function setModule(string $module): void |
|
258 | { |
||
259 | 1 | $this->module = $module; |
|
260 | 1 | } |
|
261 | |||
262 | /** |
||
263 | * Get module |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | 13 | public function getModule(): ?string |
|
268 | { |
||
269 | 13 | return $this->module; |
|
270 | } |
||
271 | |||
272 | /** |
||
273 | * Set controller |
||
274 | * |
||
275 | * @param string $controller |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | 1 | public function setController(string $controller): void |
|
280 | { |
||
281 | 1 | $this->controller = $controller; |
|
282 | 1 | } |
|
283 | |||
284 | /** |
||
285 | * Get controller |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | 13 | public function getController(): ?string |
|
290 | { |
||
291 | 13 | return $this->controller; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * Process request |
||
296 | * |
||
297 | * Example of request url |
||
298 | * - http://domain.com/pages/grid/ |
||
299 | * - http://domain.com/pages/grid/page/2/ |
||
300 | * - http://domain.com/pages/grid/page/2/order-alias/desc/ |
||
301 | * - http://domain.com/pages/grid/page/2/order-created/desc/order-alias/asc/ |
||
302 | * |
||
303 | * with prefix for support more than one grid on page |
||
304 | * - http://domain.com/users/grid/users-page/2/users-order-created/desc/ |
||
305 | * - http://domain.com/users/grid/users-page/2/users-filter-status/active/ |
||
306 | * |
||
307 | * hash support |
||
308 | * - http://domain.com/pages/grid/#/page/2/order-created/desc/order-alias/asc/ |
||
309 | * |
||
310 | * @return void |
||
311 | * @throws GridException |
||
312 | */ |
||
313 | 32 | public function processRequest(): void |
|
314 | { |
||
315 | 32 | $this->module = Request::getModule(); |
|
316 | 32 | $this->controller = Request::getController(); |
|
317 | |||
318 | 32 | $page = (int)Request::getParam($this->prefix . 'page', 1); |
|
319 | 32 | $this->setPage($page); |
|
320 | |||
321 | 32 | $limit = (int)Request::getParam($this->prefix . 'limit', $this->limit); |
|
322 | 32 | $this->setLimit($limit); |
|
323 | |||
324 | 32 | foreach ($this->allowOrders as $column) { |
|
325 | 32 | $alias = $this->applyAlias($column); |
|
326 | 32 | $order = Request::getParam($this->prefix . 'order-' . $alias); |
|
327 | 32 | if (is_array($order)) { |
|
328 | $order = current($order); |
||
329 | } |
||
330 | 32 | if (null !== $order) { |
|
331 | 1 | $this->addOrder($column, $order); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
332 | } |
||
333 | } |
||
334 | 32 | foreach ($this->allowFilters as $column) { |
|
335 | 32 | $alias = $this->applyAlias($column); |
|
336 | 32 | $filters = (array)Request::getParam($this->prefix . 'filter-' . $alias, []); |
|
337 | |||
338 | 32 | foreach ($filters as $filter) { |
|
339 | 1 | $filter = trim($filter, ' _-'); |
|
340 | 1 | if (strpos($filter, '-')) { |
|
341 | /** |
||
342 | * Example of filters |
||
343 | * - http://domain.com/users/grid/users-filter-roleId/gt-2 - roleId greater than 2 |
||
344 | * - http://domain.com/users/grid/users-filter-roleId/gt-1_lt-4 - 1 < roleId < 4 |
||
345 | * - http://domain.com/users/grid/users-filter-login/eq-admin - login == admin |
||
346 | * - http://domain.com/users/grid/users-filter-login/like-adm - login LIKE `adm` |
||
347 | * - http://domain.com/users/grid/users-filter-login/like-od- - login LIKE `od-` |
||
348 | */ |
||
349 | 1 | $filters = explode('_', $filter); |
|
350 | 1 | foreach ($filters as $rawFilter) { |
|
351 | 1 | [$filterName, $filterValue] = explode('-', $rawFilter, 2); |
|
352 | 1 | $this->addFilter($column, $filterName, urldecode($filterValue)); |
|
353 | } |
||
354 | } else { |
||
355 | /** |
||
356 | * Example of filters |
||
357 | * - http://domain.com/users/grid/users-filter-roleId/2 |
||
358 | * - http://domain.com/users/grid/users-filter-login/admin |
||
359 | */ |
||
360 | 1 | $this->addFilter($column, self::FILTER_EQ, $filter); |
|
361 | } |
||
362 | } |
||
363 | } |
||
364 | 32 | } |
|
365 | |||
366 | /** |
||
367 | * Process source |
||
368 | * |
||
369 | * @return void |
||
370 | * @throws GridException |
||
371 | */ |
||
372 | 15 | public function processSource(): void |
|
373 | { |
||
374 | 15 | if (null === $this->adapter) { |
|
375 | throw new GridException('Grid Adapter is not initiated, please update method `init()` and try again'); |
||
376 | } |
||
377 | |||
378 | try { |
||
379 | 15 | $this->data = $this->getAdapter()->process( |
|
380 | 15 | $this->getPage(), |
|
381 | 15 | $this->getLimit(), |
|
382 | 15 | $this->getFilters(), |
|
383 | 15 | $this->getOrders() |
|
384 | ); |
||
385 | } catch (Exception $e) { |
||
386 | throw new GridException('Grid Adapter can\'t process request: ' . $e->getMessage()); |
||
387 | } |
||
388 | 15 | } |
|
389 | |||
390 | /** |
||
391 | * Get data |
||
392 | * |
||
393 | * @return Data |
||
394 | * @throws GridException |
||
395 | */ |
||
396 | 15 | public function getData(): Data |
|
397 | { |
||
398 | 15 | if (!$this->data) { |
|
399 | 15 | $this->processSource(); |
|
400 | } |
||
401 | 15 | return $this->data; |
|
402 | } |
||
403 | |||
404 | /** |
||
405 | * Setup params |
||
406 | * |
||
407 | * @param $params |
||
408 | * |
||
409 | * @return void |
||
410 | */ |
||
411 | public function setParams($params): void |
||
412 | { |
||
413 | $this->params = $params; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Return params prepared for url builder |
||
418 | * |
||
419 | * @param array $rewrite |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | 13 | public function getParams(array $rewrite = []): array |
|
424 | { |
||
425 | 13 | $params = $this->params; |
|
426 | |||
427 | // change page to first for each new grid (with new filters or orders, or other stuff) |
||
428 | 13 | $page = $rewrite['page'] ?? 1; |
|
429 | |||
430 | 13 | if ($page > 1) { |
|
431 | 4 | $params[$this->prefix . 'page'] = $page; |
|
432 | } |
||
433 | |||
434 | // change limit |
||
435 | 13 | $limit = $rewrite['limit'] ?? $this->getLimit(); |
|
436 | |||
437 | 13 | if ($limit !== $this->defaultLimit) { |
|
438 | 1 | $params[$this->prefix . 'limit'] = $limit; |
|
439 | } |
||
440 | |||
441 | // change orders |
||
442 | 13 | $orders = $rewrite['orders'] ?? $this->getOrders(); |
|
443 | |||
444 | 13 | foreach ($orders as $column => $order) { |
|
445 | 3 | $column = $this->applyAlias($column); |
|
446 | 3 | $params[$this->prefix . 'order-' . $column] = $order; |
|
447 | } |
||
448 | |||
449 | // change filters |
||
450 | 13 | $filters = $rewrite['filters'] ?? $this->getFilters(); |
|
451 | |||
452 | 13 | foreach ($filters as $column => $columnFilters) { |
|
453 | /** @var array $columnFilters */ |
||
454 | 3 | $column = $this->applyAlias($column); |
|
455 | 3 | if (count($columnFilters) === 1 && isset($columnFilters[self::FILTER_EQ])) { |
|
456 | 2 | $params[$this->prefix . 'filter-' . $column] = $columnFilters[self::FILTER_EQ]; |
|
457 | 2 | continue; |
|
458 | } |
||
459 | |||
460 | 2 | $columnFilter = []; |
|
461 | 2 | foreach ($columnFilters as $filterName => $filterValue) { |
|
462 | 2 | $columnFilter[] = $filterName . '-' . $filterValue; |
|
463 | } |
||
464 | 2 | $params[$this->prefix . 'filter-' . $column] = implode('_', $columnFilter); |
|
465 | } |
||
466 | 13 | return $params; |
|
467 | } |
||
468 | |||
469 | /** |
||
470 | * Get Url |
||
471 | * |
||
472 | * @param array $params |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | 13 | public function getUrl(array $params): string |
|
477 | { |
||
478 | // prepare params |
||
479 | 13 | $params = $this->getParams($params); |
|
480 | |||
481 | // retrieve URL |
||
482 | 13 | return Router::getUrl( |
|
483 | 13 | $this->getModule(), |
|
484 | 13 | $this->getController(), |
|
485 | $params |
||
486 | ); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Add column name for allow order |
||
491 | * |
||
492 | * @param string $column |
||
493 | * |
||
494 | * @return void |
||
495 | */ |
||
496 | 32 | public function addAllowOrder(string $column): void |
|
497 | { |
||
498 | 32 | $this->allowOrders[] = $column; |
|
499 | 32 | } |
|
500 | |||
501 | /** |
||
502 | * Set allow orders |
||
503 | * |
||
504 | * @param string[] $orders |
||
505 | * |
||
506 | * @return void |
||
507 | */ |
||
508 | 32 | public function setAllowOrders(array $orders = []): void |
|
509 | { |
||
510 | 32 | $this->allowOrders = []; |
|
511 | 32 | foreach ($orders as $column) { |
|
512 | 32 | $this->addAllowOrder($column); |
|
513 | } |
||
514 | 32 | } |
|
515 | |||
516 | /** |
||
517 | * Get allow orders |
||
518 | * |
||
519 | * @return array |
||
520 | */ |
||
521 | 3 | public function getAllowOrders(): array |
|
522 | { |
||
523 | 3 | return $this->allowOrders; |
|
524 | } |
||
525 | |||
526 | /** |
||
527 | * Check order column |
||
528 | * |
||
529 | * @param string $column |
||
530 | * |
||
531 | * @return bool |
||
532 | */ |
||
533 | 3 | protected function checkOrderColumn(string $column): bool |
|
534 | { |
||
535 | 3 | return in_array($column, $this->getAllowOrders(), true); |
|
536 | } |
||
537 | |||
538 | /** |
||
539 | * Check order name |
||
540 | * |
||
541 | * @param string $order |
||
542 | * |
||
543 | * @return bool |
||
544 | */ |
||
545 | 2 | protected function checkOrderName(string $order): bool |
|
546 | { |
||
547 | 2 | return ($order === self::ORDER_ASC || $order === self::ORDER_DESC); |
|
548 | } |
||
549 | |||
550 | /** |
||
551 | * Add order rule |
||
552 | * |
||
553 | * @param string $column |
||
554 | * @param string $order |
||
555 | * |
||
556 | * @return void |
||
557 | * @throws GridException |
||
558 | */ |
||
559 | 3 | public function addOrder(string $column, string $order = self::ORDER_ASC): void |
|
560 | { |
||
561 | 3 | if (!$this->checkOrderColumn($column)) { |
|
562 | throw new GridException("Order for column `$column` is not allowed"); |
||
563 | } |
||
564 | |||
565 | 3 | if (!$this->checkOrderName($order)) { |
|
566 | throw new GridException("Order name for column `$column` is incorrect"); |
||
567 | } |
||
568 | |||
569 | 3 | $this->orders[$column] = $order; |
|
570 | 3 | } |
|
571 | |||
572 | /** |
||
573 | * Add order rules |
||
574 | * |
||
575 | * @param array $orders |
||
576 | * |
||
577 | * @return void |
||
578 | * @throws GridException |
||
579 | */ |
||
580 | 1 | public function addOrders(array $orders): void |
|
581 | { |
||
582 | 1 | foreach ($orders as $column => $order) { |
|
583 | 1 | $this->addOrder($column, $order); |
|
584 | } |
||
585 | 1 | } |
|
586 | |||
587 | /** |
||
588 | * Set order |
||
589 | * |
||
590 | * @param string $column |
||
591 | * @param string $order ASC or DESC |
||
592 | * |
||
593 | * @return void |
||
594 | * @throws GridException |
||
595 | */ |
||
596 | 1 | public function setOrder(string $column, string $order = self::ORDER_ASC): void |
|
597 | { |
||
598 | 1 | $this->orders = []; |
|
599 | 1 | $this->addOrder($column, $order); |
|
600 | 1 | } |
|
601 | |||
602 | /** |
||
603 | * Set orders |
||
604 | * |
||
605 | * @param array $orders |
||
606 | * |
||
607 | * @return void |
||
608 | * @throws GridException |
||
609 | */ |
||
610 | 1 | public function setOrders(array $orders): void |
|
611 | { |
||
612 | 1 | $this->orders = []; |
|
613 | 1 | $this->addOrders($orders); |
|
614 | 1 | } |
|
615 | |||
616 | /** |
||
617 | * Get orders |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | 25 | public function getOrders(): array |
|
622 | { |
||
623 | 25 | if (empty($this->orders)) { |
|
624 | 22 | return $this->getDefaultOrder(); |
|
625 | } |
||
626 | |||
627 | 3 | return $this->orders; |
|
628 | } |
||
629 | |||
630 | /** |
||
631 | * Add column name to allow filter it |
||
632 | * |
||
633 | * @param string $column |
||
634 | * |
||
635 | * @return void |
||
636 | */ |
||
637 | 32 | public function addAllowFilter(string $column): void |
|
638 | { |
||
639 | 32 | $this->allowFilters[] = $column; |
|
640 | 32 | } |
|
641 | |||
642 | /** |
||
643 | * Set allowed filters |
||
644 | * |
||
645 | * @param string[] $filters |
||
646 | * |
||
647 | * @return void |
||
648 | */ |
||
649 | 32 | public function setAllowFilters(array $filters = []): void |
|
650 | { |
||
651 | 32 | $this->allowFilters = []; |
|
652 | 32 | foreach ($filters as $column) { |
|
653 | 32 | $this->addAllowFilter($column); |
|
654 | } |
||
655 | 32 | } |
|
656 | |||
657 | /** |
||
658 | * Get allow filters |
||
659 | * |
||
660 | * @return array |
||
661 | */ |
||
662 | 12 | public function getAllowFilters(): array |
|
663 | { |
||
664 | 12 | return $this->allowFilters; |
|
665 | } |
||
666 | |||
667 | /** |
||
668 | * Check filter column |
||
669 | * |
||
670 | * @param string $column |
||
671 | * |
||
672 | * @return bool |
||
673 | */ |
||
674 | 12 | protected function checkFilterColumn(string $column): bool |
|
675 | { |
||
676 | 12 | return array_key_exists($column, $this->getAllowFilters()) || |
|
677 | 12 | in_array($column, $this->getAllowFilters(), false); |
|
678 | } |
||
679 | |||
680 | /** |
||
681 | * Check filter |
||
682 | * |
||
683 | * @param string $filter |
||
684 | * |
||
685 | * @return bool |
||
686 | */ |
||
687 | 12 | protected function checkFilterName(string $filter): bool |
|
688 | { |
||
689 | 12 | return in_array($filter, $this->allowFilterNames, false); |
|
690 | } |
||
691 | |||
692 | /** |
||
693 | * Add filter |
||
694 | * |
||
695 | * @param string $column name |
||
696 | * @param string $filter |
||
697 | * @param string $value |
||
698 | * |
||
699 | * @return void |
||
700 | * @throws GridException |
||
701 | */ |
||
702 | 10 | public function addFilter(string $column, string $filter, string $value): void |
|
703 | { |
||
704 | 10 | if (!$this->checkFilterColumn($column)) { |
|
705 | 1 | throw new GridException("Filter for column `$column` is not allowed"); |
|
706 | } |
||
707 | 9 | if (!$this->checkFilterName($filter)) { |
|
708 | 1 | throw new GridException('Filter name is incorrect'); |
|
709 | } |
||
710 | 8 | if (!isset($this->filters[$column])) { |
|
711 | 8 | $this->filters[$column] = []; |
|
712 | } |
||
713 | 8 | $this->filters[$column][$filter] = $value; |
|
714 | 8 | } |
|
715 | |||
716 | |||
717 | /** |
||
718 | * Get filter |
||
719 | * |
||
720 | * @param string $column |
||
721 | * @param string|null $filter |
||
722 | * |
||
723 | * @return mixed |
||
724 | */ |
||
725 | public function getFilter(string $column, string $filter = null) |
||
726 | { |
||
727 | if (null === $filter) { |
||
728 | return $this->filters[$column] ?? null; |
||
729 | } |
||
730 | return $this->filters[$column][$filter] ?? null; |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * Get filters |
||
735 | * |
||
736 | * @return array |
||
737 | */ |
||
738 | 21 | public function getFilters(): array |
|
739 | { |
||
740 | 21 | return $this->filters; |
|
741 | } |
||
742 | |||
743 | /** |
||
744 | * Add alias for column name |
||
745 | * |
||
746 | * @param string $column |
||
747 | * @param string $alias |
||
748 | * |
||
749 | * @return void |
||
750 | */ |
||
751 | 30 | public function addAlias(string $column, string $alias): void |
|
752 | { |
||
753 | 30 | $this->aliases[$column] = $alias; |
|
754 | 30 | } |
|
755 | |||
756 | /** |
||
757 | * Get column name by alias |
||
758 | * |
||
759 | * @param string $alias |
||
760 | * |
||
761 | * @return string |
||
762 | */ |
||
763 | protected function reverseAlias(string $alias): string |
||
764 | { |
||
765 | return array_search($alias, $this->aliases, true) ?: $alias; |
||
766 | } |
||
767 | |||
768 | /** |
||
769 | * Get alias by column name |
||
770 | * |
||
771 | * @param string $column |
||
772 | * |
||
773 | * @return string |
||
774 | */ |
||
775 | 32 | public function applyAlias(string $column): string |
|
776 | { |
||
777 | 32 | return $this->aliases[$column] ?? $column; |
|
778 | } |
||
779 | |||
780 | /** |
||
781 | * Set page |
||
782 | * |
||
783 | * @param integer $page |
||
784 | * |
||
785 | * @return void |
||
786 | * @throws GridException |
||
787 | */ |
||
788 | 32 | public function setPage(int $page = 1): void |
|
789 | { |
||
790 | 32 | if ($page < 1) { |
|
791 | 1 | throw new GridException('Wrong page number, should be greater than zero'); |
|
792 | } |
||
793 | 32 | $this->page = $page; |
|
794 | 32 | } |
|
795 | |||
796 | /** |
||
797 | * Get page |
||
798 | * |
||
799 | * @return integer |
||
800 | */ |
||
801 | 17 | public function getPage(): int |
|
802 | { |
||
803 | 17 | return $this->page; |
|
804 | } |
||
805 | |||
806 | /** |
||
807 | * Set limit per page |
||
808 | * |
||
809 | * @param integer $limit |
||
810 | * |
||
811 | * @return void |
||
812 | * @throws GridException |
||
813 | */ |
||
814 | 32 | public function setLimit(int $limit): void |
|
815 | { |
||
816 | 32 | if ($limit < 1) { |
|
817 | 1 | throw new GridException('Wrong limit value, should be greater than zero'); |
|
818 | } |
||
819 | 32 | $this->limit = $limit; |
|
820 | 32 | } |
|
821 | |||
822 | /** |
||
823 | * Get limit per page |
||
824 | * |
||
825 | * @return integer |
||
826 | */ |
||
827 | 24 | public function getLimit(): int |
|
828 | { |
||
829 | 24 | return $this->limit; |
|
830 | } |
||
831 | |||
832 | /** |
||
833 | * Set default limit |
||
834 | * |
||
835 | * @param integer $limit |
||
836 | * |
||
837 | * @return void |
||
838 | * @throws GridException |
||
839 | */ |
||
840 | 32 | public function setDefaultLimit(int $limit): void |
|
841 | { |
||
842 | 32 | if ($limit < 1) { |
|
843 | 1 | throw new GridException('Wrong default limit value, should be greater than zero'); |
|
844 | } |
||
845 | 32 | $this->setLimit($limit); |
|
846 | |||
847 | 32 | $this->defaultLimit = $limit; |
|
848 | 32 | } |
|
849 | |||
850 | /** |
||
851 | * Get default limit |
||
852 | * |
||
853 | * @return integer |
||
854 | */ |
||
855 | 1 | public function getDefaultLimit(): int |
|
856 | { |
||
857 | 1 | return $this->defaultLimit; |
|
858 | } |
||
859 | |||
860 | /** |
||
861 | * Set default order |
||
862 | * |
||
863 | * @param string $column |
||
864 | * @param string $order ASC or DESC |
||
865 | * |
||
866 | * @return void |
||
867 | */ |
||
868 | 3 | public function setDefaultOrder(string $column, string $order = self::ORDER_ASC): void |
|
869 | { |
||
870 | 3 | $this->defaultOrder = [$column => $order]; |
|
871 | 3 | } |
|
872 | |||
873 | /** |
||
874 | * Get default order |
||
875 | * |
||
876 | * @return array |
||
877 | */ |
||
878 | 22 | public function getDefaultOrder(): ?array |
|
879 | { |
||
880 | 22 | return $this->defaultOrder; |
|
881 | } |
||
882 | } |
||
883 |