3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Mapper\Tree\Exception;
7 use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
8 use CuyZ\Valinor\Mapper\Tree\Message\HasParameters;
9 use CuyZ\Valinor\Type\Types\UnionType;
10 use CuyZ\Valinor\Utility\String\StringFormatter;
11 use CuyZ\Valinor\Utility\TypeHelper;
14 use function array_map;
18 final class CannotResolveTypeFromUnion extends RuntimeException implements ErrorMessage, HasParameters
22 /** @var array<string, string> */
23 private array $parameters;
25 public function __construct(mixed $source, UnionType $unionType)
28 'allowed_types' => implode(
30 // PHP8.1 First-class callable syntax
31 array_map([TypeHelper::class, 'dump'], $unionType->types())
35 if ($source === null) {
36 $this->body = TypeHelper::containsObject($unionType)
38 : 'Cannot be empty and must be filled with a value matching any of {allowed_types}.';
40 $this->body = TypeHelper::containsObject($unionType)
41 ? 'Invalid value {source_value}.'
42 : 'Value {source_value} does not match any of {allowed_types}.';
45 parent::__construct(StringFormatter::for($this), 1607027306);
48 public function body(): string
53 public function parameters(): array
55 return $this->parameters;