Files
bogazici-api/app/DTOs/CommentData.php
2026-03-27 10:41:54 +03:00

49 lines
1.3 KiB
PHP

<?php
namespace App\DTOs;
final readonly class CommentData
{
public function __construct(
public string $commentableType,
public int $commentableId,
public string $authorName,
public string $content,
public ?int $rating,
public bool $isApproved = false,
public ?string $adminReply = null,
) {}
/**
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
return new self(
commentableType: $data['commentable_type'],
commentableId: $data['commentable_id'],
content: $data['content'],
authorName: $data['author_name'],
rating: $data['rating'] ?? null,
isApproved: $data['is_approved'] ?? false,
adminReply: $data['admin_reply'] ?? null,
);
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'commentable_type' => $this->commentableType,
'commentable_id' => $this->commentableId,
'author_name' => $this->authorName,
'content' => $this->content,
'rating' => $this->rating,
'is_approved' => $this->isApproved,
'admin_reply' => $this->adminReply,
];
}
}