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.
This commit is contained in:
Jérémy Munsch 2026-08-14 12:07:04 +02:00
parent 70375f8139
commit 37ecd9faa1
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
9 changed files with 330 additions and 73 deletions

View file

@ -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<int, string>, health: array<int, string>, nutrition: array<int, string>, other: array<int, string>} $allegations
* @param object $ingredients
* @param array{lowPart: int, highPart: int, approximative: bool}|null $portions
* @param array<int, array{code: int, label: string}> $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<int, array{code: string, label: string}> $additives
* @param array<int, AllergenDto> $allergens
* @param array<int, AdditiveDto> $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<string, mixed> */
return get_object_vars($this);
/** @var array<string, mixed> $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;
}
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\Additive;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class AdditiveDto implements ApiResponseDto
{
public function __construct(
public string $code,
public string $label,
) {
}
public static function fromDomain(Additive $additive): self
{
return new self(
code: $additive->code,
label: $additive->label,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\Allegations;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class AllegationsDto implements ApiResponseDto
{
/**
* @param array<int, string> $allegations
* @param array<int, string> $health
* @param array<int, string> $nutrition
* @param array<int, string> $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<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\Allergen;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class AllergenDto implements ApiResponseDto
{
public function __construct(
public int $code,
public string $label,
) {
}
public static function fromDomain(Allergen $allergen): self
{
return new self(
code: $allergen->code,
label: $allergen->label,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\Brand;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class BrandDto implements ApiResponseDto
{
public function __construct(
public string $code,
public string $label,
) {
}
public static function fromDomain(Brand $brand): self
{
return new self(
code: $brand->code,
label: $brand->label,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\Nutriscore;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class NutriscoreDto implements ApiResponseDto
{
public function __construct(
public string $grade,
) {
}
public static function fromDomain(Nutriscore $nutriscore): self
{
return new self(
grade: $nutriscore->grade,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\PortionAmount;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class PortionsDto implements ApiResponseDto
{
public function __construct(
public int $lowPart,
public int $highPart,
public bool $approximative,
) {
}
public static function fromDomain(PortionAmount $portions): self
{
return new self(
lowPart: $portions->lowPart(),
highPart: $portions->highPart(),
approximative: $portions->approximative(),
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\StorageCondition;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class StorageConditionDto implements ApiResponseDto
{
public function __construct(
public string $instructions,
) {
}
public static function fromDomain(StorageCondition $storageCondition): self
{
return new self(
instructions: $storageCondition->instructions,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Presentation\Api\V1\Dto\IncoSheets\Parts;
use Domain\Models\ValueObjects\IncoSheets\IncoWeight;
use Presentation\Api\V1\Contracts\ApiResponseDto;
final readonly class WeightDto implements ApiResponseDto
{
public function __construct(
public int $netWeight,
public int $grossWeight,
public int $theoreticalAverage,
) {
}
public static function fromDomain(IncoWeight $weight): self
{
return new self(
netWeight: $weight->netWeight,
grossWeight: $weight->grossWeight,
theoreticalAverage: $weight->theoreticalAverage,
);
}
#[\Override]
public function toArray(): array
{
/** @var array<string, mixed> $vars */
$vars = get_object_vars($this);
return $vars;
}
}