| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Webkul\BagistoApi\State;
- use ApiPlatform\Laravel\Eloquent\Paginator;
- use ApiPlatform\Metadata\Operation;
- use ApiPlatform\State\Pagination\Pagination;
- use ApiPlatform\State\ProviderInterface;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Webkul\BagistoApi\Models\Attribute;
- class AttributeCollectionProvider implements ProviderInterface
- {
- public function __construct(
- private readonly Pagination $pagination
- ) {}
- public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
- {
- $args = $context['args'] ?? [];
- $first = isset($args['first']) ? (int) $args['first'] : null;
- $last = isset($args['last']) ? (int) $args['last'] : null;
- $after = $args['after'] ?? null;
- $before = $args['before'] ?? null;
- $defaultPerPage = 30;
- // Determine page size
- if ($first !== null) {
- $perPage = $first;
- } elseif ($last !== null) {
- $perPage = $last;
- } else {
- $perPage = $defaultPerPage;
- }
- $offset = 0;
- if ($after) {
- $decoded = base64_decode($after, true);
- $offset = ctype_digit((string) $decoded) ? ((int) $decoded + 1) : 0;
- }
- if ($before) {
- $decoded = base64_decode($before, true);
- $cursor = ctype_digit((string) $decoded) ? (int) $decoded : 0;
- $offset = max(0, $cursor - $perPage);
- }
- $query = Attribute::query()
- ->with(['options', 'translations', 'translation', 'options.translations', 'options.translation'])
- ->orderBy('id', 'asc');
- $total = (clone $query)->count();
- if ($offset > $total) {
- $offset = max(0, $total - $perPage);
- }
- $items = $query
- ->offset($offset)
- ->limit($perPage)
- ->get();
- $currentPage = $total > 0 ? (int) floor($offset / $perPage) + 1 : 1;
- return new Paginator(
- new LengthAwarePaginator(
- $items,
- $total,
- $perPage,
- $currentPage,
- ['path' => request()->url()]
- )
- );
- }
- }
|