Passed
Push — master ( b83a20...221be0 )
by Robson
02:53 queued 10s
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
     * @param array $params
74
     */
75
    public function pager(int $rows, int $limit = 10, int $page = null, int $range = 3, string $hash = null, array $params = []): void
76
    {
77
        $this->rows = $this->toPositive($rows);
78
        $this->limit = $this->toPositive($limit);
79
        $this->range = $this->toPositive($range);
80
        $this->pages = (int)ceil($this->rows / $this->limit);
81
        $this->page = ($page <= $this->pages ? $this->toPositive($page) : $this->pages);
82
83
        $this->offset = (($this->page * $this->limit) - $this->limit >= 0 ? ($this->page * $this->limit) - $this->limit : 0);
84
        $this->hash = (!empty($hash) ? "#{$hash}" : null);
85
		
86
		$this->addGetParams($params);
87
88
        if ($this->rows && $this->offset >= $this->rows) {
89
            header("Location: {$this->link}" . ceil($this->rows / $this->limit));
90
            exit;
91
        }
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function limit(): int
98
    {
99
        return $this->limit;
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    public function offset(): int
106
    {
107
        return $this->offset;
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function page()
114
    {
115
        return $this->page;
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function pages()
122
    {
123
        return $this->pages;
124
    }
125
126
    /**
127
     * @param string $cssClass
128
     * @param bool $fixedFirstAndLastPage
129
     * @return null|string
130
     */
131
    public function render(string $cssClass = null, bool $fixedFirstAndLastPage = true): ?string
132
    {
133
        $this->class = $cssClass ?? "paginator";
134
135
        if ($this->rows > $this->limit):
136
            $paginator = "<nav class=\"{$this->class}\">";
137
            $paginator .= $this->firstPage($fixedFirstAndLastPage);
138
            $paginator .= $this->beforePages();
139
            $paginator .= "<span class=\"{$this->class}_item {$this->class}_active\">{$this->page}</span>";
140
            $paginator .= $this->afterPages();
141
            $paginator .= $this->lastPage($fixedFirstAndLastPage);
142
            $paginator .= "</nav>";
143
            return $paginator;
144
        endif;
145
146
        return null;
147
    }
148
149
    /**
150
     * @return null|string
151
     */
152
    private function beforePages(): ?string
153
    {
154
        $before = null;
155
        for ($iPag = $this->page - $this->range; $iPag <= $this->page - 1; $iPag++):
156
            if ($iPag >= 1):
157
                $before .= "<a class='{$this->class}_item' title=\"{$this->title} {$iPag}\" href=\"{$this->link}{$iPag}{$this->hash}{$this->params}\">{$iPag}</a>";
158
            endif;
159
        endfor;
160
161
        return $before;
162
    }
163
164
    /**
165
     * @return string|null
166
     */
167
    private function afterPages(): ?string
168
    {
169
        $after = null;
170
        for ($dPag = $this->page + 1; $dPag <= $this->page + $this->range; $dPag++):
171
            if ($dPag <= $this->pages):
172
                $after .= "<a class='{$this->class}_item' title=\"{$this->title} {$dPag}\" href=\"{$this->link}{$dPag}{$this->hash}{$this->params}\">{$dPag}</a>";
173
            endif;
174
        endfor;
175
176
        return $after;
177
    }
178
179
    /**
180
     * @param bool $fixedFirstAndLastPage
181
     * @return string|null
182
     */
183
    public function firstPage(bool $fixedFirstAndLastPage = true): ?string
184
    {
185
        if ($fixedFirstAndLastPage || $this->page != 1) {
186
            return "<a class='{$this->class}_item' title=\"{$this->first[0]}\" href=\"{$this->link}1{$this->hash}{$this->params}\">{$this->first[1]}</a>";
187
        }
188
        return null;
189
    }
190
191
    /**
192
     * @param bool $fixedFirstAndLastPage
193
     * @return string|null
194
     */
195
    public function lastPage(bool $fixedFirstAndLastPage = true): ?string
196
    {
197
        if ($fixedFirstAndLastPage || $this->page != $this->pages) {
198
            return "<a class='{$this->class}_item' title=\"{$this->last[0]}\" href=\"{$this->link}{$this->pages}{$this->hash}{$this->params}\">{$this->last[1]}</a>";
199
        }
200
        return null;
201
    }
202
203
    /**
204
     * @param $number
205
     * @return int
206
     */
207
    private function toPositive($number): int
208
    {
209
        return ($number >= 1 ? $number : 1);
210
    }
211
	
212
    /**
213
     * Add get parameters
214
     * @param array $params
215
     * @return null
216
     */
217
    private function addGetParams(array $params)
218
    {
219
        $this->params = '';
220
        
221
        if (count($params) > 0) {
222
			
223
            if (isset($params['page'])) {
224
                unset($params['page']);
225
            }
226
            
227
            $this->params  = '&';
228
            $this->params .= http_build_query($params);
229
        }
230
        
231
        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...
232
    }
233
}