Passed
Pull Request — master (#154)
by
unknown
02:52
created
src/Libraries/Database/BasePaginator.php 2 patches
Indentation   +294 added lines, -294 removed lines patch added patch discarded remove patch
@@ -4,299 +4,299 @@
 block discarded – undo
4 4
 
5 5
 abstract class BasePaginator implements PaginatorInterface
6 6
 {
7
-	/**
8
-	 * @var string
9
-	 */
10
-	protected const PAGINATION_CLASS = 'pagination';
11
-
12
-	/**
13
-	 * @var string
14
-	 */
15
-	protected const PAGINATION_CLASS_ACTIVE = 'active';
16
-
17
-	/**
18
-	 * @var string
19
-	 */
20
-	protected const PER_PAGE = 'per_page';
21
-
22
-	/**
23
-	 * @var string
24
-	 */
25
-	protected const PAGE = 'page';
26
-
27
-	/**
28
-	 * @var int
29
-	 */
30
-	protected const FIRST_PAGE_NUMBER = 1;
31
-
32
-	/**
33
-	 * @var int
34
-	 */
35
-	protected $total;
36
-
37
-	/**
38
-	 * @var string
39
-	 */
40
-	protected $baseUrl;
41
-
42
-	/**
43
-	 * @var int
44
-	 */
45
-	protected $perPage;
46
-
47
-	/**
48
-	 * @var int
49
-	 */
50
-	protected $page;
51
-
52
-	/**
53
-	 * @return int
54
-	 */
55
-	public function currentPageNumber(): int
56
-	{
57
-		return $this->page;
58
-	}
59
-
60
-	/**
61
-	 * @param bool $withBaseUrl
62
-	 * @return string|null
63
-	 */
64
-	public function currentPageLink(bool $withBaseUrl = false): ?string
65
-	{
66
-		return $this->getPageLink($this->page, $withBaseUrl);
67
-	}
68
-
69
-	/**
70
-	 * @return int|null
71
-	 */
72
-	public function previousPageNumber(): ?int
73
-	{
74
-		$previous = null;
75
-		if ($this->page > 1) {
76
-			$previous = $this->page - 1;
77
-		} elseif ($this->page == 1) {
78
-			$previous = $this->page;
79
-		}
80
-
81
-		return $previous;
82
-	}
83
-
84
-	/**
85
-	 * @param bool $withBaseUrl
86
-	 * @return string|null
87
-	 */
88
-	public function previousPageLink(bool $withBaseUrl = false): ?string
89
-	{
90
-		return $this->getPageLink($this->previousPageNumber(), $withBaseUrl);
91
-	}
92
-
93
-	/**
94
-	 * @return int|null
95
-	 */
96
-	public function nextPageNumber(): ?int
97
-	{
98
-		$next = null;
99
-		if ($this->page < $this->lastPageNumber()) {
100
-			$next = $this->page + 1;
101
-		} elseif ($this->page == $this->lastPageNumber()) {
102
-			$next = $this->page;
103
-		}
104
-		return $next;
105
-	}
106
-
107
-	/**
108
-	 * @param bool $withBaseUrl
109
-	 * @return string|null
110
-	 */
111
-	public function nextPageLink(bool $withBaseUrl = false): ?string
112
-	{
113
-		return $this->getPageLink($this->nextPageNumber(), $withBaseUrl);
114
-	}
115
-
116
-	/**
117
-	 * @param bool $withBaseUrl
118
-	 * @return string|null
119
-	 */
120
-	public function firstPageLink(bool $withBaseUrl = false): ?string
121
-	{
122
-		return $this->getPageLink(self::FIRST_PAGE_NUMBER, $withBaseUrl);
123
-	}
124
-
125
-	/**
126
-	 * @return int
127
-	 */
128
-	public function lastPageNumber(): int
129
-	{
130
-		return (int)ceil($this->total() / $this->perPage);
131
-	}
132
-
133
-	/**
134
-	 * @param bool $withBaseUrl
135
-	 * @return string|null
136
-	 */
137
-	public function lastPageLink(bool $withBaseUrl = false): ?string
138
-	{
139
-		return $this->getPageLink($this->lastPageNumber(), $withBaseUrl);
140
-	}
141
-
142
-	/**
143
-	 * @return int
144
-	 */
145
-	public function perPage(): int
146
-	{
147
-		return $this->perPage;
148
-	}
149
-
150
-	/**
151
-	 * @return int
152
-	 */
153
-	public function total(): int
154
-	{
155
-		return $this->total;
156
-	}
157
-
158
-	/**
159
-	 * @param bool $withBaseUrl
160
-	 * @return array
161
-	 */
162
-	public function links(bool $withBaseUrl = false): array
163
-	{
164
-		$links = [];
165
-		for ($i = 1; $i <= $this->lastPageNumber(); $i++) {
166
-			$links[] = $this->getPageLink($i, $withBaseUrl);
167
-		}
168
-
169
-		return $links;
170
-	}
171
-
172
-	/**
173
-	 * @param bool $withBaseUrl
174
-	 * @return string
175
-	 */
176
-	protected function getUri(bool $withBaseUrl = false): string
177
-	{
178
-		$routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri());
179
-		$routeUrl = preg_replace('/&/', '?', $routeUrl, 1);
180
-		$url = $routeUrl;
181
-
182
-		if ($withBaseUrl) {
183
-			$url = $this->baseUrl . $routeUrl;
184
-		}
185
-
186
-		$delimiter = strpos($url, '?') ? '&' : '?';
187
-		return $url . $delimiter;
188
-	}
189
-
190
-	protected function getPageLink($pageNumber, $withBaseUrl = false): ?string
191
-	{
192
-		if (!empty($pageNumber)){
193
-			return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber;
194
-		}
195
-		return null;
196
-	}
197
-
198
-	/**
199
-	 * @param bool $withBaseUrl
200
-	 * @param $pageItemsCount
201
-	 * @return string|null
202
-	 */
203
-	public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string
204
-	{
205
-		$totalPages = $this->lastPageNumber();
206
-		if ($totalPages <= 1) {
207
-			return null;
208
-		}
209
-
210
-		if (!is_null($pageItemsCount) && $pageItemsCount < 3) {
211
-			$pageItemsCount = 3;
212
-		}
213
-
214
-		$pagination = '<ul class="'. self::PAGINATION_CLASS .'">';
215
-		$currentPage = $this->currentPageNumber();
7
+    /**
8
+     * @var string
9
+     */
10
+    protected const PAGINATION_CLASS = 'pagination';
11
+
12
+    /**
13
+     * @var string
14
+     */
15
+    protected const PAGINATION_CLASS_ACTIVE = 'active';
16
+
17
+    /**
18
+     * @var string
19
+     */
20
+    protected const PER_PAGE = 'per_page';
21
+
22
+    /**
23
+     * @var string
24
+     */
25
+    protected const PAGE = 'page';
26
+
27
+    /**
28
+     * @var int
29
+     */
30
+    protected const FIRST_PAGE_NUMBER = 1;
31
+
32
+    /**
33
+     * @var int
34
+     */
35
+    protected $total;
36
+
37
+    /**
38
+     * @var string
39
+     */
40
+    protected $baseUrl;
41
+
42
+    /**
43
+     * @var int
44
+     */
45
+    protected $perPage;
46
+
47
+    /**
48
+     * @var int
49
+     */
50
+    protected $page;
51
+
52
+    /**
53
+     * @return int
54
+     */
55
+    public function currentPageNumber(): int
56
+    {
57
+        return $this->page;
58
+    }
59
+
60
+    /**
61
+     * @param bool $withBaseUrl
62
+     * @return string|null
63
+     */
64
+    public function currentPageLink(bool $withBaseUrl = false): ?string
65
+    {
66
+        return $this->getPageLink($this->page, $withBaseUrl);
67
+    }
68
+
69
+    /**
70
+     * @return int|null
71
+     */
72
+    public function previousPageNumber(): ?int
73
+    {
74
+        $previous = null;
75
+        if ($this->page > 1) {
76
+            $previous = $this->page - 1;
77
+        } elseif ($this->page == 1) {
78
+            $previous = $this->page;
79
+        }
80
+
81
+        return $previous;
82
+    }
83
+
84
+    /**
85
+     * @param bool $withBaseUrl
86
+     * @return string|null
87
+     */
88
+    public function previousPageLink(bool $withBaseUrl = false): ?string
89
+    {
90
+        return $this->getPageLink($this->previousPageNumber(), $withBaseUrl);
91
+    }
92
+
93
+    /**
94
+     * @return int|null
95
+     */
96
+    public function nextPageNumber(): ?int
97
+    {
98
+        $next = null;
99
+        if ($this->page < $this->lastPageNumber()) {
100
+            $next = $this->page + 1;
101
+        } elseif ($this->page == $this->lastPageNumber()) {
102
+            $next = $this->page;
103
+        }
104
+        return $next;
105
+    }
106
+
107
+    /**
108
+     * @param bool $withBaseUrl
109
+     * @return string|null
110
+     */
111
+    public function nextPageLink(bool $withBaseUrl = false): ?string
112
+    {
113
+        return $this->getPageLink($this->nextPageNumber(), $withBaseUrl);
114
+    }
115
+
116
+    /**
117
+     * @param bool $withBaseUrl
118
+     * @return string|null
119
+     */
120
+    public function firstPageLink(bool $withBaseUrl = false): ?string
121
+    {
122
+        return $this->getPageLink(self::FIRST_PAGE_NUMBER, $withBaseUrl);
123
+    }
124
+
125
+    /**
126
+     * @return int
127
+     */
128
+    public function lastPageNumber(): int
129
+    {
130
+        return (int)ceil($this->total() / $this->perPage);
131
+    }
132
+
133
+    /**
134
+     * @param bool $withBaseUrl
135
+     * @return string|null
136
+     */
137
+    public function lastPageLink(bool $withBaseUrl = false): ?string
138
+    {
139
+        return $this->getPageLink($this->lastPageNumber(), $withBaseUrl);
140
+    }
141
+
142
+    /**
143
+     * @return int
144
+     */
145
+    public function perPage(): int
146
+    {
147
+        return $this->perPage;
148
+    }
149
+
150
+    /**
151
+     * @return int
152
+     */
153
+    public function total(): int
154
+    {
155
+        return $this->total;
156
+    }
157
+
158
+    /**
159
+     * @param bool $withBaseUrl
160
+     * @return array
161
+     */
162
+    public function links(bool $withBaseUrl = false): array
163
+    {
164
+        $links = [];
165
+        for ($i = 1; $i <= $this->lastPageNumber(); $i++) {
166
+            $links[] = $this->getPageLink($i, $withBaseUrl);
167
+        }
168
+
169
+        return $links;
170
+    }
171
+
172
+    /**
173
+     * @param bool $withBaseUrl
174
+     * @return string
175
+     */
176
+    protected function getUri(bool $withBaseUrl = false): string
177
+    {
178
+        $routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri());
179
+        $routeUrl = preg_replace('/&/', '?', $routeUrl, 1);
180
+        $url = $routeUrl;
181
+
182
+        if ($withBaseUrl) {
183
+            $url = $this->baseUrl . $routeUrl;
184
+        }
185
+
186
+        $delimiter = strpos($url, '?') ? '&' : '?';
187
+        return $url . $delimiter;
188
+    }
189
+
190
+    protected function getPageLink($pageNumber, $withBaseUrl = false): ?string
191
+    {
192
+        if (!empty($pageNumber)){
193
+            return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber;
194
+        }
195
+        return null;
196
+    }
197
+
198
+    /**
199
+     * @param bool $withBaseUrl
200
+     * @param $pageItemsCount
201
+     * @return string|null
202
+     */
203
+    public function getPagination(bool $withBaseUrl = false, $pageItemsCount = null): ?string
204
+    {
205
+        $totalPages = $this->lastPageNumber();
206
+        if ($totalPages <= 1) {
207
+            return null;
208
+        }
209
+
210
+        if (!is_null($pageItemsCount) && $pageItemsCount < 3) {
211
+            $pageItemsCount = 3;
212
+        }
213
+
214
+        $pagination = '<ul class="'. self::PAGINATION_CLASS .'">';
215
+        $currentPage = $this->currentPageNumber();
216 216
 		
217
-		if ($currentPage > 1) {
218
-			$pagination .= $this->getPreviousPageItem($this->previousPageLink());
219
-		}
220
-
221
-		if ($pageItemsCount) {
222
-			$pagination = $this->getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl);
223
-		}
224
-
225
-		if ($currentPage < $totalPages) {
226
-			$pagination .= $this->getNextPageItem($this->nextPageLink());
227
-		}
228
-
229
-		$pagination .= '</ul>';
230
-
231
-		return $pagination;
232
-	}
233
-
234
-	/**
235
-	 * @param $pagination
236
-	 * @param $currentPage
237
-	 * @param $totalPages
238
-	 * @param $pageItemsCount
239
-	 * @param $withBaseUrl
240
-	 * @return mixed|string
241
-	 */
242
-	protected function getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl = false)
243
-	{
244
-		$startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2));
245
-		$endPage = min($totalPages, $startPage + $pageItemsCount - 3);
246
-		$startPage = max(1, $endPage - $pageItemsCount + 3);
247
-
248
-		if ($startPage > 1) {
249
-			$pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>';
250
-			if ($startPage > 2) {
251
-				$pagination .= '<li><span>...</span></li>';
252
-			}
253
-		}
254
-
255
-		$links = $this->links($withBaseUrl);
256
-		$pagination = $this->getItemsLinks($pagination, $startPage, $endPage, $currentPage, $links);
257
-
258
-		if ($endPage < $totalPages) {
259
-			if ($endPage < $totalPages - 1) {
260
-				$pagination .= '<li><span>...</span></li>';
261
-			}
262
-
263
-			$pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>';
264
-		}
265
-		return $pagination;
266
-	}
267
-
268
-	/**
269
-	 * @param string|null $nextPageLink
270
-	 * @return string
271
-	 */
272
-	protected function getNextPageItem(?string $nextPageLink): string
273
-	{
274
-		$link = '';
275
-		if (!empty($nextPageLink)){
276
-			$link = '<li><a href="' . $nextPageLink . '">Next &raquo;</a></li>';
277
-		}
278
-		return $link;
279
-	}
280
-
281
-	/**
282
-	 * @param string|null $previousPageLink
283
-	 * @return string
284
-	 */
285
-	protected function getPreviousPageItem(?string $previousPageLink): string
286
-	{
287
-		$link = '';
288
-		if (!empty($previousPageLink)){
289
-			$link = '<li><a href="' . $previousPageLink . '">&laquo; Previous</a></li>';
290
-		}
291
-		return $link;
292
-	}
293
-
294
-	protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links)
295
-	{
296
-		for ($i = $startPage; $i <= $endPage; $i++) {
297
-			$active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : '';
298
-			$pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>';
299
-		}
300
-		return $pagination;
301
-	}
217
+        if ($currentPage > 1) {
218
+            $pagination .= $this->getPreviousPageItem($this->previousPageLink());
219
+        }
220
+
221
+        if ($pageItemsCount) {
222
+            $pagination = $this->getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl);
223
+        }
224
+
225
+        if ($currentPage < $totalPages) {
226
+            $pagination .= $this->getNextPageItem($this->nextPageLink());
227
+        }
228
+
229
+        $pagination .= '</ul>';
230
+
231
+        return $pagination;
232
+    }
233
+
234
+    /**
235
+     * @param $pagination
236
+     * @param $currentPage
237
+     * @param $totalPages
238
+     * @param $pageItemsCount
239
+     * @param $withBaseUrl
240
+     * @return mixed|string
241
+     */
242
+    protected function getPageItems($pagination, $currentPage, $totalPages, $pageItemsCount, $withBaseUrl = false)
243
+    {
244
+        $startPage = max(1, $currentPage - ceil(($pageItemsCount - 3) / 2));
245
+        $endPage = min($totalPages, $startPage + $pageItemsCount - 3);
246
+        $startPage = max(1, $endPage - $pageItemsCount + 3);
247
+
248
+        if ($startPage > 1) {
249
+            $pagination .= '<li><a href="' . $this->firstPageLink() . '">1</a></li>';
250
+            if ($startPage > 2) {
251
+                $pagination .= '<li><span>...</span></li>';
252
+            }
253
+        }
254
+
255
+        $links = $this->links($withBaseUrl);
256
+        $pagination = $this->getItemsLinks($pagination, $startPage, $endPage, $currentPage, $links);
257
+
258
+        if ($endPage < $totalPages) {
259
+            if ($endPage < $totalPages - 1) {
260
+                $pagination .= '<li><span>...</span></li>';
261
+            }
262
+
263
+            $pagination .= '<li><a href="' . $links[$totalPages - 1] . '">' . $totalPages . '</a></li>';
264
+        }
265
+        return $pagination;
266
+    }
267
+
268
+    /**
269
+     * @param string|null $nextPageLink
270
+     * @return string
271
+     */
272
+    protected function getNextPageItem(?string $nextPageLink): string
273
+    {
274
+        $link = '';
275
+        if (!empty($nextPageLink)){
276
+            $link = '<li><a href="' . $nextPageLink . '">Next &raquo;</a></li>';
277
+        }
278
+        return $link;
279
+    }
280
+
281
+    /**
282
+     * @param string|null $previousPageLink
283
+     * @return string
284
+     */
285
+    protected function getPreviousPageItem(?string $previousPageLink): string
286
+    {
287
+        $link = '';
288
+        if (!empty($previousPageLink)){
289
+            $link = '<li><a href="' . $previousPageLink . '">&laquo; Previous</a></li>';
290
+        }
291
+        return $link;
292
+    }
293
+
294
+    protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links)
295
+    {
296
+        for ($i = $startPage; $i <= $endPage; $i++) {
297
+            $active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : '';
298
+            $pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>';
299
+        }
300
+        return $pagination;
301
+    }
302 302
 }
