Passed
Push — master ( 238d4d...ef85a6 )
by compolom
02:34 queued 28s
created

Pagination::start()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\Pagination;
4
5
class Pagination
6
{
7
    private $total;
8
9
    private $page;
10
11
    private $limit;
12
13
    private $totalPages;
14
15
    private $length;
16
17
    /**
18
     * Pagination constructor.
19
     *
20
     * @param int $page
21
     * @param int $limit
22
     * @param int $total
23
     * @param int $length
24
     */
25 5
    public function __construct(int $page, int $limit, int $total, int $length = 3)
26
    {
27 5
        $this->totalPages = (int)ceil($total / $limit);
28 5
        $this->page = $page < 1 ? 1 : ($page > $this->totalPages ? $this->totalPages : $page);
29 5
        $this->limit = $limit > 0 ? $limit : 10;
30 5
        $this->total = $total;
31 5
        $this->length = $length >= 0 ? $length : 3;
32 5
    }
33
34
    /**
35
     * @return int
36
     */
37 1
    public function getLimit(): int
38
    {
39 1
        return $this->limit;
40
    }
41
42
    /**
43
     * @return int
44
     */
45 1
    public function getEnd(): int
46
    {
47 1
        return $this->page === $this->totalPages ? $this->total : $this->page * $this->limit;
48
    }
49
50
    /**
51
     * @return int
52
     */
53 1
    public function getOffset(): int
54
    {
55 1
        return $this->page === 1 ? 0 : ($this->page - 1) * $this->limit;
56
    }
57
58
    /**
59
     * @return array
60
     */
61 1
    private function start(): array
62
    {
63 1
        $result = [];
64
65 1
        if ($this->page > 1) {
66 1
            $result['minus'] = $this->page - 1;
67
        }
68
69 1
        if ($this->page !== 1) {
70 1
            $result['first'] = 1;
71
        }
72
73 1
        return $result;
74
    }
75
76
    /**
77
     * @return int
78
     */
79 1
    private function leftDots(): int
80
    {
81 1
        return $this->page - $this->length;
82
    }
83
84
    /**
85
     * @return array
86
     */
87 1
    private function leftPad(): array
88
    {
89 1
        $result = [];
90
91 1
        $leftDots = $this->leftDots();
92
93 1
        if ($leftDots > 2) {
94 1
            $result['leftDots'] = '...';
95
        }
96
97 1
        if ($leftDots > 0) {
98 1
            foreach (array_reverse(range($this->page - 1, $leftDots)) as $value) {
99 1
                $result[$value] = $value;
100
            }
101 1
            if (isset($result[1])) {
102 1
                unset($result[1]);
103
            }
104
        }
105
106 1
        return $result;
107
    }
108
109 1
    private function rightDots(): int
110
    {
111 1
        return ($this->totalPages - 1) - ($this->page + $this->length);
112
    }
113
114
    /**
115
     * @return array
116
     */
117 1
    private function rightPad(): array
118
    {
119 1
        $result = [];
120
121 1
        $rightDots = $this->rightDots();
122
123 1
        $array = $rightDots > 0
124 1
            ? range($this->page + 1, $this->page + $this->length)
125 1
            : ($this->page + 1 < $this->totalPages
126 1
                ? range($this->page + 1, $this->totalPages - 1)
127 1
                : []
128
            );
129
130 1
        foreach ($array as $value) {
131 1
            $result[$value] = $value;
132
        }
133
134 1
        if ($rightDots > 0) {
135 1
            $result['rightDots'] = '...';
136
        }
137
138 1
        return $result;
139
    }
140
141 1
    private function end(): array
142
    {
143 1
        $result = [];
144
145 1
        if ($this->page !== $this->totalPages) {
146 1
            $result['last'] = $this->totalPages;
147
        }
148
149 1
        if ($this->totalPages - $this->page > 0) {
150 1
            $result['plus'] = $this->page + 1;
151
        }
152
153 1
        return $result;
154
    }
155
156
    /**
157
     * @return array
158
     */
159 1
    public function get(): array
160
    {
161 1
        return array_merge($this->start(), $this->leftPad(), ['current' => $this->page], $this->rightPad(), $this->end());
162
    }
163
}
164