forked from projects/fipamo
91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use function _\filter;
|
||
|
|
||
|
class PaginateService
|
||
|
{
|
||
|
protected $content;
|
||
|
|
||
|
public function __construct(ContentService $contentService)
|
||
|
{
|
||
|
$this->content = $contentService;
|
||
|
}
|
||
|
|
||
|
public function getPage(int $page, int $limit, string $sort = null)
|
||
|
{
|
||
|
$content = $this->content->loadAllPages();
|
||
|
|
||
|
$published = filter($content, function ($item) {
|
||
|
return $item['published'] == true && $item['deleted'] == false;
|
||
|
});
|
||
|
$deleted = filter($content, function ($item) {
|
||
|
return $item['deleted'] == true;
|
||
|
});
|
||
|
|
||
|
// $all = $content;
|
||
|
$all = filter($content, function ($item) {
|
||
|
return $item['deleted'] == false;
|
||
|
});
|
||
|
$filter = isset($sort) ? $sort : 'all';
|
||
|
switch ($filter) {
|
||
|
case 'published':
|
||
|
$filtered = $published;
|
||
|
break;
|
||
|
case 'deleted':
|
||
|
$filtered = $deleted;
|
||
|
break;
|
||
|
default:
|
||
|
$filtered = $all;
|
||
|
break;
|
||
|
}
|
||
|
$numOfPages = ceil(count($filtered) / ($limit + 1));
|
||
|
$folder = [];
|
||
|
|
||
|
if (count($filtered) != 0) {
|
||
|
if (count($filtered) < $limit) {
|
||
|
$limit = count($filtered) - 1;
|
||
|
}
|
||
|
$range = $page * $limit - $limit;
|
||
|
|
||
|
if ($range != 0) {
|
||
|
$range = $range + 1;
|
||
|
}
|
||
|
for ($i = 0; $i <= $limit; ++$i) {
|
||
|
if (isset($filtered[$i + $range])) {
|
||
|
array_push($folder, $filtered[$i + $range]);
|
||
|
} else {
|
||
|
// chill out
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$prev = $page - 1;
|
||
|
if ($prev <= 0) {
|
||
|
$prev = $numOfPages;
|
||
|
}
|
||
|
|
||
|
$next = $page + 1;
|
||
|
if ($next > $numOfPages) {
|
||
|
$next = 1;
|
||
|
}
|
||
|
|
||
|
return [
|
||
|
'pages' => $folder,
|
||
|
'numOfPages' => $numOfPages,
|
||
|
'entryCount' => count($filtered),
|
||
|
'paginate' => [
|
||
|
'sort' => $sort,
|
||
|
'nextPage' => $next,
|
||
|
'prevPage' => $prev,
|
||
|
],
|
||
|
'stats' => [
|
||
|
'all' => count($all),
|
||
|
'published' => count($published),
|
||
|
'deleted' => count($deleted),
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
}
|