ldc-arcadia-api/src/Domain/Models/ValueObjects/Inco/Ingredients.php

53 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\Inco;
/**
* Represents a structured and parsed ingredient list.
*
* Each entry is either:
* - an indexed group (int key) -> list of ingredient strings without a section label
* - a labelled group (string key) -> list of ingredient strings under a section label
* (e.g. "Ingrédients", "Traces possibles")
*
* @phpstan-type IngredientGroup array<int|string,string[]>
*/
final readonly class Ingredients
{
/**
* @param array<int|string,string[]> $groups
*/
public function __construct(
public readonly array $groups
) {
foreach ($groups as $key => $lines) {
if (! is_array($lines)) {
throw new \DomainException(
"Invalid ingredient group at key '{$key}': value must be an array of strings"
);
}
foreach ($lines as $i => $line) {
if (! is_string($line)) {
throw new \DomainException(
"Invalid ingredient at group '{$key}', index '{$i}': value must be a string"
);
}
if (empty(trim($line))) {
throw new \DomainException(
"Invalid ingredient at group '{$key}', index '{$i}': string should not be empty"
);
}
}
}
}
public function jsonSerialize(): mixed
{
return $this->groups;
}
}