- 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.
173 lines
7.2 KiB
PHP
173 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Dto;
|
|
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Models\Product;
|
|
use Carbon\CarbonInterface;
|
|
use Domain\Models\Entities\Product as DomainProduct;
|
|
use Domain\Models\ValueObjects\Products\NutritionalValues;
|
|
use Domain\Models\ValueObjects\Products\ProductCategory;
|
|
use Domain\Models\ValueObjects\Products\Technology;
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
/**
|
|
* @implements Arrayable<string, mixed>
|
|
*/
|
|
final readonly class ProductDto implements \JsonSerializable, Arrayable
|
|
{
|
|
/**
|
|
* @param \Domain\Models\ValueObjects\Products\BreedingMethod[] $breedingMethods
|
|
* @param \Domain\Models\ValueObjects\Products\Business[] $businesses
|
|
* @param \Domain\Models\ValueObjects\Products\ProductFormat[] $formats
|
|
*/
|
|
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 $category,
|
|
public Technology $technology,
|
|
public bool $isEgalim,
|
|
public array $breedingMethods,
|
|
public array $businesses,
|
|
public array $formats,
|
|
public string $description,
|
|
public string $detailsAndAdvantages,
|
|
public string $handlingAdvices,
|
|
public ?string $ingredients,
|
|
public string $conservationBeforeOpening,
|
|
public ?string $conservationAfterOpening,
|
|
public ?NutritionalValues $nutritionalValues,
|
|
public ?string $pdf,
|
|
public ?string $pdfUrl,
|
|
public ?CarbonInterface $createdAt,
|
|
public ?CarbonInterface $updatedAt,
|
|
public ?CarbonInterface $deletedAt,
|
|
) {
|
|
}
|
|
|
|
public static function fromModel(Product $product, PublicS3Disk $publicS3Disk): self
|
|
{
|
|
|
|
$picturePath = $product->picture?->value;
|
|
$pdfPath = $product->pdf?->value;
|
|
|
|
return new self(
|
|
id: (string) $product->id,
|
|
seoTitle: (string) $product->seo_title,
|
|
seoDescription: (string) $product->seo_description,
|
|
name: (string) $product->name,
|
|
picture: $picturePath,
|
|
pictureUrl: $picturePath !== null ? $publicS3Disk->publicUrlOf($picturePath) : null,
|
|
pictureAlt: (string) $product->picture_alt,
|
|
pictureTitle: (string) $product->picture_title,
|
|
slug: (string) $product->slug,
|
|
category: $product->category,
|
|
technology: $product->technology,
|
|
isEgalim: (bool) $product->is_egalim,
|
|
breedingMethods: $product->breeding_methods->values()->all(),
|
|
businesses: $product->businesses->values()->all(),
|
|
formats: $product->formats->values()->all(),
|
|
description: (string) $product->description,
|
|
detailsAndAdvantages: (string) $product->details_and_advantages,
|
|
handlingAdvices: (string) $product->handling_advices,
|
|
ingredients: $product->ingredients,
|
|
conservationBeforeOpening: (string) $product->conservation_before_opening,
|
|
conservationAfterOpening: $product->conservation_after_opening,
|
|
nutritionalValues: $product->nutritional_values,
|
|
pdf: $pdfPath,
|
|
pdfUrl: $pdfPath !== null ? $publicS3Disk->publicUrlOf($pdfPath) : null,
|
|
createdAt: $product->created_at,
|
|
updatedAt: $product->updated_at,
|
|
deletedAt: $product->deleted_at,
|
|
);
|
|
}
|
|
|
|
public static function fromEntity(
|
|
DomainProduct $product,
|
|
PublicS3Disk $publicS3Disk
|
|
): self {
|
|
|
|
return new self(
|
|
id: (string) $product->id->value,
|
|
seoTitle: (string) ($product->seoTitle ?? ''),
|
|
seoDescription: (string) ($product->seoDescription ?? ''),
|
|
name: $product->name,
|
|
picture: $product->picture,
|
|
pictureUrl: $product->picture !== null ? $publicS3Disk->publicUrlOf($product->picture) : null,
|
|
pictureAlt: (string) ($product->pictureAlt ?? ''),
|
|
pictureTitle: (string) ($product->pictureTitle ?? ''),
|
|
slug: $product->slug,
|
|
category: $product->category,
|
|
technology: $product->technology,
|
|
isEgalim: $product->isEgalim,
|
|
breedingMethods: $product->breedingMethods,
|
|
businesses: $product->businesses,
|
|
formats: $product->formats,
|
|
description: $product->description,
|
|
detailsAndAdvantages: $product->detailsAndAdvantages,
|
|
handlingAdvices: $product->handlingAdvices,
|
|
ingredients: $product->ingredients,
|
|
conservationBeforeOpening: $product->conservationBeforeOpening,
|
|
conservationAfterOpening: $product->conservationAfterOpening,
|
|
nutritionalValues: $product->nutritionalValues,
|
|
pdf: $product->pdfFilePath,
|
|
pdfUrl: $product->pdfFilePath !== null ? $publicS3Disk->publicUrlOf($product->pdfFilePath) : null,
|
|
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,
|
|
'category' => $this->category->value,
|
|
'technology' => $this->technology->value,
|
|
'is_egalim' => $this->isEgalim,
|
|
'breeding_methods' => $this->breedingMethods,
|
|
'businesses' => $this->businesses,
|
|
'formats' => $this->formats,
|
|
'description' => $this->description,
|
|
'details_and_advantages' => $this->detailsAndAdvantages,
|
|
'handling_advices' => $this->handlingAdvices,
|
|
'ingredients' => $this->ingredients,
|
|
'conservation_before_opening' => $this->conservationBeforeOpening,
|
|
'conservation_after_opening' => $this->conservationAfterOpening,
|
|
'nutritional_values' => $this->nutritionalValues?->toArray(),
|
|
'pdf' => $this->pdf,
|
|
'pdfUrl' => $this->pdfUrl,
|
|
'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();
|
|
}
|
|
}
|