|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypeLang\Mapper\Type\Builder; |
| 6 | + |
| 7 | +use TypeLang\Mapper\Exception\Definition\Shape\ShapeFieldsNotSupportedException; |
| 8 | +use TypeLang\Mapper\Exception\Definition\Template\Hint\TemplateArgumentHintsNotSupportedException; |
| 9 | +use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsException; |
| 10 | +use TypeLang\Mapper\Exception\Definition\TypeNotFoundException; |
| 11 | +use TypeLang\Mapper\Runtime\Parser\TypeParserInterface; |
| 12 | +use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface; |
| 13 | +use TypeLang\Mapper\Type\ListType; |
| 14 | +use TypeLang\Parser\Node\Stmt\NamedTypeNode; |
| 15 | +use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode; |
| 16 | +use TypeLang\Parser\Node\Stmt\TypeStatement; |
| 17 | + |
| 18 | +/** |
| 19 | + * @template-extends NamedTypeBuilder<ListType> |
| 20 | + */ |
| 21 | +class ListTypeBuilder extends NamedTypeBuilder |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var non-empty-lowercase-string |
| 25 | + */ |
| 26 | + public const DEFAULT_INNER_VALUE_TYPE = 'mixed'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param non-empty-array<non-empty-string>|non-empty-string $names |
| 30 | + * @param non-empty-string $valueType |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + array|string $names, |
| 34 | + protected readonly string $valueType = self::DEFAULT_INNER_VALUE_TYPE, |
| 35 | + ) { |
| 36 | + parent::__construct($names); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @throws ShapeFieldsNotSupportedException |
| 41 | + * @throws TemplateArgumentHintsNotSupportedException |
| 42 | + * @throws TooManyTemplateArgumentsException |
| 43 | + * @throws TypeNotFoundException |
| 44 | + * @throws \Throwable |
| 45 | + */ |
| 46 | + public function build( |
| 47 | + TypeStatement $statement, |
| 48 | + TypeRepositoryInterface $types, |
| 49 | + TypeParserInterface $parser, |
| 50 | + ): ListType { |
| 51 | + $this->expectNoShapeFields($statement); |
| 52 | + |
| 53 | + $arguments = $statement->arguments->items ?? []; |
| 54 | + |
| 55 | + return match (\count($arguments)) { |
| 56 | + 0 => $this->buildWithNoValue($types, $parser), |
| 57 | + 1 => $this->buildWithValue($statement, $types), |
| 58 | + default => throw TooManyTemplateArgumentsException::becauseTemplateArgumentsRangeOverflows( |
| 59 | + passedArgumentsCount: \count($arguments), |
| 60 | + minSupportedArgumentsCount: 0, |
| 61 | + maxSupportedArgumentsCount: 1, |
| 62 | + type: $statement, |
| 63 | + ), |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @throws TypeNotFoundException |
| 69 | + * @throws \Throwable |
| 70 | + */ |
| 71 | + private function buildWithNoValue(TypeRepositoryInterface $types, TypeParserInterface $parser): ListType |
| 72 | + { |
| 73 | + return new ListType( |
| 74 | + value: $types->getTypeByStatement( |
| 75 | + statement: $parser->getStatementByDefinition( |
| 76 | + definition: $this->valueType, |
| 77 | + ), |
| 78 | + ), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @throws TemplateArgumentHintsNotSupportedException |
| 84 | + * @throws TypeNotFoundException |
| 85 | + * @throws \Throwable |
| 86 | + */ |
| 87 | + private function buildWithValue( |
| 88 | + NamedTypeNode $statement, |
| 89 | + TypeRepositoryInterface $types, |
| 90 | + ): ListType { |
| 91 | + $arguments = $statement->arguments->items ?? []; |
| 92 | + |
| 93 | + assert(\array_key_exists(0, $arguments)); |
| 94 | + |
| 95 | + /** @var TemplateArgumentNode $value */ |
| 96 | + $value = $arguments[0]; |
| 97 | + |
| 98 | + $this->expectNoTemplateArgumentHint($statement, $value); |
| 99 | + |
| 100 | + return new ListType( |
| 101 | + value: $types->getTypeByStatement( |
| 102 | + statement: $value->value, |
| 103 | + ), |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments