Updated ProductPolicy and RecipePolicy to accept ProductDto in addition to Product. This change enhances the flexibility of the policies by allowing DTOs to be used for authorization checks. refactor(provider): simplify service provider bindings Refactored AppServiceProvider to simplify the binding of ProductDirectory and RecipeDirectory. Removed unnecessary dependencies and improved clarity. feat(route): update route bindings for front-end Changed route bindings for products and recipes to use frontProduct and frontRecipe. This improves clarity and consistency in route handling. fix(breadcrumbs): update breadcrumb checks for DTOs Updated breadcrumb generation to check for ProductDto and RecipeDto instead of their model counterparts. This ensures breadcrumbs function correctly with the new DTO structure. refactor(types): remove FilePath type and update schemas Removed the FilePath type definition and updated related schemas to use string directly. This simplifies type handling for file paths. style(entities): standardize naming conventions Updated naming conventions in Product and Recipe entities to use snake_case for consistency across the codebase. feat(frontend): update product and recipe pages for new DTO structure Modified front-end components to accommodate changes in the product and recipe data structures, ensuring compatibility with the new DTOs.
237 lines
8.9 KiB
PHP
237 lines
8.9 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\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\Products\NutritionalValues;
|
|
use Domain\Models\ValueObjects\Products\ProductCategory;
|
|
use Domain\Models\ValueObjects\Products\Technology;
|
|
use Illuminate\Contracts\Routing\UrlRoutable;
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
/**
|
|
* @implements Arrayable<string, mixed>
|
|
*/
|
|
final readonly class ProductDto implements \JsonSerializable, Arrayable, UrlRoutable
|
|
{
|
|
/**
|
|
* @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 {
|
|
$picturePath = $product->picture;
|
|
$pdfPath = $product->pdfFilePath;
|
|
|
|
return new self(
|
|
id: (string) $product->id->value,
|
|
seoTitle: (string) ($product->seoTitle ?? ''),
|
|
seoDescription: (string) ($product->seoDescription ?? ''),
|
|
name: $product->name,
|
|
picture: $picturePath,
|
|
pictureUrl: $picturePath !== null ? $publicS3Disk->publicUrlOf($picturePath) : 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: $pdfPath,
|
|
pdfUrl: $pdfPath !== null ? $publicS3Disk->publicUrlOf($pdfPath) : null,
|
|
createdAt: null,
|
|
updatedAt: null,
|
|
deletedAt: null,
|
|
);
|
|
}
|
|
|
|
public static function dummyForZiggy(): self
|
|
{
|
|
return new self(
|
|
id: 'dummy-id',
|
|
seoTitle: 'dummy-seo-title',
|
|
seoDescription: 'dummy-seo-description',
|
|
name: 'dummy-name',
|
|
picture: null,
|
|
pictureUrl: null,
|
|
pictureAlt: 'dummy-picture-alt',
|
|
pictureTitle: 'dummy-picture-title',
|
|
slug: 'dummy-slug',
|
|
category: ProductCategory::OMELETS_AND_DELI_AND_DESSERT_LINES,
|
|
technology: Technology::FRESH,
|
|
isEgalim: false,
|
|
breedingMethods: [],
|
|
businesses: [],
|
|
formats: [],
|
|
description: 'dummy-description',
|
|
detailsAndAdvantages: 'dummy-details-and-advantages',
|
|
handlingAdvices: 'dummy-handling-advices',
|
|
ingredients: 'dummy-ingredients',
|
|
conservationBeforeOpening: 'dummy-conservation-before-opening',
|
|
conservationAfterOpening: 'dummy-conservation-after-opening',
|
|
nutritionalValues: null,
|
|
pdf: null,
|
|
pdfUrl: null,
|
|
createdAt: null,
|
|
updatedAt: null,
|
|
deletedAt: null,
|
|
);
|
|
}
|
|
|
|
public function getId(): ProductId
|
|
{
|
|
return new ProductId(Uuid::fromString($this->id));
|
|
}
|
|
|
|
#[\Override]
|
|
public function getRouteKey(): mixed
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
#[\Override]
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'id';
|
|
}
|
|
|
|
#[\Override]
|
|
public function resolveRouteBinding($value, $field = null): mixed
|
|
{
|
|
return null;
|
|
}
|
|
|
|
#[\Override]
|
|
public function resolveChildRouteBinding($childType, $value, $field): mixed
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'slug' => $this->slug,
|
|
'seo_title' => $this->seoTitle,
|
|
'seo_description' => $this->seoDescription,
|
|
'picture' => $this->pictureUrl,
|
|
'picture_alt' => $this->pictureAlt,
|
|
'picture_title' => $this->pictureTitle,
|
|
'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->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();
|
|
}
|
|
}
|