Completed
Branch master (3b97d6)
by Pavel
09:49
created

Pager::rebuildPaginationOptions()   A

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
        $this->rebuildPaginationOptions();
93
    }
94
95
    public function getPaginationOptions()
96
    {
97
        return [
98
            'currentPage' => $this->page,
99
            'pages' => $this->pages
100
        ];
101
    }
102
103
    /**
104
     * @internal
105
     */
106
    public function rebuildPaginationOptions()
107
    {
108
        if (is_int($this->limit) && is_int($this->totalCount)) {
109
            $this->maxPage = (int)ceil($this->totalCount / $this->limit);
110
            $this->page = $this->page > 0 && $this->page <= $this->maxPage ? $this->page : 1;
111
            $this->pages = $this->buildPages();
112
        }
113
    }
114
115
    protected function buildPages()
116
    {
117
        if ($this->maxPage === 0) {
118
            return [1];
119
        }
120
        if ($this->maxPage <= 10) {
121
            return range(1, $this->maxPage);
122
        }
123
        if ($this->page < 5) {
124
            return array_merge(range(1, 6), [null, $this->maxPage]);
125
        }
126
        if ($this->page > $this->maxPage - 4) {
127
            return array_merge([1, null], range($this->maxPage - 5, $this->maxPage));
128
        }
129
        return array_merge([1, null], range($this->page - 2, $this->page + 2), [null, $this->maxPage]);
130
    }
131
132
}
133