Pager::rebuildPaginationOptions()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 4
nc 5
nop 0
dl 0
loc 6
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid;
5
6
7
class Pager
8
{
9
    protected $page = 1;
10
11
    protected $limit = null;
12
13
    protected $totalCount;
14
15
    protected $maxPage;
16
17
    protected $pages = [];
18
19
    protected $isEnabled;
20
21
22
    public function isEnabled()
23
    {
24
        return $this->isEnabled;
25
    }
26
27
    public function enable()
28
    {
29
        $this->isEnabled = true;
30
    }
31
32
    public function disable()
33
    {
34
        $this->isEnabled = false;
35
    }
36
37
    public function setOptions(array $options)
38
    {
39
        foreach ($options as $key => $value) {
40
            $setter = 'set' . ucfirst($key);
41
            if (method_exists($this, $setter)) {
42
                $this->$setter($value);
43
            }
44
        }
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getFirst()
51
    {
52
        return ($this->page - 1) * $this->limit;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getPage(): int
59
    {
60
        return $this->page;
61
    }
62
    /**
63
     * @param int $page
64
     */
65
    public function setPage(int $page): void
66
    {
67
        $this->page = $page;
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function getLimit()
74
    {
75
        return $this->limit;
76
    }
77
78
    /**
79
     * @param $limit
80
     */
81
    public function setLimit($limit): void
82
    {
83
        $this->limit = $limit;
84
    }
85
86
    /**
87
     * @param int $totalCount
88
     */
89
    public function setTotalCount(int $totalCount): void
90
    {
91
        $this->totalCount = $totalCount;
92
    }
93
94
    public function getPaginationOptions()
95
    {
96
        return [
97
            'currentPage' => $this->page,
98
            'pages' => $this->pages
99
        ];
100
    }
101
102
    /**
103
     * @internal
104
     */
105
    public function rebuildPaginationOptions()
106
    {
107
        if (is_int($this->limit) && is_int($this->totalCount)) {
108
            $this->maxPage = (int)ceil($this->totalCount / $this->limit);
109
            $this->page = $this->page > 0 && $this->page <= $this->maxPage ? $this->page : 1;
110
            $this->pages = $this->buildPages();
111
        }
112
    }
113
114
    protected function buildPages()
115
    {
116
        if ($this->maxPage === 0) {
117
            return [1];
118
        }
119
        if ($this->maxPage <= 10) {
120
            return range(1, $this->maxPage);
121
        }
122
        if ($this->page < 5) {
123
            return array_merge(range(1, 6), [null, $this->maxPage]);
124
        }
125
        if ($this->page > $this->maxPage - 4) {
126
            return array_merge([1, null], range($this->maxPage - 5, $this->maxPage));
127
        }
128
        return array_merge([1, null], range($this->page - 2, $this->page + 2), [null, $this->maxPage]);
129
    }
130
131
}
132