34 lines
1.0 KiB
PHP
34 lines
1.0 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Support;
|
||
|
|
|
||
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Caps a listing to a small preview for unauthenticated visitors — always page 1,
|
||
|
|
* always a short page, regardless of what page/perPage the request asks for — while
|
||
|
|
* authenticated users get the real, full pagination. total() still reflects the true
|
||
|
|
* match count either way, so guests see "N results, sign in to see all" rather than a
|
||
|
|
* silently truncated list.
|
||
|
|
*/
|
||
|
|
class GuestPreview
|
||
|
|
{
|
||
|
|
public const LIMIT = 5;
|
||
|
|
|
||
|
|
public static function paginate(Builder|Relation $query, int $fullPerPage = 25, string $pageName = 'page'): LengthAwarePaginator
|
||
|
|
{
|
||
|
|
if (self::isLimited()) {
|
||
|
|
return $query->paginate(self::LIMIT, ['*'], $pageName, 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $query->paginate($fullPerPage, ['*'], $pageName);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function isLimited(): bool
|
||
|
|
{
|
||
|
|
return ! auth()->check();
|
||
|
|
}
|
||
|
|
}
|