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), ]; } }