Completed
Push — master ( 07e5da...3f33ed )
by Maxim
04:12
created

Paginator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 132
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setTotal() 0 4 1
A getCurrentPage() 0 3 1
D getCurrentPages() 0 27 9
A getPageCount() 0 3 1
A getOffset() 0 3 1
A getItemsPerPage() 0 3 1
A setCurrentPage() 0 4 1
A getTotal() 0 3 1
A setItemsPerPage() 0 4 1
B validate() 0 13 5
A getLimit() 0 3 1
1
<?php
2
3
namespace WebComplete\core\utils\paginator;
4
5
class Paginator
6
{
7
    const DEFAULT_ITEMS_PER_PAGE = 25;
8
9
    protected $currentPage = 1;
10
    protected $itemsPerPage = self::DEFAULT_ITEMS_PER_PAGE;
11
    protected $total = 0;
12
13
    /**
14
     * @return int
15
     */
16
    public function getTotal(): int
17
    {
18
        return $this->total;
19
    }
20
21
    /**
22
     * @param int $total
23
     */
24
    public function setTotal($total)
25
    {
26
        $this->total = (int)$total;
27
        $this->validate();
28
    }
29
30
    /**
31
     * @return int
32
     */
33
    public function getCurrentPage(): int
34
    {
35
        return $this->currentPage;
36
    }
37
38
    /**
39
     * @param int $currentPage
40
     */
41
    public function setCurrentPage($currentPage)
42
    {
43
        $this->currentPage = (int)$currentPage;
44
        $this->validate();
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getItemsPerPage(): int
51
    {
52
        return $this->itemsPerPage;
53
    }
54
55
    /**
56
     * @param int $itemsPerPage
57
     */
58
    public function setItemsPerPage($itemsPerPage)
59
    {
60
        $this->itemsPerPage = (int)$itemsPerPage;
61
        $this->validate();
62
    }
63
64
65
    /**
66
     * @return int
67
     */
68
    public function getPageCount(): int
69
    {
70
        return (int)\ceil($this->getTotal() / $this->getItemsPerPage());
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getCurrentPages(): array
77
    {
78
        $result = [];
79
        $pageCurrent = $this->getCurrentPage();
80
        if ($pageCount = $this->getPageCount()) {
81
            if ($pageCount <= 5) {
82
                for ($i = 1; $i <= $pageCount; $i++) {
83
                    $result[] = $i;
84
                }
85
            } else {
86
                if ($pageCurrent <= 3) {
87
                    for ($i = 1; $i <= 5; $i++) {
88
                        $result[] = $i;
89
                    }
90
                } elseif ($pageCount - $pageCurrent <= 2) {
91
                    for ($i = $pageCount - 4; $i <= $pageCount; $i++) {
92
                        $result[] = $i;
93
                    }
94
                } else {
95
                    for ($i = $pageCurrent - 2; $i <= $pageCurrent + 2; $i++) {
96
                        $result[] = $i;
97
                    }
98
                }
99
            }
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getOffset(): int
109
    {
110
        return $this->itemsPerPage * ($this->currentPage - 1);
111
    }
112
113
    /**
114
     * @return int
115
     */
116
    public function getLimit(): int
117
    {
118
        return $this->itemsPerPage;
119
    }
120
121
    /**
122
     *
123
     */
124
    protected function validate()
125
    {
126
        if ($this->currentPage < 1) {
127
            $this->currentPage = 1;
128
        }
129
        if ($this->itemsPerPage < 1) {
130
            $this->itemsPerPage = 1;
131
        }
132
        if ($this->total < 0) {
133
            $this->total = 0;
134
        }
135
        if ($this->itemsPerPage * ($this->currentPage - 1) > $this->total) {
136
            $this->currentPage = 1;
137
        }
138
    }
139
}
140