Passed
Pull Request — master (#5)
by
unknown
02:11
created

Paginator::addGetParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 15
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
    /** @var string */
50
    private $params;
51
	
52
    /**
53
     * Paginator constructor.
54
     * @param string|null $link
55
     * @param string|null $title
56
     * @param array|null $first
57
     * @param array|null $last
58
     */
59
    public function __construct(string $link = null, string $title = null, array $first = null, array $last = null)
60
    {
61
        $this->link = ($link ?? "?page=");
62
        $this->title = ($title ?? "Página");
63
        $this->first = ($first ?? ["Primeira página", "<<"]);
64
        $this->last = ($last ?? ["Última página", ">>"]);
65
    }
66
67
    /**
68
     * @param int $rows
69
     * @param int $limit
70
     * @param int|null $page
71
     * @param int $range
72
     * @param string|null $hash
73
     */
74
    public function pager(int $rows, int $limit = 10, int $page = null, int $range = 3, string $hash = null, array $params = []): void
75
    {
76
        $this->rows = $this->toPositive($rows);
77
        $this->limit = $this->toPositive($limit);
78
        $this->range = $this->toPositive($range);
79
        $this->pages = (int)ceil($this->rows / $this->limit);
80
        $this->page = ($page <= $this->pages ? $this->toPositive($page) : $this->pages);
81
82
        $this->offset = (($this->page * $this->limit) - $this->limit >= 0 ? ($this->page * $this->limit) - $this->limit : 0);
83
        $this->hash = (!empty($hash) ? "#{$hash}" : null);
84
		
85
		$this->addGetParams($params);
86
87
        if ($this->rows && $this->offset >= $this->rows) {
88
            header("Location: {$this->link}" . ceil($this->rows / $this->limit));
89
            exit;
90
        }
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function limit(): int
97
    {
98
        return $this->limit;
99
    }
100
101
    /**
102
     * @return int
103
     */
104
    public function offset(): int
105
    {
106
        return $this->offset;
107
    }
108
109
    /**
110
     * @return int
111
     */
112
    public function page()
113
    {
114
        return $this->page;
115
    }
116
117
    /**
118
     * @return int
119
     */
120
    public function pages()
121
    {
122
        return $this->pages;
123
    }
124
125
    /**
126
     * @param string $cssClass
127
     * @param bool $fixedFirstAndLastPage
128
     * @return null|string
129
     */
130
    public function render(string $cssClass = null, bool $fixedFirstAndLastPage = true): ?string
131
    {
132
        $this->class = $cssClass ?? "paginator";
133
134
        if ($this->rows > $this->limit):
135
            $paginator = "<nav class=\"{$this->class}\">";
136
            $paginator .= $this->firstPage($fixedFirstAndLastPage);
137
            $paginator .= $this->beforePages();
138
            $paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>";
139
            $paginator .= $this->afterPages();
140
            $paginator .= $this->lastPage($fixedFirstAndLastPage);
141
            $paginator .= "</nav>";
142
            return $paginator;
143
        endif;
144
145
        return null;
146
    }
147
148
    /**
149
     * @return null|string
150
     */
151
    private function beforePages(): ?string
152
    {
153
        $before = null;
154
        for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++):
155
            if ($iPag >= 1):
156
                $before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}{$this->params}\">{$iPag}</a>";
157
            endif;
158
        endfor;
159
160
        return $before;
161
    }
162
163
    /**
164
     * @return string|null
165
     */
166
    private function afterPages(): ?string
167
    {
168
        $after = null;
169
        for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++):
170
            if ($dPag <= $this->pages):
171
                $after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}{$this->params}\">{$dPag}</a>";
172
            endif;
173
        endfor;
174
175
        return $after;
176
    }
177
178
    /**
179
     * @param bool $fixedFirstAndLastPage
180
     * @return string|null
181
     */
182
    public function firstPage(bool $fixedFirstAndLastPage = true): ?string
183
    {
184
        if ($fixedFirstAndLastPage || $this->page != 1) {
185
            return "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}{$this->params}\">{$this->first[1]}</a>";
186
        }
187
        return null;
188
    }
189
190
    /**
191
     * @param bool $fixedFirstAndLastPage
192
     * @return string|null
193
     */
194
    public function lastPage(bool $fixedFirstAndLastPage = true): ?string
195
    {
196
        if ($fixedFirstAndLastPage || $this->page != $this->pages) {
197
            return "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}{$this->params}\">{$this->last[1]}</a>";
198
        }
199
        return null;
200
    }
201
202
    /**
203
     * @param $number
204
     * @return int
205
     */
206
    private function toPositive($number): int
207
    {
208
        return ($number >= 1 ? $number : 1);
209
    }
210
	
211
    /**
212
     * Add get parameters
213
     * @param array $params
214
     * @return null
215
     */
216
    private function addGetParams(array $params)
217
    {
218
        $this->params = '';
219
        
220
        if (count($params) > 0) {
221
			
222
            if (isset($params['page'])) {
223
                unset($params['page']);
224
            }
225
            
226
            $this->params  = '&';
227
            $this->params .= http_build_query($params);
228
        }
229
        
230
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type CoffeeCode\Paginator\Paginator which is incompatible with the documented return type null.
Loading history...
231
    }
232
}