Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
| Pager | |
100.00% |
42 / 42 |
|
100.00% |
13 / 13 |
20 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| calculateStartAndEnd | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| route | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| paramsForPage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| hasLeftHidden | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasRightHidden | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| start | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| end | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| page | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| max | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prev | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| params | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | class Pager { |
| 6 | |
| 7 | protected $page = 0; |
| 8 | protected $limit = 25; |
| 9 | protected $count; |
| 10 | protected $max; |
| 11 | protected $next; |
| 12 | protected $prev; |
| 13 | protected $start; |
| 14 | protected $end; |
| 15 | protected $hideLeft; |
| 16 | protected $hideRight; |
| 17 | protected $params; |
| 18 | protected $route; |
| 19 | |
| 20 | public function __construct(string $route, array $params, int $count, int $pagerLimit = 7) { |
| 21 | $this->route = $route; |
| 22 | $this->params = $params; |
| 23 | $this->page = isset($params['page']) ? (int)$params['page'] : 0; |
| 24 | $this->count = $count; |
| 25 | $pageSize = isset($params['page_size']) ? (int)$params['page_size'] : 10; |
| 26 | $this->max = ceil($this->count / $pageSize) - 1; |
| 27 | if ($this->page > $this->max) { |
| 28 | $this->page = $this->max; |
| 29 | } |
| 30 | if ($this->page < 0) { |
| 31 | $this->page = 0; |
| 32 | } |
| 33 | $this->prev = $this->page != 0; |
| 34 | $this->next = $this->page != $this->max; |
| 35 | $this->calculateStartAndEnd($pagerLimit); |
| 36 | $this->hideLeft = $this->start > 1; |
| 37 | $this->hideRight = $this->end < $this->max - 1; |
| 38 | } |
| 39 | |
| 40 | protected function calculateStartAndEnd($pagerLimit) { |
| 41 | $limit = (int)floor($pagerLimit / 2); |
| 42 | $this->start = $this->page - $limit; |
| 43 | $add = 0; |
| 44 | if ($this->start < 0) { |
| 45 | $add = $limit - $this->page; |
| 46 | $this->start = 0; |
| 47 | } |
| 48 | $this->end = $this->page + $limit + $add; |
| 49 | $sub = 0; |
| 50 | if ($this->end > $this->max) { |
| 51 | $sub = $this->end - $this->max; |
| 52 | $this->end = $this->max; |
| 53 | } |
| 54 | $this->start -= $sub; |
| 55 | if ($this->start < 0) { |
| 56 | $this->start = 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public function route() { |
| 61 | return $this->route; |
| 62 | } |
| 63 | |
| 64 | public function paramsForPage(int $page) { |
| 65 | $params = $this->params; |
| 66 | $params['page'] = $page; |
| 67 | return $params; |
| 68 | } |
| 69 | |
| 70 | public function hasLeftHidden() { |
| 71 | return $this->hideLeft; |
| 72 | } |
| 73 | |
| 74 | public function hasRightHidden() { |
| 75 | return $this->hideRight; |
| 76 | } |
| 77 | |
| 78 | public function start() { |
| 79 | return $this->start; |
| 80 | } |
| 81 | |
| 82 | public function end() { |
| 83 | return $this->end; |
| 84 | } |
| 85 | |
| 86 | public function page() { |
| 87 | return $this->page; |
| 88 | } |
| 89 | |
| 90 | public function max() { |
| 91 | return $this->max; |
| 92 | } |
| 93 | |
| 94 | public function prev() { |
| 95 | return $this->prev; |
| 96 | } |
| 97 | |
| 98 | public function next() { |
| 99 | return $this->next; |
| 100 | } |
| 101 | |
| 102 | public function params() { |
| 103 | return $this->params; |
| 104 | } |
| 105 | } |