taktik - laravel
This commit is contained in:
47
app/Http/Resources/PaginableResource.php
Normal file
47
app/Http/Resources/PaginableResource.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?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,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user