ldc-gen-game-web/app/Lib/Helpers/CollectionHelper.php

53 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2023-08-21 10:22:07 +02:00
<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
namespace App\Lib\Helpers;
2023-08-21 10:22:07 +02:00
use Illuminate\Container\Container;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class CollectionHelper
{
/**
* Paginate a collection
*/
public static function paginate(Collection $results, int $pageSize): LengthAwarePaginator
{
$page = Paginator::resolveCurrentPage('page');
return self::paginateManually($results, $pageSize, $page);
}
/**
* Paginate a collection
*/
public static function paginateManually(Collection $results, int $pageSize, int $page = 1): LengthAwarePaginator
{
$total = $results->count();
return self::paginator($results->forPage($page, $pageSize), $total, $pageSize, $page, [
2026-10-02 12:09:46 +02:00
'path' => Paginator::resolveCurrentPath(),
2023-08-21 10:22:07 +02:00
'pageName' => 'page',
]);
}
/**
* Create a new length-aware paginator instance.
*
2026-10-02 12:09:46 +02:00
* @return LengthAwarePaginator
2023-08-21 10:22:07 +02:00
*/
protected static function paginator(Collection $items, int $total, int $perPage, int $currentPage, array $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, [
2026-10-02 12:09:46 +02:00
'items' => $items,
'total' => $total,
'perPage' => $perPage,
2023-08-21 10:22:07 +02:00
'currentPage' => $currentPage,
2026-10-02 12:09:46 +02:00
'options' => $options,
2023-08-21 10:22:07 +02:00
]);
}
}