145 lines
5.1 KiB
PHP
145 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Dtos;
|
|
|
|
class RecipeDto
|
|
{
|
|
public ?int $id = null;
|
|
public ?string $title = null;
|
|
public ?string $slug = null;
|
|
public ?string $description = null;
|
|
public ?int $prepTime = null;
|
|
public ?int $cookTime = null;
|
|
public ?int $totalTime = null;
|
|
public ?int $servings = null;
|
|
public ?string $difficulty = null;
|
|
public ?string $cuisine = null;
|
|
public ?string $category = null;
|
|
public ?int $calories = null;
|
|
public ?string $featuredImage = null;
|
|
public ?string $videoUrl = null;
|
|
public ?int $status = null;
|
|
public ?int $viewsCount = null;
|
|
public ?float $ratingAvg = null;
|
|
public ?int $ratingCount = null;
|
|
public ?int $createdAt = null;
|
|
public ?int $updatedAt = null;
|
|
|
|
/** @var RecipeIngredientDto[] */
|
|
public array $ingredients = [];
|
|
|
|
/** @var RecipeStepDto[] */
|
|
public array $steps = [];
|
|
|
|
/** @var RecipeImageDto[] */
|
|
public array $images = [];
|
|
|
|
public ?RecipeNutritionDto $nutrition = null;
|
|
|
|
/** @var TagDto[] */
|
|
public array $tags = [];
|
|
|
|
/**
|
|
* Create DTO from array
|
|
*
|
|
* @param array $data
|
|
* @return static
|
|
*/
|
|
public static function fromArray(array $data): static
|
|
{
|
|
$dto = new static();
|
|
|
|
$dto->id = $data['id'] ?? null;
|
|
$dto->title = $data['title'] ?? null;
|
|
$dto->slug = $data['slug'] ?? null;
|
|
$dto->description = $data['description'] ?? null;
|
|
$dto->prepTime = isset($data['prepTime']) ? (int)$data['prepTime'] : null;
|
|
$dto->cookTime = isset($data['cookTime']) ? (int)$data['cookTime'] : null;
|
|
$dto->totalTime = isset($data['totalTime']) ? (int)$data['totalTime'] : null;
|
|
$dto->servings = isset($data['servings']) ? (int)$data['servings'] : null;
|
|
$dto->difficulty = $data['difficulty'] ?? null;
|
|
$dto->cuisine = $data['cuisine'] ?? null;
|
|
$dto->category = $data['category'] ?? null;
|
|
$dto->calories = isset($data['calories']) ? (int)$data['calories'] : null;
|
|
$dto->featuredImage = $data['featuredImage'] ?? null;
|
|
$dto->videoUrl = $data['videoUrl'] ?? null;
|
|
$dto->status = isset($data['status']) ? (int)$data['status'] : null;
|
|
$dto->viewsCount = isset($data['viewsCount']) ? (int)$data['viewsCount'] : null;
|
|
$dto->ratingAvg = isset($data['ratingAvg']) ? (float)$data['ratingAvg'] : null;
|
|
$dto->ratingCount = isset($data['ratingCount']) ? (int)$data['ratingCount'] : null;
|
|
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
|
$dto->updatedAt = isset($data['updatedAt']) ? (int)$data['updatedAt'] : null;
|
|
|
|
// Parse nested DTOs
|
|
if (isset($data['ingredients']) && is_array($data['ingredients'])) {
|
|
$dto->ingredients = array_map(
|
|
fn($item) => RecipeIngredientDto::fromArray($item),
|
|
$data['ingredients']
|
|
);
|
|
}
|
|
|
|
if (isset($data['steps']) && is_array($data['steps'])) {
|
|
$dto->steps = array_map(
|
|
fn($item) => RecipeStepDto::fromArray($item),
|
|
$data['steps']
|
|
);
|
|
}
|
|
|
|
if (isset($data['images']) && is_array($data['images'])) {
|
|
$dto->images = array_map(
|
|
fn($item) => RecipeImageDto::fromArray($item),
|
|
$data['images']
|
|
);
|
|
}
|
|
|
|
if (isset($data['nutrition']) && is_array($data['nutrition'])) {
|
|
$dto->nutrition = RecipeNutritionDto::fromArray($data['nutrition']);
|
|
}
|
|
|
|
if (isset($data['tags']) && is_array($data['tags'])) {
|
|
$dto->tags = array_map(
|
|
fn($item) => TagDto::fromArray($item),
|
|
$data['tags']
|
|
);
|
|
}
|
|
|
|
return $dto;
|
|
}
|
|
|
|
/**
|
|
* Convert DTO to array
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'title' => $this->title,
|
|
'slug' => $this->slug,
|
|
'description' => $this->description,
|
|
'prepTime' => $this->prepTime,
|
|
'cookTime' => $this->cookTime,
|
|
'totalTime' => $this->totalTime,
|
|
'servings' => $this->servings,
|
|
'difficulty' => $this->difficulty,
|
|
'cuisine' => $this->cuisine,
|
|
'category' => $this->category,
|
|
'calories' => $this->calories,
|
|
'featuredImage' => $this->featuredImage,
|
|
'videoUrl' => $this->videoUrl,
|
|
'status' => $this->status,
|
|
'viewsCount' => $this->viewsCount,
|
|
'ratingAvg' => $this->ratingAvg,
|
|
'ratingCount' => $this->ratingCount,
|
|
'createdAt' => $this->createdAt,
|
|
'updatedAt' => $this->updatedAt,
|
|
'ingredients' => array_map(fn($item) => $item->toArray(), $this->ingredients),
|
|
'steps' => array_map(fn($item) => $item->toArray(), $this->steps),
|
|
'images' => array_map(fn($item) => $item->toArray(), $this->images),
|
|
'nutrition' => $this->nutrition?->toArray(),
|
|
'tags' => array_map(fn($item) => $item->toArray(), $this->tags),
|
|
];
|
|
}
|
|
}
|