303 303
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
 	 */
128 128
 	public function lastPageNumber(): int
129 129
 	{
130
-		return (int)ceil($this->total() / $this->perPage);
130
+		return (int) ceil($this->total() / $this->perPage);
131 131
 	}
132 132
 
133 133
 	/**
@@ -189,8 +189,8 @@  discard block
 block discarded – undo
189 189
 
190 190
 	protected function getPageLink($pageNumber, $withBaseUrl = false): ?string
191 191
 	{
192
-		if (!empty($pageNumber)){
193
-			return $this->getUri($withBaseUrl) . self::PER_PAGE .'=' . $this->perPage . '&'. self::PAGE .'=' . $pageNumber;
192
+		if (!empty($pageNumber)) {
193
+			return $this->getUri($withBaseUrl) . self::PER_PAGE . '=' . $this->perPage . '&' . self::PAGE . '=' . $pageNumber;
194 194
 		}
195 195
 		return null;
196 196
 	}
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
 			$pageItemsCount = 3;
212 212
 		}
213 213
 
214
-		$pagination = '<ul class="'. self::PAGINATION_CLASS .'">';
214
+		$pagination = '<ul class="' . self::PAGINATION_CLASS . '">';
215 215
 		$currentPage = $this->currentPageNumber();
216 216
 		
217 217
 		if ($currentPage > 1) {
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
 	protected function getNextPageItem(?string $nextPageLink): string
273 273
 	{
274 274
 		$link = '';
275
-		if (!empty($nextPageLink)){
275
+		if (!empty($nextPageLink)) {
276 276
 			$link = '<li><a href="' . $nextPageLink . '">Next &raquo;</a></li>';
277 277
 		}
278 278
 		return $link;
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
 	protected function getPreviousPageItem(?string $previousPageLink): string
286 286
 	{
287 287
 		$link = '';
288
-		if (!empty($previousPageLink)){
288
+		if (!empty($previousPageLink)) {
289 289
 			$link = '<li><a href="' . $previousPageLink . '">&laquo; Previous</a></li>';
290 290
 		}
291 291
 		return $link;
@@ -294,7 +294,7 @@  discard block
 block discarded – undo
294 294
 	protected function getItemsLinks($pagination, $startPage, $endPage, $currentPage, array $links)
295 295
 	{
296 296
 		for ($i = $startPage; $i <= $endPage; $i++) {
297
-			$active = $i == $currentPage ? 'class="'. self::PAGINATION_CLASS_ACTIVE .'"' : '';
297
+			$active = $i == $currentPage ? 'class="' . self::PAGINATION_CLASS_ACTIVE . '"' : '';
298 298
 			$pagination .= '<li ' . $active . '><a href="' . $links[$i - 1] . '">' . $i . '</a></li>';
299 299
 		}
300 300
 		return $pagination;
Please login to merge, or discard this patch.