-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.php
25 lines (23 loc) · 918 Bytes
/
pages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
echo implode(' - ', generatePages($_GET['page'], $_GET['pages'] ?? 10));
function generatePages(
$currentPage,
$pagesCount,
$startCount = 1,
$middleCount = 2,
$endCount = 1,
$strSplit = '...',
$removeSinglePageSplitter = true
){
return array_reduce(array_merge(
range(1, $startCount),
range(max($currentPage - $middleCount, $startCount + 1), min($currentPage + $middleCount, $pagesCount - $endCount)),
range($pagesCount - $endCount + 1, $pagesCount)),
function($carry, $item) use ($pagesCount, $strSplit, $removeSinglePageSplitter) {
if (is_numeric(end($carry)) && $item - end($carry) > 1) {
$carry[] = ($removeSinglePageSplitter && $item - end($carry) == 2) ? $item - 1 : $strSplit;
}
$carry[] = $item;
return $carry;
}, []);
}