taktik-laravel-pohovor/app/Http/Resources/PaginableResource.php

48 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2025-01-23 00:19:07 +01:00
<?php
declare(strict_types=1);
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use OpenApi\Annotations as OA;
/**
* @template T
* @mixin \App\Services\PaginableResource<T>
* Souhrnný formát výstupu pro paginovaná data s metadaty.
*
* @OA\Schema(
* schema="PaginableResourceMeta",
* type="object",
* title="Paginable Resource (meta)",
* description="A paginated collection with meta information",
* @OA\Property(property="totalCount", type="integer", example=1),
* @OA\Property(property="pages", type="integer", example=10),
* )
*/
class PaginableResource extends JsonResource
{
public function __construct(mixed $resource, protected readonly string $classResource)
{
parent::__construct($resource);
}
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'items' => $this->classResource::make($this->data),
'meta' => [
'totalCount' => $this->totalCount,
'pages' => $this->totalPages,
]
];
}
}