Passed
Branch master (8a45da)
by compolom
02:40 queued 59s
created

Pagination::leftPad()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 3
b 0
f 0
nc 36
nop 0
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
crap 7.0222
rs 8.8333
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
     * @return int
64
     */
65 1
    public function getLimit(): int
66
    {
67 1
        return $this->limit;
68
    }
69
70
    /**
71
     * @return int
72
     */
73 1
    public function getEnd(): int
74
    {
75 1
        return $this->page === $this->totalPages ? $this->total : $this->page * $this->limit;
76
    }
77
78
    /**
79
     * @return int
80
     */
81 1
    public function getOffset(): int
82
    {
83 1
        return $this->page === 1 ? 0 : ($this->page - 1) * $this->limit;
84
    }
85
86
    /**
87
     * @return array
88
     */
89 1
    private function start(): array
90
    {
91 1
        $result = [];
92
93 1
        if ($this->uiKeys) {
94
            if ($this->page > 1) {
95
                $result['prev'] = $this->page - 1;
96
            }
97
            if ($this->page !== 1) {
98
                $result['first'] = 1;
99
            }
100
            if ($this->page > 3) {
101
                $result['second'] = 2;
102
            }
103
        }
104
105 1
        return $this->pos !== 'full' ? $result : [];
106
    }
107
108
    /**
109
     * @return array
110
     */
111 1
    private function end(): array
112
    {
113 1
        $result = [];
114
115 1
        if ($this->uiKeys) {
116
            if ($this->page !== $this->totalPages) {
117
                $result['last'] = $this->totalPages;
118
            }
119
            if ($this->totalPages - $this->page > 0) {
120
                $result['next'] = $this->page + 1;
121
            }
122
            if ($this->totalPages - $this->page + $this->length > 3) {
123
                $result['preLast'] = $this->totalPages - 1;
124
            }
125
        }
126
127 1
        return $this->pos !== 'full' ? $result : [];
128
    }
129
130
    /**
131
     * @return array
132
     */
133 1
    private function leftPad(): array
134
    {
135 1
        $result = [];
136 1
        $for = [];
137
138 1
            if ($this->page - $this->length > 1) {
139 1
                $result[] = 1;
140
            }
141 1
            if ($this->pos !== 'noLeftDots') {
142 1
                if ($this->uiKeys) {
143
                    $result['leftDots'] = '...';
144
                } else {
145 1
                    $result[] = '...';
146
                }
147
            }
148 1
            foreach (range($this->page - 1, $this->page - $this->length) as $value) {
149 1
                if ($value > 0) {
150 1
                    $for[] = $value;
151
                }
152
            }
153
154 1
        return $this->pos === 'full' ? [] : array_merge($result, array_reverse($for));
155
    }
156
157
    /**
158
     * @return array
159
     */
160 1
    private function rightPad(): array
161
    {
162 1
        $result = [];
163
164 1
        foreach (range($this->page + 1, $this->page + $this->length) as $value) {
165 1
            if ($value <= $this->totalPages) {
166 1
                $result[] = $value;
167
            }
168
        }
169 1
        if ($this->pos !== 'noRightDots') {
170 1
            if ($this->uiKeys) {
171
                $result['rightDots'] = '...';
172
            } else {
173 1
                $result[] = '...';
174
            }
175
        }
176 1
        if ($this->totalPages - $this->page - $this->length > 0) {
177 1
            $result[] = $this->totalPages;
178
        }
179
180 1
        return $this->pos === 'full' ? [] : $result;
181
    }
182
183
    /**
184
     * @return array
185
     */
186 1
    private function getCurrent(): array
187
    {
188 1
        if ($this->uiKeys) {
189
            $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...
190
        } else {
191 1
            $result[] = $this->page;
192
        }
193
194 1
        if ($this->pos === 'full') {
195
            $result = [];
196
            foreach (range(1, $this->totalPages) as $value) {
197
                if ($this->uiKeys && $value === $this->page) {
198
                    $result['current'] = $value;
199
                } else {
200
                    $result[] = $value;
201
                }
202
            }
203
        }
204
205 1
        return $result;
206
    }
207
208
    /**
209
     * @return array
210
     */
211 1
    public function get(): array
212
    {
213 1
        return array_merge($this->start(), $this->leftPad(), $this->getCurrent(), $this->rightPad(), $this->end());
214
    }
215
216
    /**
217
     * @return int
218
     */
219
    public function getTotalPages(): int
220
    {
221
        return $this->totalPages;
222
    }
223
}
224