From 37ecd9faa15f39682b63132c833b262efd021a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 14 Aug 2026 12:07:04 +0200 Subject: [PATCH] feat(dto): add DTOs for IncoSheet components - Introduced AdditiveDto, AllegationsDto, AllergenDto, BrandDto, NutriscoreDto, PortionsDto, StorageConditionDto, and WeightDto. - These DTOs facilitate structured data handling for IncoSheet components. --- .../Api/V1/Dto/IncoSheets/IncoSheetDto.php | 129 ++++++++---------- .../V1/Dto/IncoSheets/Parts/AdditiveDto.php | 33 +++++ .../Dto/IncoSheets/Parts/AllegationsDto.php | 43 ++++++ .../V1/Dto/IncoSheets/Parts/AllergenDto.php | 33 +++++ .../Api/V1/Dto/IncoSheets/Parts/BrandDto.php | 33 +++++ .../V1/Dto/IncoSheets/Parts/NutriscoreDto.php | 31 +++++ .../V1/Dto/IncoSheets/Parts/PortionsDto.php | 35 +++++ .../IncoSheets/Parts/StorageConditionDto.php | 31 +++++ .../Api/V1/Dto/IncoSheets/Parts/WeightDto.php | 35 +++++ 9 files changed, 330 insertions(+), 73 deletions(-) create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/AdditiveDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllegationsDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllergenDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/BrandDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/NutriscoreDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/PortionsDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/StorageConditionDto.php create mode 100644 src/Presentation/Api/V1/Dto/IncoSheets/Parts/WeightDto.php diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/IncoSheetDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/IncoSheetDto.php index 275d6a1..0adcdc2 100644 --- a/src/Presentation/Api/V1/Dto/IncoSheets/IncoSheetDto.php +++ b/src/Presentation/Api/V1/Dto/IncoSheets/IncoSheetDto.php @@ -9,38 +9,39 @@ use Application\Usecase\IncoSheets\FindAll\PaginatedIncoSheet; use Domain\Models\ValueObjects\IncoSheets\Additive; use Domain\Models\ValueObjects\IncoSheets\Allergen; use Presentation\Api\V1\Contracts\ApiResponseDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\AdditiveDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\AllegationsDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\AllergenDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\BrandDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\NutriscoreDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\PortionsDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\StorageConditionDto; +use Presentation\Api\V1\Dto\IncoSheets\Parts\WeightDto; final readonly class IncoSheetDto implements ApiResponseDto { /** - * @param array{allegations: array, health: array, nutrition: array, other: array} $allegations - * @param object $ingredients - * @param array{lowPart: int, highPart: int, approximative: bool}|null $portions - * @param array $allergens - * @param array{code: string, label: string} $brand - * @param array{netWeight: int, grossWeight: int, theoreticalAverage: int}|null $weight - * @param array{grade: string}|null $nutriscore - * @param array{instructions: string}|null $storageCondition - * @param array $additives + * @param array $allergens + * @param array $additives */ public function __construct( public string $id, public string $name, public string $label, public string $description, - public array $allegations, + public AllegationsDto $allegations, public string $sellArguments, public string $preparationAdvices, public ?string $tips, public ?string $sellFamilly, public object $ingredients, - public ?array $portions, + public ?PortionsDto $portions, public array $allergens, - public array $brand, + public BrandDto $brand, public bool $isSeasonal, - public ?array $weight, - public ?array $nutriscore, - public ?array $storageCondition, + public ?WeightDto $weight, + public ?NutriscoreDto $nutriscore, + public ?StorageConditionDto $storageCondition, public array $additives, public ?string $compo, public ?string $createdAt, @@ -58,44 +59,24 @@ final readonly class IncoSheetDto implements ApiResponseDto name: $output->name, label: $output->label, description: $output->description, - allegations: [ - 'allegations' => array_values($output->allegations->allegations()), - 'health' => array_values($output->allegations->health()), - 'nutrition' => array_values($output->allegations->nutrition()), - 'other' => array_values($output->allegations->other()), - ], + allegations: AllegationsDto::fromDomain($output->allegations), sellArguments: $output->sellArguments, preparationAdvices: $output->preparationAdvices, tips: $output->tips, sellFamilly: $output->sellFamilly, ingredients: $ingObject, - portions: $output->portions !== null ? [ - 'lowPart' => $output->portions->lowPart(), - 'highPart' => $output->portions->highPart(), - 'approximative' => $output->portions->approximative(), - ] : null, + portions: $output->portions !== null ? PortionsDto::fromDomain($output->portions) : null, allergens: array_values(array_map( - static fn (Allergen $a): array => ['code' => $a->code, 'label' => $a->label], + static fn (Allergen $a): AllergenDto => AllergenDto::fromDomain($a), $output->allergens )), - brand: [ - 'code' => $output->brand->code, - 'label' => $output->brand->label, - ], + brand: BrandDto::fromDomain($output->brand), isSeasonal: $output->isSeasonal, - weight: $output->weight !== null ? [ - 'netWeight' => $output->weight->netWeight, - 'grossWeight' => $output->weight->grossWeight, - 'theoreticalAverage' => $output->weight->theoreticalAverage, - ] : null, - nutriscore: $output->nutriscore !== null ? [ - 'grade' => $output->nutriscore->grade, - ] : null, - storageCondition: $output->storageCondition !== null ? [ - 'instructions' => $output->storageCondition->instructions, - ] : null, + weight: $output->weight !== null ? WeightDto::fromDomain($output->weight) : null, + nutriscore: $output->nutriscore !== null ? NutriscoreDto::fromDomain($output->nutriscore) : null, + storageCondition: $output->storageCondition !== null ? StorageConditionDto::fromDomain($output->storageCondition) : null, additives: array_values(array_map( - static fn (Additive $a): array => ['code' => $a->code, 'label' => $a->label], + static fn (Additive $a): AdditiveDto => AdditiveDto::fromDomain($a), $output->additives )), compo: $output->compo !== null ? (string) $output->compo : null, @@ -114,44 +95,24 @@ final readonly class IncoSheetDto implements ApiResponseDto name: $sheet->name, label: $sheet->label, description: $sheet->description, - allegations: [ - 'allegations' => array_values($sheet->allegations->allegations()), - 'health' => array_values($sheet->allegations->health()), - 'nutrition' => array_values($sheet->allegations->nutrition()), - 'other' => array_values($sheet->allegations->other()), - ], + allegations: AllegationsDto::fromDomain($sheet->allegations), sellArguments: $sheet->sellArguments, preparationAdvices: $sheet->preparationAdvices, tips: $sheet->tips, sellFamilly: $sheet->sellFamilly, ingredients: $ingObject, - portions: $sheet->portions !== null ? [ - 'lowPart' => $sheet->portions->lowPart(), - 'highPart' => $sheet->portions->highPart(), - 'approximative' => $sheet->portions->approximative(), - ] : null, + portions: $sheet->portions !== null ? PortionsDto::fromDomain($sheet->portions) : null, allergens: array_values(array_map( - static fn (Allergen $a): array => ['code' => $a->code, 'label' => $a->label], + static fn (Allergen $a): AllergenDto => AllergenDto::fromDomain($a), $sheet->allergens )), - brand: [ - 'code' => $sheet->brand->code, - 'label' => $sheet->brand->label, - ], + brand: BrandDto::fromDomain($sheet->brand), isSeasonal: $sheet->isSeasonal, - weight: $sheet->weight !== null ? [ - 'netWeight' => $sheet->weight->netWeight, - 'grossWeight' => $sheet->weight->grossWeight, - 'theoreticalAverage' => $sheet->weight->theoreticalAverage, - ] : null, - nutriscore: $sheet->nutriscore !== null ? [ - 'grade' => $sheet->nutriscore->grade, - ] : null, - storageCondition: $sheet->storageCondition !== null ? [ - 'instructions' => $sheet->storageCondition->instructions, - ] : null, + weight: $sheet->weight !== null ? WeightDto::fromDomain($sheet->weight) : null, + nutriscore: $sheet->nutriscore !== null ? NutriscoreDto::fromDomain($sheet->nutriscore) : null, + storageCondition: $sheet->storageCondition !== null ? StorageConditionDto::fromDomain($sheet->storageCondition) : null, additives: array_values(array_map( - static fn (Additive $a): array => ['code' => $a->code, 'label' => $a->label], + static fn (Additive $a): AdditiveDto => AdditiveDto::fromDomain($a), $sheet->additives )), compo: $sheet->compo !== null ? (string) $sheet->compo : null, @@ -164,7 +125,29 @@ final readonly class IncoSheetDto implements ApiResponseDto #[\Override] public function toArray(): array { - /** @var array */ - return get_object_vars($this); + /** @var array $vars */ + $vars = get_object_vars($this); + + // Map over allergens and additives to call their toArray methods + $vars['allergens'] = array_map(static fn (AllergenDto $dto): array => $dto->toArray(), $this->allergens); + $vars['additives'] = array_map(static fn (AdditiveDto $dto): array => $dto->toArray(), $this->additives); + + // Call toArray on the nested object DTOs + $vars['allegations'] = $this->allegations->toArray(); + if ($this->portions !== null) { + $vars['portions'] = $this->portions->toArray(); + } + $vars['brand'] = $this->brand->toArray(); + if ($this->weight !== null) { + $vars['weight'] = $this->weight->toArray(); + } + if ($this->nutriscore !== null) { + $vars['nutriscore'] = $this->nutriscore->toArray(); + } + if ($this->storageCondition !== null) { + $vars['storageCondition'] = $this->storageCondition->toArray(); + } + + return $vars; } } diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AdditiveDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AdditiveDto.php new file mode 100644 index 0000000..7201d7c --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AdditiveDto.php @@ -0,0 +1,33 @@ +code, + label: $additive->label, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllegationsDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllegationsDto.php new file mode 100644 index 0000000..ef2f8c5 --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllegationsDto.php @@ -0,0 +1,43 @@ + $allegations + * @param array $health + * @param array $nutrition + * @param array $other + */ + public function __construct( + public array $allegations, + public array $health, + public array $nutrition, + public array $other, + ) { + } + + public static function fromDomain(Allegations $allegations): self + { + return new self( + allegations: array_values($allegations->allegations()), + health: array_values($allegations->health()), + nutrition: array_values($allegations->nutrition()), + other: array_values($allegations->other()), + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllergenDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllergenDto.php new file mode 100644 index 0000000..3ff20b6 --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/AllergenDto.php @@ -0,0 +1,33 @@ +code, + label: $allergen->label, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/BrandDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/BrandDto.php new file mode 100644 index 0000000..e6349d1 --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/BrandDto.php @@ -0,0 +1,33 @@ +code, + label: $brand->label, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/NutriscoreDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/NutriscoreDto.php new file mode 100644 index 0000000..00af9f2 --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/NutriscoreDto.php @@ -0,0 +1,31 @@ +grade, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/PortionsDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/PortionsDto.php new file mode 100644 index 0000000..5ca1e9a --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/PortionsDto.php @@ -0,0 +1,35 @@ +lowPart(), + highPart: $portions->highPart(), + approximative: $portions->approximative(), + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/StorageConditionDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/StorageConditionDto.php new file mode 100644 index 0000000..f530ae4 --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/StorageConditionDto.php @@ -0,0 +1,31 @@ +instructions, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +} diff --git a/src/Presentation/Api/V1/Dto/IncoSheets/Parts/WeightDto.php b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/WeightDto.php new file mode 100644 index 0000000..5da059c --- /dev/null +++ b/src/Presentation/Api/V1/Dto/IncoSheets/Parts/WeightDto.php @@ -0,0 +1,35 @@ +netWeight, + grossWeight: $weight->grossWeight, + theoreticalAverage: $weight->theoreticalAverage, + ); + } + + #[\Override] + public function toArray(): array + { + /** @var array $vars */ + $vars = get_object_vars($this); + return $vars; + } +}