Complex classes like Grid often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Grid, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class Grid |
||
| 39 | { |
||
| 40 | use Options; |
||
| 41 | use Helper; |
||
| 42 | |||
| 43 | const ORDER_ASC = 'asc'; |
||
| 44 | const ORDER_DESC = 'desc'; |
||
| 45 | |||
| 46 | const FILTER_LIKE = 'like'; // like |
||
| 47 | const FILTER_ENUM = 'enum'; // one from .., .., .. |
||
| 48 | const FILTER_NUM = 'num'; // ==, !=, >, >=, <, <= |
||
| 49 | |||
| 50 | const FILTER_EQ = 'eq'; // equal to .. |
||
| 51 | const FILTER_NE = 'ne'; // not equal to .. |
||
| 52 | const FILTER_GT = 'gt'; // greater than .. |
||
| 53 | const FILTER_GE = 'ge'; // greater than .. or equal |
||
| 54 | const FILTER_LT = 'lt'; // less than .. |
||
| 55 | const FILTER_LE = 'le'; // less than .. or equal |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Source\AbstractSource instance of Source |
||
| 59 | */ |
||
| 60 | protected $adapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Data instance of Data |
||
| 64 | */ |
||
| 65 | protected $data; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string unique identification of grid |
||
| 69 | */ |
||
| 70 | protected $uid; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string unique prefix of grid |
||
| 74 | */ |
||
| 75 | protected $prefix; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string location of Grid |
||
| 79 | */ |
||
| 80 | protected $module; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string location of Grid |
||
| 84 | */ |
||
| 85 | protected $controller; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array custom array params |
||
| 89 | */ |
||
| 90 | protected $params = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var integer start from first page |
||
| 94 | */ |
||
| 95 | protected $page = 1; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var integer limit per page |
||
| 99 | */ |
||
| 100 | protected $limit = 25; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var integer default value of page limit |
||
| 104 | * @see Grid::$limit |
||
| 105 | */ |
||
| 106 | protected $defaultLimit = 25; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * List of orders |
||
| 110 | * |
||
| 111 | * Example |
||
| 112 | * 'first' => 'ASC', |
||
| 113 | * 'last' => 'ASC' |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $orders = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array default order |
||
| 121 | * @see Grid::$orders |
||
| 122 | */ |
||
| 123 | protected $defaultOrder; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array list of allow orders |
||
| 127 | * @see Grid::$orders |
||
| 128 | */ |
||
| 129 | protected $allowOrders = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array list of filters |
||
| 133 | */ |
||
| 134 | protected $filters = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * List of allow filters |
||
| 138 | * |
||
| 139 | * Example |
||
| 140 | * ['id', 'status' => ['active', 'disable']] |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | * @see Grid::$filters |
||
| 144 | */ |
||
| 145 | protected $allowFilters = []; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * List of aliases for columns in DB |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $aliases = []; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Grid constructor |
||
| 156 | * |
||
| 157 | * @param array $options |
||
| 158 | */ |
||
| 159 | 33 | public function __construct($options = null) |
|
| 160 | { |
||
| 161 | 33 | if ($options) { |
|
| 162 | $this->setOptions($options); |
||
| 163 | } |
||
| 164 | |||
| 165 | 33 | if ($this->uid) { |
|
| 166 | 33 | $this->prefix = $this->getUid() . '-'; |
|
| 167 | } else { |
||
| 168 | $this->prefix = ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | 33 | $this->init(); |
|
| 172 | |||
| 173 | 33 | $this->processRequest(); |
|
| 174 | |||
| 175 | // initial default helper path |
||
| 176 | 33 | $this->addHelperPath(dirname(__FILE__) . '/Helper/'); |
|
| 177 | 33 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Initialize Grid |
||
| 181 | * |
||
| 182 | * @return Grid |
||
| 183 | */ |
||
| 184 | abstract public function init(); |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set source adapter |
||
| 188 | * |
||
| 189 | * @param Source\AbstractSource $adapter |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | 33 | public function setAdapter(Source\AbstractSource $adapter) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Get source adapter |
||
| 199 | * |
||
| 200 | * @return Source\AbstractSource |
||
| 201 | * @throws GridException |
||
| 202 | */ |
||
| 203 | 15 | public function getAdapter() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get unique Grid Id |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 33 | public function getUid() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get prefix |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | 1 | public function getPrefix() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Set module |
||
| 233 | * |
||
| 234 | * @param string $module |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | 1 | public function setModule($module) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get module |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 11 | public function getModule() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Set controller |
||
| 254 | * |
||
| 255 | * @param string $controller |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | 1 | public function setController($controller) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Get controller |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 11 | public function getController() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Process request |
||
| 275 | * |
||
| 276 | * Example of request url |
||
| 277 | * - http://domain.com/pages/grid/ |
||
| 278 | * - http://domain.com/pages/grid/page/2/ |
||
| 279 | * - http://domain.com/pages/grid/page/2/order-alias/desc/ |
||
| 280 | * - http://domain.com/pages/grid/page/2/order-created/desc/order-alias/asc/ |
||
| 281 | * |
||
| 282 | * with prefix for support more than one grid on page |
||
| 283 | * - http://domain.com/users/grid/users-page/2/users-order-created/desc/ |
||
| 284 | * - http://domain.com/users/grid/users-page/2/users-filter-status/active/ |
||
| 285 | * |
||
| 286 | * hash support |
||
| 287 | * - http://domain.com/pages/grid/#/page/2/order-created/desc/order-alias/asc/ |
||
| 288 | * |
||
| 289 | * @return Grid |
||
| 290 | */ |
||
| 291 | 33 | public function processRequest() |
|
| 292 | { |
||
| 293 | 33 | $this->module = Request::getModule(); |
|
| 294 | 33 | $this->controller = Request::getController(); |
|
| 295 | |||
| 296 | 33 | $page = Request::getParam($this->prefix . 'page', 1); |
|
| 297 | 33 | $this->setPage($page); |
|
| 298 | |||
| 299 | 33 | $limit = Request::getParam($this->prefix . 'limit', $this->limit); |
|
| 300 | 33 | $this->setLimit($limit); |
|
| 301 | |||
| 302 | 33 | foreach ($this->allowOrders as $column) { |
|
| 303 | 33 | $order = Request::getParam($this->prefix . 'order-' . $column); |
|
| 304 | 33 | if ($order) { |
|
| 305 | 33 | $this->addOrder($column, $order); |
|
| 306 | } |
||
| 307 | } |
||
| 308 | 33 | foreach ($this->allowFilters as $column) { |
|
| 309 | 33 | $filter = Request::getParam($this->prefix . 'filter-' . $column); |
|
| 310 | |||
| 311 | 33 | if ($filter) { |
|
| 312 | 1 | if (strpos($filter, '-')) { |
|
| 313 | 1 | $filter = trim($filter, ' -'); |
|
| 314 | |||
| 315 | 1 | while ($pos = strpos($filter, '-')) { |
|
| 316 | 1 | $filterType = substr($filter, 0, $pos); |
|
| 317 | 1 | $filter = substr($filter, $pos + 1); |
|
| 318 | |||
| 319 | 1 | if (strpos($filter, '-')) { |
|
| 320 | $filterValue = substr($filter, 0, strpos($filter, '-')); |
||
| 321 | $filter = substr($filter, strpos($filter, '-') + 1); |
||
| 322 | } else { |
||
| 323 | 1 | $filterValue = $filter; |
|
| 324 | } |
||
| 325 | |||
| 326 | 1 | $this->addFilter($column, $filterType, $filterValue); |
|
| 327 | } |
||
| 328 | } else { |
||
| 329 | 33 | $this->addFilter($column, self::FILTER_EQ, $filter); |
|
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | 33 | return $this; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Process source |
||
| 338 | * |
||
| 339 | * @return self |
||
| 340 | * @throws GridException |
||
| 341 | */ |
||
| 342 | 15 | public function processSource() |
|
| 343 | { |
||
| 344 | 15 | if (null === $this->adapter) { |
|
| 345 | throw new GridException("Grid Adapter is not initiated, please update method init() and try again"); |
||
| 346 | } |
||
| 347 | |||
| 348 | try { |
||
| 349 | 15 | $this->data = $this->getAdapter()->process($this->getSettings()); |
|
| 350 | } catch (\Exception $e) { |
||
| 351 | throw new GridException("Grid Adapter can't process request: ". $e->getMessage()); |
||
| 352 | } |
||
| 353 | |||
| 354 | 15 | return $this; |
|
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get data |
||
| 359 | * |
||
| 360 | * @return Data |
||
| 361 | */ |
||
| 362 | 15 | public function getData() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Get settings |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | 15 | public function getSettings() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Setup params |
||
| 387 | * |
||
| 388 | * @param $params |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | public function setParams($params) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Return params prepared for url builder |
||
| 398 | * |
||
| 399 | * @param array $rewrite |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | 11 | public function getParams(array $rewrite = []) |
|
| 403 | { |
||
| 404 | 11 | $params = $this->params; |
|
| 405 | |||
| 406 | // change page |
||
| 407 | 11 | if (isset($rewrite['page']) && $rewrite['page'] > 1) { |
|
| 408 | 4 | $params[$this->prefix . 'page'] = $rewrite['page']; |
|
| 409 | } |
||
| 410 | |||
| 411 | // change limit |
||
| 412 | 11 | if (isset($rewrite['limit'])) { |
|
| 413 | 1 | if ($rewrite['limit'] != $this->defaultLimit) { |
|
| 414 | 1 | $params[$this->prefix . 'limit'] = ($rewrite['limit'] != $this->limit) |
|
| 415 | 1 | ? $rewrite['limit'] : $this->limit; |
|
| 416 | } |
||
| 417 | } else { |
||
| 418 | 10 | if ($this->limit != $this->defaultLimit) { |
|
| 419 | $params[$this->prefix . 'limit'] = $this->limit; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | // change orders |
||
| 424 | 11 | $orders = $rewrite['orders'] ?? $this->getOrders(); |
|
| 425 | |||
| 426 | 11 | foreach ($orders as $column => $order) { |
|
| 427 | 2 | $params[$this->prefix . 'order-' . $column] = $order; |
|
| 428 | } |
||
| 429 | |||
| 430 | // change filters |
||
| 431 | 11 | $filters = $rewrite['filters'] ?? $this->getFilters(); |
|
| 432 | |||
| 433 | 11 | foreach ($filters as $column => $columnFilters) { |
|
| 434 | 1 | $columnFilter = []; |
|
| 435 | 1 | foreach ($columnFilters as $filterName => $filterValue) { |
|
| 436 | 1 | if ($filterName == self::FILTER_EQ) { |
|
| 437 | $columnFilter[] = $filterValue; |
||
| 438 | } else { |
||
| 439 | 1 | $columnFilter[] = $filterName . '-' . $filterValue; |
|
| 440 | } |
||
| 441 | } |
||
| 442 | 1 | $params[$this->prefix . 'filter-' . $column] = join('-', $columnFilter); |
|
| 443 | } |
||
| 444 | |||
| 445 | 11 | return $params; |
|
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get Url |
||
| 450 | * |
||
| 451 | * @param array $params |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 11 | public function getUrl($params) |
|
| 455 | { |
||
| 456 | // prepare params |
||
| 457 | 11 | $params = $this->getParams($params); |
|
| 458 | |||
| 459 | // retrieve URL |
||
| 460 | 11 | return Router::getUrl( |
|
| 461 | 11 | $this->getModule(), |
|
| 462 | 11 | $this->getController(), |
|
| 463 | $params |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Set allow orders |
||
| 469 | * |
||
| 470 | * @param string[] $orders |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | 33 | public function setAllowOrders(array $orders = []) |
|
| 474 | { |
||
| 475 | 33 | $this->allowOrders = $orders; |
|
| 476 | 33 | } |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Get allow orders |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | 2 | public function getAllowOrders() |
|
| 484 | { |
||
| 485 | 2 | return $this->allowOrders; |
|
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Add order rule |
||
| 490 | * |
||
| 491 | * @param string $column |
||
| 492 | * @param string $order |
||
| 493 | * @return void |
||
| 494 | * @throws GridException |
||
| 495 | */ |
||
| 496 | 8 | public function addOrder($column, $order = Grid::ORDER_ASC) |
|
| 497 | { |
||
| 498 | 8 | if (!in_array($column, $this->allowOrders)) { |
|
| 499 | 1 | throw new GridException('Wrong column order'); |
|
| 500 | } |
||
| 501 | |||
| 502 | 7 | if (strtolower($order) != Grid::ORDER_ASC |
|
| 503 | 7 | && strtolower($order) != Grid::ORDER_DESC |
|
| 504 | ) { |
||
| 505 | 1 | throw new GridException('Order for column "' . $column . '" is incorrect'); |
|
| 506 | } |
||
| 507 | 6 | $column = $this->applyAlias($column); |
|
| 508 | |||
| 509 | 6 | $this->orders[$column] = $order; |
|
| 510 | 6 | } |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Add order rules |
||
| 514 | * |
||
| 515 | * @param array $orders |
||
| 516 | * @return void |
||
| 517 | */ |
||
| 518 | 1 | public function addOrders(array $orders) |
|
| 519 | { |
||
| 520 | 1 | foreach ($orders as $column => $order) { |
|
| 521 | 1 | $this->addOrder($column, $order); |
|
| 522 | } |
||
| 523 | 1 | } |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Set order |
||
| 527 | * |
||
| 528 | * @param string $column |
||
| 529 | * @param string $order ASC or DESC |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | 6 | public function setOrder($column, $order = Grid::ORDER_ASC) |
|
| 533 | { |
||
| 534 | 6 | $this->orders = []; |
|
| 535 | 6 | $this->addOrder($column, $order); |
|
| 536 | 4 | } |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Set orders |
||
| 540 | * |
||
| 541 | * @param array $orders |
||
| 542 | * @return void |
||
| 543 | */ |
||
| 544 | 1 | public function setOrders(array $orders) |
|
| 545 | { |
||
| 546 | 1 | $this->orders = []; |
|
| 547 | 1 | $this->addOrders($orders); |
|
| 548 | 1 | } |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Get orders |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | 24 | public function getOrders() |
|
| 556 | { |
||
| 557 | 24 | $default = $this->getDefaultOrder(); |
|
| 558 | |||
| 559 | // remove default order when another one is set |
||
| 560 | 24 | if (is_array($default) |
|
| 561 | 24 | && count($this->orders) > 1 |
|
| 562 | 24 | && isset($this->orders[key($default)]) |
|
| 563 | 24 | && $this->orders[key($default)] == reset($default) |
|
| 564 | ) { |
||
| 565 | unset($this->orders[key($default)]); |
||
| 566 | } |
||
| 567 | |||
| 568 | 24 | return $this->orders; |
|
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Set allowed filters |
||
| 573 | * |
||
| 574 | * @param string[] $filters |
||
| 575 | * @return void |
||
| 576 | */ |
||
| 577 | 33 | public function setAllowFilters(array $filters = []) |
|
| 578 | { |
||
| 579 | 33 | $this->allowFilters = $filters; |
|
| 580 | 33 | } |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Get allow filters |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | 3 | public function getAllowFilters() |
|
| 588 | { |
||
| 589 | 3 | return $this->allowFilters; |
|
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Check filter |
||
| 594 | * |
||
| 595 | * @param string $filter |
||
| 596 | * @return bool |
||
| 597 | */ |
||
| 598 | 10 | public function checkFilter($filter) |
|
| 599 | { |
||
| 600 | 10 | if ($filter == self::FILTER_EQ || |
|
| 601 | 9 | $filter == self::FILTER_NE || |
|
| 602 | 6 | $filter == self::FILTER_GT || |
|
| 603 | 6 | $filter == self::FILTER_GE || |
|
| 604 | 6 | $filter == self::FILTER_LT || |
|
| 605 | 6 | $filter == self::FILTER_LE || |
|
| 606 | 5 | $filter == self::FILTER_NUM || |
|
| 607 | 5 | $filter == self::FILTER_ENUM || |
|
| 608 | 10 | $filter == self::FILTER_LIKE |
|
| 609 | ) { |
||
| 610 | 8 | return true; |
|
| 611 | } |
||
| 612 | 2 | return false; |
|
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Add filter |
||
| 617 | * |
||
| 618 | * @param string $column |
||
| 619 | * @param string $filter |
||
| 620 | * @param string $value |
||
| 621 | * @return void |
||
| 622 | * @throws GridException |
||
| 623 | */ |
||
| 624 | 10 | public function addFilter($column, $filter, $value) |
|
| 625 | { |
||
| 626 | 10 | if (!in_array($column, $this->allowFilters) && |
|
| 627 | 10 | !array_key_exists($column, $this->allowFilters) |
|
| 628 | ) { |
||
| 629 | 1 | throw new GridException('Wrong column name for filter'); |
|
| 630 | } |
||
| 631 | |||
| 632 | 9 | $filter = strtolower($filter); |
|
| 633 | |||
| 634 | 9 | if (!$this->checkFilter($filter)) { |
|
| 635 | 1 | throw new GridException('Wrong filter name'); |
|
| 636 | } |
||
| 637 | |||
| 638 | 8 | $column = $this->applyAlias($column); |
|
| 639 | |||
| 640 | 8 | if (!isset($this->filters[$column])) { |
|
| 641 | 8 | $this->filters[$column] = []; |
|
| 642 | } |
||
| 643 | 8 | $this->filters[$column][$filter] = $value; |
|
| 644 | 8 | } |
|
| 645 | |||
| 646 | |||
| 647 | /** |
||
| 648 | * Get filter |
||
| 649 | * |
||
| 650 | * @param string $column |
||
| 651 | * @param string $filter |
||
| 652 | * @return mixed |
||
| 653 | */ |
||
| 654 | public function getFilter($column, $filter = null) |
||
| 655 | { |
||
| 656 | if (isset($this->filters[$column])) { |
||
| 657 | if ($filter) { |
||
|
|
|||
| 658 | return $this->filters[$column][$filter] ?? null; |
||
| 659 | } else { |
||
| 660 | return $this->filters[$column]; |
||
| 661 | } |
||
| 662 | } else { |
||
| 663 | return null; |
||
| 664 | } |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get filters |
||
| 669 | * |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | 23 | public function getFilters() |
|
| 673 | { |
||
| 674 | 23 | return $this->filters; |
|
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Add alias |
||
| 679 | * |
||
| 680 | * @param string $key |
||
| 681 | * @param string $value |
||
| 682 | * @return void |
||
| 683 | */ |
||
| 684 | public function addAlias($key, $value) |
||
| 685 | { |
||
| 686 | $this->aliases[$key] = $value; |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Set aliases |
||
| 691 | * |
||
| 692 | * @param array $aliases |
||
| 693 | * @return void |
||
| 694 | */ |
||
| 695 | public function setAliases($aliases) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Apply Alias |
||
| 702 | * |
||
| 703 | * @param string $key |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | 11 | protected function applyAlias($key) |
|
| 707 | { |
||
| 708 | 11 | return $this->aliases[$key] ?? $key; |
|
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Set page |
||
| 713 | * |
||
| 714 | * @param integer $page |
||
| 715 | * @return void |
||
| 716 | * @throws GridException |
||
| 717 | */ |
||
| 718 | 33 | public function setPage($page = 1) |
|
| 719 | { |
||
| 720 | 33 | if ($page < 1) { |
|
| 721 | 1 | throw new GridException('Wrong page number, should be greater than zero'); |
|
| 722 | } |
||
| 723 | 33 | $this->page = (int)$page; |
|
| 724 | 33 | } |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Get page |
||
| 728 | * |
||
| 729 | * @return integer |
||
| 730 | */ |
||
| 731 | 17 | public function getPage() |
|
| 732 | { |
||
| 733 | 17 | return $this->page; |
|
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Set limit per page |
||
| 738 | * |
||
| 739 | * @param integer $limit |
||
| 740 | * @return void |
||
| 741 | * @throws GridException |
||
| 742 | */ |
||
| 743 | 33 | public function setLimit($limit) |
|
| 744 | { |
||
| 745 | 33 | if ($limit < 1) { |
|
| 746 | 1 | throw new GridException('Wrong limit value, should be greater than zero'); |
|
| 747 | } |
||
| 748 | 33 | $this->limit = (int)$limit; |
|
| 749 | 33 | } |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Get limit per page |
||
| 753 | * |
||
| 754 | * @return integer |
||
| 755 | */ |
||
| 756 | 16 | public function getLimit() |
|
| 757 | { |
||
| 758 | 16 | return $this->limit; |
|
| 759 | } |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Set default limit |
||
| 763 | * |
||
| 764 | * @param integer $limit |
||
| 765 | * @return void |
||
| 766 | * @throws GridException |
||
| 767 | */ |
||
| 768 | 33 | public function setDefaultLimit($limit) |
|
| 769 | { |
||
| 770 | 33 | if ($limit < 1) { |
|
| 771 | 1 | throw new GridException('Wrong default limit value, should be greater than zero'); |
|
| 772 | } |
||
| 773 | 33 | $this->setLimit($limit); |
|
| 774 | |||
| 775 | 33 | $this->defaultLimit = (int)$limit; |
|
| 776 | 33 | } |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Get default limit |
||
| 780 | * |
||
| 781 | * @return integer |
||
| 782 | */ |
||
| 783 | 1 | public function getDefaultLimit() |
|
| 787 | |||
| 788 | /** |
||
| 789 | * Set default order |
||
| 790 | * |
||
| 791 | * @param string $column |
||
| 792 | * @param string $order ASC or DESC |
||
| 793 | * @return void |
||
| 794 | * @throws GridException |
||
| 795 | */ |
||
| 796 | 5 | public function setDefaultOrder($column, $order = Grid::ORDER_ASC) |
|
| 797 | { |
||
| 798 | 5 | $this->setOrder($column, $order); |
|
| 799 | |||
| 800 | 3 | $this->defaultOrder = [$column => $order]; |
|
| 801 | 3 | } |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Get default order |
||
| 805 | * |
||
| 806 | * @return array |
||
| 807 | */ |
||
| 808 | 23 | public function getDefaultOrder() |
|
| 812 | } |
||
| 813 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: