Passed
Pull Request — master (#2)
by compolom
02:16
created

Pagination::getEnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\Pagination;
6
7
class Pagination
8
{
9
    private int $total;
10
11
    private int $page;
12
13
    private int $limit;
14
15
    private int $totalPages;
16
17
    private int $length;
18
19
    private string $pos;
20
21
    private bool $uiKeys;
22
23
    /**
24
     * @param int $page
25
     * @param int $limit
26
     * @param int $total
27
     * @param int $length
28
     * @param bool $uiKeys
29
     */
30 5
    public function __construct(int $page, int $limit, int $total, int $length = 3, bool $uiKeys = false)
31
    {
32 5
        $this->totalPages = (int)ceil($total / $limit);
33 5
        $this->page = $page > 1 ? ($page > $this->totalPages ? 1 : $page) : 1;
34 5
        $this->limit = $limit > 0 ? $limit : 10;
35 5
        $this->total = $total;
36 5
        $this->length = $length >= 0 ? $length : 3;
37 5
        $this->pos = $this->init();
38 5
        $this->uiKeys = $uiKeys;
39
    }
40
41 5
    private function init(): string
42
    {
43 5
        switch ($this->page) {
44 5
            case ($this->totalPages < 7):
45
            default:
46 4
                $pos = 'full';
47 4
                break;
48 1
            case (($this->page - $this->length) < 3):
49 1
                $pos  = 'noLeftDots';
50 1
                break;
51 1
            case (($this->page - $this->length) >= 3 && ($this->totalPages - $this->page - $this->length) > 1):
52 1
                $pos  = 'center';
53 1
                break;
54 1
            case (abs($this->totalPages - $this->page - $this->length) >= 0):
55 1
                $pos  = 'noRightDots';
56 1
                break;
57
        }
58
59 5
        return $pos;
60
61
    }
62
63
    /**
64
     * @return int
65
     */
66 1
    public function getLimit(): int
67
    {
68 1
        return $this->limit;
69
    }
70
71
    /**
72
     * @return int
73
     */
74 1
    public function getEnd(): int
75
    {
76 1
        return $this->page === $this->totalPages ? $this->total : $this->page * $this->limit;
77
    }
78
79
    /**
80
     * @return int
81
     */
82 1
    public function getOffset(): int
83
    {
84 1
        return $this->page === 1 ? 0 : ($this->page - 1) * $this->limit;
85
    }
86
87
    /**
88
     * @return array
89
     */
90 1
    private function start(): array
91
    {
92 1
        $result = [];
93
94 1
        if ($this->page > 1) {
95 1
            $result['prev'] = $this->page - 1;
96
        }
97
98 1
        if ($this->page !== 1) {
99 1
            $result['first'] = 1;
100
        }
101
102 1
        if ($this->page > 3) {
103 1
            $result['second'] = 2;
104
        }
105
106 1
        return $this->uiKeys ? $result : [];
107
    }
108
109
    /**
110
     * @return array
111
     */
112 1
    private function end(): array
113
    {
114 1
        $result = [];
115
116 1
        if ($this->page !== $this->totalPages) {
117 1
            $result['last'] = $this->totalPages;
118
        }
119
120 1
        if ($this->totalPages - $this->page > 0) {
121 1
            $result['next'] = $this->page + 1;
122
        }
123
124 1
        if ($this->totalPages - $this->page + $this->length > 3) {
125 1
            $result['preLast'] = $this->totalPages - 1;
126
        }
127
128 1
        return $this->uiKeys ? $result : [];
129
    }
130
131
    /**
132
     * @return array
133
     */
134 1
    private function leftPad(): array
135
    {
136 1
        $result = [];
137 1
        $for = [];
138
139 1
        if ($this->page - $this->length > 1) {
140 1
            $result[] = 1;
141
        }
142
143 1
        if ($this->pos !== 'noLeftDots') {
144 1
            if ($this->uiKeys) {
145
                $result['leftDots'] = '...';
146
            } else {
147 1
                $result[] = '...';
148
            }
149
        }
150
151 1
        foreach (range($this->page - 1, $this->page - $this->length) as $value) {
152 1
            if ($value > 0) {
153 1
                $for[] = $value;
154
            }
155
        }
156
157 1
        return array_merge($result, array_reverse($for));
158
    }
159
160
    /**
161
     * @return array
162
     */
163 1
    private function rightPad(): array
164
    {
165 1
        $result = [];
166
167 1
        foreach (range($this->page + 1, $this->page + $this->length) as $value) {
168 1
            if ($value <= $this->totalPages) {
169 1
                $result[] = $value;
170
            }
171
        }
172
173 1
        if ($this->pos !== 'noRightDots') {
174 1
            if ($this->uiKeys) {
175
                $result['rightDots'] = '...';
176
            } else {
177 1
                $result[] = '...';
178
            }
179
        }
180
181 1
        if ($this->totalPages - $this->page - $this->length > 0) {
182 1
            $result[] = $this->totalPages;
183
        }
184
185 1
        return $result;
186
    }
187
188
    /**
189
     * @return array
190
     */
191 1
    private function getCurrent(): array
192
    {
193 1
        if ($this->uiKeys) {
194
            $result['current'] = $this->page;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
195
        } else {
196 1
            $result[] = $this->page;
197
        }
198
199 1
        return $result;
200
    }
201
202
    /**
203
     * @return array
204
     */
205 1
    public function get(): array
206
    {
207 1
        return array_merge($this->start(), $this->leftPad(), $this->getCurrent(), $this->rightPad(), $this->end());
208
    }
209
}
210