Passed
Push — master ( a01ea1...7c8627 )
by Robson
02:07
created

Paginator::pages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CoffeeCode\Paginator;
4
5
/**
6
 * Class CoffeeCode Paginator
7
 *
8
 * @author Robson V. Leite <https://github.com/robsonvleite>
9
 * @package CoffeeCode\Paginator
10
 */
11
class Paginator
12
{
13
    /** @var int */
14
    private $page;
15
16
    /** @var int */
17
    private $pages;
18
19
    /** @var int */
20
    private $rows;
21
22
    /** @var int */
23
    private $limit;
24
25
    /** @var int */
26
    private $offset;
27
28
    /** @var int */
29
    private $range;
30
31
    /** @var string */
32
    private $link;
33
34
    /** @var string */
35
    private $title;
36
37
    /** @var string */
38
    private $class;
39
40
    /** @var string */
41
    private $hash;
42
43
    /** @var array */
44
    private $first;
45
46
    /** @var array */
47
    private $last;
48
49
    /**
50
     * Paginator constructor.
51
     * @param string|null $link
52
     * @param string|null $title
53
     * @param array|null $first
54
     * @param array|null $last
55
     */
56
    public function __construct(string $link = null, string $title = null, array $first = null, array $last = null)
57
    {
58
        $this->link = ($link ?? "?page=");
59
        $this->title = ($title ?? "Página");
60
        $this->first = ($first ?? ["Primeira página", "<<"]);
61
        $this->last = ($last ?? ["Última página", ">>"]);
62
    }
63
64
    /**
65
     * @param int $rows
66
     * @param int $limit
67
     * @param int|null $page
68
     * @param int $range
69
     * @param string|null $hash
70
     */
71
    public function pager(int $rows, int $limit = 10, int $page = null, int $range = 3, string $hash = null): void
72
    {
73
        $this->rows = $this->toPositive($rows);
74
        $this->limit = $this->toPositive($limit);
75
        $this->range = $this->toPositive($range);
76
        $this->pages = (int)ceil($this->rows / $this->limit);
77
        $this->page = ($page <= $this->pages ? $this->toPositive($page) : $this->pages);
78
79
        $this->offset = (($this->page * $this->limit) - $this->limit >= 0 ? ($this->page * $this->limit) - $this->limit : 0);
80
        $this->hash = (!empty($hash) ? "#{$hash}" : null);
81
82
        if ($this->rows && $this->offset >= $this->rows) {
83
            header("Location: {$this->link}" . ceil($this->rows / $this->limit));
84
            exit;
85
        }
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function limit(): int
92
    {
93
        return $this->limit;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function offset(): int
100
    {
101
        return $this->offset;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function page()
108
    {
109
        return $this->page;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function pages()
116
    {
117
        return $this->pages;
118
    }
119
120
    /**
121
     * @param string $cssClass
122
     * @return null|string
123
     */
124
    public function render(string $cssClass = "paginator"): ?string
125
    {
126
        $this->class = $cssClass;
127
128
        if ($this->rows > $this->limit):
129
            $paginator = "<nav class=\"{$this->class}\">";
130
            $paginator .= "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}\">{$this->first[1]}</a>";
131
            $paginator .= $this->beforePages();
132
            $paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>";
133
            $paginator .= $this->afterPages();
134
            $paginator .= "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}\">{$this->last[1]}</a>";
135
            $paginator .= "</nav>";
136
            return $paginator;
137
        endif;
138
139
        return null;
140
    }
141
142
    /**
143
     * @return null|string
144
     */
145
    private function beforePages(): ?string
146
    {
147
        $before = null;
148
        for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++):
149
            if ($iPag >= 1):
150
                $before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}\">{$iPag}</a>";
151
            endif;
152
        endfor;
153
154
        return $before;
155
    }
156
157
    /**
158
     * @return string|null
159
     */
160
    private function afterPages(): ?string
161
    {
162
        $after = null;
163
        for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++):
164
            if ($dPag <= $this->pages):
165
                $after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}\">{$dPag}</a>";
166
            endif;
167
        endfor;
168
169
        return $after;
170
    }
171
172
    /**
173
     * @param $number
174
     * @return int
175
     */
176
    private function toPositive($number): int
177
    {
178
        return ($number >= 1 ? $number : 1);
179
    }
180
}