Passed
Push — master ( 88a531...7ee353 )
by Robson
02:40 queued 14s
created

Paginator::firstPage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
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
     * @param bool $fixedFirstAndLastPage
123
     * @return null|string
124
     */
125
    public function render(string $cssClass = null, bool $fixedFirstAndLastPage = true): ?string
126
    {
127
        $this->class = $cssClass ?? "paginator";
128
129
        if ($this->rows > $this->limit):
130
            $paginator = "<nav class=\"{$this->class}\">";
131
            $paginator .= $this->firstPage($fixedFirstAndLastPage);
132
            $paginator .= $this->beforePages();
133
            $paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>";
134
            $paginator .= $this->afterPages();
135
            $paginator .= $this->lastPage($fixedFirstAndLastPage);
136
            $paginator .= "</nav>";
137
            return $paginator;
138
        endif;
139
140
        return null;
141
    }
142
143
    /**
144
     * @return null|string
145
     */
146
    private function beforePages(): ?string
147
    {
148
        $before = null;
149
        for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++):
150
            if ($iPag >= 1):
151
                $before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}\">{$iPag}</a>";
152
            endif;
153
        endfor;
154
155
        return $before;
156
    }
157
158
    /**
159
     * @return string|null
160
     */
161
    private function afterPages(): ?string
162
    {
163
        $after = null;
164
        for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++):
165
            if ($dPag <= $this->pages):
166
                $after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}\">{$dPag}</a>";
167
            endif;
168
        endfor;
169
170
        return $after;
171
    }
172
173
    public function firstPage($fixedFirstAndLastPage): ?string
174
    {
175
        if ($fixedFirstAndLastPage || $this->page != 1) {
176
            return "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}\">{$this->first[1]}</a>";
177
        }
178
        return null;
179
    }
180
181
    public function lastPage($fixedFirstAndLastPage): ?string
182
    {
183
        if ($fixedFirstAndLastPage || $this->page != $this->pages) {
184
            return "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}\">{$this->last[1]}</a>";
185
        }
186
        return null;
187
    }
188
189
    /**
190
     * @param $number
191
     * @return int
192
     */
193
    private function toPositive($number): int
194
    {
195
        return ($number >= 1 ? $number : 1);
196
    }
197
}