ldc-ovoteam/app/Dto/RecipeDto.php
Jérémy Munsch 04a9f97719
feat(recipes): implement CRUD actions for recipes
- Added actions for creating, updating, deleting, force deleting, and restoring recipes.
- Introduced input classes for each action to handle data validation and transfer.
- Implemented DTOs for recipes to facilitate data transfer between layers.
- Updated RecipeController to utilize new actions and input classes.
- Enhanced recipe model to manage picture URLs and storage.
- Updated frontend components to handle new data structures and display images correctly.
2026-11-05 11:54:19 +01:00

155 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Dto;
use App\Lib\PublicS3Disk;
use App\Models\Product;
use App\Models\Recipe;
use Carbon\CarbonInterface;
use Domain\Models\Entities\Recipe as DomainRecipe;
use Domain\Models\ValueObjects\Products\ProductCategory;
use Domain\Models\ValueObjects\Recipes\ConsumptionTime;
use Domain\Models\ValueObjects\Recipes\Inspiration;
use Domain\Models\ValueObjects\Recipes\Season;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;
/**
* @implements Arrayable<string, mixed>
*/
final readonly class RecipeDto implements Arrayable, JsonSerializable
{
/**
* @param string[] $products
*/
public function __construct(
public string $id,
public string $seoTitle,
public string $seoDescription,
public string $name,
public ?string $picture,
public ?string $pictureUrl,
public string $pictureAlt,
public string $pictureTitle,
public string $slug,
public ProductCategory $productCategory,
public ConsumptionTime $consomptionTime,
public Inspiration $inspiration,
public Season $season,
public ?string $description,
public ?string $details,
public string $ingredientsList,
public string $preparationSteps,
public ?string $tipsAndAdvices,
public array $products,
public ?CarbonInterface $createdAt,
public ?CarbonInterface $updatedAt,
public ?CarbonInterface $deletedAt,
) {
}
public static function fromModel(Recipe $recipe, PublicS3Disk $publicS3Disk): self
{
$picturePath = $recipe->picture?->value;
$products = $recipe->relationLoaded('products')
? $recipe->products->map(fn (Product $product): string => (string) $product->id)->all()
: [];
return new self(
id: (string) $recipe->id,
seoTitle: (string) $recipe->seo_title,
seoDescription: (string) $recipe->seo_description,
name: (string) $recipe->name,
picture: $picturePath,
pictureUrl: $picturePath !== null ? $publicS3Disk->publicUrlOf($picturePath) : null,
pictureAlt: (string) $recipe->picture_alt,
pictureTitle: (string) $recipe->picture_title,
slug: (string) $recipe->slug,
productCategory: $recipe->product_category,
consomptionTime: $recipe->consomption_time,
inspiration: $recipe->inspiration,
season: $recipe->season,
description: $recipe->description,
details: $recipe->details,
ingredientsList: (string) $recipe->ingredients_list,
preparationSteps: (string) $recipe->preparation_steps,
tipsAndAdvices: $recipe->tips_and_advices,
products: $products,
createdAt: $recipe->created_at,
updatedAt: $recipe->updated_at,
deletedAt: $recipe->deleted_at,
);
}
public static function fromEntity(
DomainRecipe $recipe,
PublicS3Disk $publicS3Disk
): self {
return new self(
id: (string) $recipe->id->value,
seoTitle: (string) ($recipe->seoTitle ?? ''),
seoDescription: (string) ($recipe->seoDescription ?? ''),
name: $recipe->name,
picture: $recipe->picture,
pictureUrl: $recipe->picture !== null ? $publicS3Disk->publicUrlOf($recipe->picture) : null,
pictureAlt: '',
pictureTitle: '',
slug: $recipe->slug,
productCategory: $recipe->productRange,
consomptionTime: $recipe->consomptionTime,
inspiration: $recipe->inspiration,
season: $recipe->season,
description: $recipe->description,
details: $recipe->details,
ingredientsList: $recipe->ingredientsList,
preparationSteps: $recipe->preparationSteps,
tipsAndAdvices: $recipe->tipsAndAdvices,
products: [],
createdAt: null,
updatedAt: null,
deletedAt: null,
);
}
/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'id' => $this->id,
'seo_title' => $this->seoTitle,
'seo_description' => $this->seoDescription,
'name' => $this->name,
'picture' => $this->picture,
'pictureUrl' => $this->pictureUrl,
'picture_alt' => $this->pictureAlt,
'picture_title' => $this->pictureTitle,
'slug' => $this->slug,
'product_category' => $this->productCategory->value,
'consomption_time' => $this->consomptionTime->value,
'inspiration' => $this->inspiration->value,
'season' => $this->season->value,
'description' => $this->description,
'details' => $this->details,
'ingredients_list' => $this->ingredientsList,
'preparation_steps' => $this->preparationSteps,
'tips_and_advices' => $this->tipsAndAdvices,
'products' => $this->products,
'created_at' => $this->createdAt?->toISOString(),
'updated_at' => $this->updatedAt?->toISOString(),
'deleted_at' => $this->deletedAt?->toISOString(),
];
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}