Passed
Pull Request — master (#1)
by
unknown
02:17
created

Paginator::toPositive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
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 ( strpos(" {$this->class}" ,"pagination")) {
129
            return $this->renderHtmlBootstrap();
130
        }
131
132
        if ($this->rows > $this->limit):
133
            $paginator = "<nav class=\"{$this->class}\">";
134
            $paginator .= "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}\">{$this->first[1]}</a>";
135
            $paginator .= $this->beforePages();
136
            $paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>";
137
            $paginator .= $this->afterPages();
138
            $paginator .= "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}\">{$this->last[1]}</a>";
139
            $paginator .= "</nav>";
140
            return $paginator;
141
        endif;
142
143
        return null;
144
    }
145
146
    /**
147
     * @return string|null
148
     */
149
    private function renderHtmlBootstrap(): ?string
150
    {
151
        $class = strpos($this->class, "-") ? $this->class : "";
152
        if ($this->rows > $this->limit):
153
            $paginator = "<nav aria-label='Navegação de página'>";
154
            $paginator .= "<ul class='pagination {$class}'>";
155
            $paginator .= "<li class='page-item'>";
156
            $paginator .= "<a class='page-link' title='{$this->first[0]}' href='{$this->link}1{$this->hash}'>{$this->first[1]}</a>";
157
            $paginator .= "</li>";
158
            $paginator .= "<li class='page-item'>";
159
            $paginator .= str_replace("{$this->class}_item", "page-link", $this->beforePages());
160
            $paginator .= "</li>";
161
            $paginator .= "<li class='page-item active'>";
162
            $paginator .= "<span class='page-link'> {$this->page}<span class='sr-only'>(atual)</span></span>";
163
            $paginator .= "</li>";
164
            $paginator .= "<li class='page-item'>";
165
            $paginator .= str_replace("{$this->class}_item", "page-link", $this->afterPages());
166
            $paginator .= "</li>";
167
            $paginator .= "<li class='page-item'>";
168
            $paginator .= "<a class='page-link' title='{$this->last[0]}' href='{$this->link}{$this->pages}{$this->hash}'>{$this->last[1]}</a>";
169
            $paginator .= "</li>";
170
            $paginator .= "</ul>";
171
            $paginator .= "</nav>";
172
            return $paginator;
173
        endif;
174
175
        return null;
176
    }
177
178
    /**
179
     * @return null|string
180
     */
181
    private function beforePages(): ?string
182
    {
183
        $before = null;
184
        for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++):
185
            if ($iPag >= 1):
186
                $before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}\">{$iPag}</a>";
187
            endif;
188
        endfor;
189
190
        return $before;
191
    }
192
193
    /**
194
     * @return string|null
195
     */
196
    private function afterPages(): ?string
197
    {
198
        $after = null;
199
        for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++):
200
            if ($dPag <= $this->pages):
201
                $after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}\">{$dPag}</a>";
202
            endif;
203
        endfor;
204
205
        return $after;
206
    }
207
208
    /**
209
     * @param $number
210
     * @return int
211
     */
212
    private function toPositive($number): int
213
    {
214
        return ($number >= 1 ? $number : 1);
215
    }
216
}