90 lines
2.5 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 App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use OpenApi\Annotations as OA;
/**
* @mixin Post
* @OA\Schema(
* schema="PostResource",
* type="object",
* title="Post Resource",
* description="Resource representing a single post",
* @OA\Property(
* property="id",
* type="integer",
* description="ID of the post",
* example=1
* ),
* @OA\Property(
* property="title",
* type="string",
* description="Title of the post",
* example="Sample Post Title"
* ),
* @OA\Property(
* property="content",
* type="string",
* description="Content of the post",
* example="This is a sample post content."
* ),
* @OA\Property(
* property="category_id",
* type="integer",
* description="Category ID the post belongs to",
* example=1
* ),
* @OA\Property(
* property="category",
* description="Category object",
* ref="#/components/schemas/CategoryResource",
* ),
* @OA\Property(
* property="tags",
* ref="#/components/schemas/TagCollection",
* description="Collection of tags related to the post"
* ),
* @OA\Property(
* property="created_at",
* type="string",
* format="date-time",
* description="Timestamp when the post was created",
* example="2023-12-10T15:24:00Z"
* ),
* @OA\Property(
* property="updated_at",
* type="string",
* format="date-time",
* description="Timestamp when the post was last updated",
* example="2023-12-10T15:26:00Z"
* )
* )
*/
class PostResource extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'category_id' => $this->category_id,
'category' => new CategoryResource($this->whenLoaded('category')),
'tags' => new TagCollection($this->whenLoaded('tags')),
'created_at' => $this->created_at->toDateTimeString(),
'updated_at' => $this->updated_at->toDateTimeString(),
];
}
}