|
4 | 4 |
|
5 | 5 | namespace TypeLang\Mapper\Type\Builder; |
6 | 6 |
|
7 | | -use TypeLang\Mapper\Exception\Definition\Template\Hint\TemplateArgumentHintsNotSupportedException; |
8 | | -use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsException; |
9 | | -use TypeLang\Mapper\Exception\Definition\TypeNotFoundException; |
10 | 7 | use TypeLang\Mapper\Type\ArrayType; |
11 | | -use TypeLang\Mapper\Type\Parser\TypeParserInterface; |
12 | | -use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface; |
13 | | -use TypeLang\Parser\Node\Stmt\NamedTypeNode; |
14 | | -use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode; |
15 | | -use TypeLang\Parser\Node\Stmt\TypeStatement; |
| 8 | +use TypeLang\Mapper\Type\TypeInterface; |
16 | 9 |
|
17 | 10 | /** |
18 | 11 | * @template TKey of array-key = array-key |
19 | 12 | * @template TValue of mixed = mixed |
20 | | - * @template-extends NamedTypeBuilder<ArrayType<TKey, TValue>> |
| 13 | + * @template-extends MapTypeBuilder<TKey, TValue> |
21 | 14 | */ |
22 | | -class ArrayTypeBuilder extends NamedTypeBuilder |
| 15 | +class ArrayTypeBuilder extends MapTypeBuilder |
23 | 16 | { |
24 | | - /** |
25 | | - * @var non-empty-lowercase-string |
26 | | - */ |
27 | | - public const DEFAULT_INNER_KEY_TYPE = 'array-key'; |
28 | | - |
29 | | - /** |
30 | | - * @var non-empty-lowercase-string |
31 | | - */ |
32 | | - public const DEFAULT_INNER_VALUE_TYPE = 'mixed'; |
33 | | - |
34 | | - /** |
35 | | - * @param non-empty-array<non-empty-string>|non-empty-string $names |
36 | | - * @param non-empty-string $keyType |
37 | | - * @param non-empty-string $valueType |
38 | | - */ |
39 | | - public function __construct( |
40 | | - array|string $names, |
41 | | - protected readonly string $keyType = self::DEFAULT_INNER_KEY_TYPE, |
42 | | - protected readonly string $valueType = self::DEFAULT_INNER_VALUE_TYPE, |
43 | | - ) { |
44 | | - parent::__construct($names); |
45 | | - } |
46 | | - |
47 | | - public function build( |
48 | | - TypeStatement $statement, |
49 | | - TypeRepositoryInterface $types, |
50 | | - TypeParserInterface $parser, |
51 | | - ): ArrayType { |
52 | | - /** @phpstan-ignore-next-line : Additional DbC assertion */ |
53 | | - assert($statement instanceof NamedTypeNode); |
54 | | - |
55 | | - $this->expectNoShapeFields($statement); |
56 | | - |
57 | | - $arguments = $statement->arguments->items ?? []; |
58 | | - |
59 | | - /** @phpstan-ignore-next-line : It's too difficult for PHPStan to calculate the specified type */ |
60 | | - return match (\count($arguments)) { |
61 | | - 0 => $this->buildWithNoKeyValue($types, $parser), |
62 | | - 1 => $this->buildWithValue($statement, $types, $parser), |
63 | | - 2 => $this->buildWithKeyValue($statement, $types), |
64 | | - default => throw TooManyTemplateArgumentsException::becauseTemplateArgumentsRangeOverflows( |
65 | | - minSupportedArgumentsCount: 0, |
66 | | - maxSupportedArgumentsCount: 2, |
67 | | - type: $statement, |
68 | | - ), |
69 | | - }; |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * @return ArrayType<TKey, TValue> |
74 | | - * @throws TypeNotFoundException |
75 | | - * @throws \Throwable |
76 | | - */ |
77 | | - private function buildWithNoKeyValue(TypeRepositoryInterface $types, TypeParserInterface $parser): ArrayType |
78 | | - { |
79 | | - /** @phpstan-ignore-next-line : It's too difficult for PHPStan to calculate the specified type */ |
80 | | - return new ArrayType( |
81 | | - key: $types->getTypeByStatement( |
82 | | - statement: $parser->getStatementByDefinition( |
83 | | - definition: $this->keyType, |
84 | | - ), |
85 | | - ), |
86 | | - value: $types->getTypeByStatement( |
87 | | - statement: $parser->getStatementByDefinition( |
88 | | - definition: $this->valueType, |
89 | | - ), |
90 | | - ), |
91 | | - ); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * @return ArrayType<TKey, TValue> |
96 | | - * @throws TemplateArgumentHintsNotSupportedException |
97 | | - * @throws TypeNotFoundException |
98 | | - * @throws \Throwable |
99 | | - */ |
100 | | - private function buildWithKeyValue(NamedTypeNode $statement, TypeRepositoryInterface $types): ArrayType |
| 17 | + protected function create(TypeInterface $key, TypeInterface $value): ArrayType |
101 | 18 | { |
102 | | - $arguments = $statement->arguments->items ?? []; |
103 | | - |
104 | | - assert(\array_key_exists(0, $arguments)); |
105 | | - assert(\array_key_exists(1, $arguments)); |
106 | | - |
107 | | - /** @var TemplateArgumentNode $key */ |
108 | | - $key = $arguments[0]; |
109 | | - $this->expectNoTemplateArgumentHint($statement, $key); |
110 | | - |
111 | | - /** @var TemplateArgumentNode $value */ |
112 | | - $value = $arguments[1]; |
113 | | - $this->expectNoTemplateArgumentHint($statement, $value); |
114 | | - |
115 | | - /** @phpstan-ignore-next-line : It's too difficult for PHPStan to calculate the specified type */ |
116 | | - return new ArrayType( |
117 | | - key: $types->getTypeByStatement($key->value), |
118 | | - value: $types->getTypeByStatement($value->value), |
119 | | - ); |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * @return ArrayType<TKey, TValue> |
124 | | - * @throws TemplateArgumentHintsNotSupportedException |
125 | | - * @throws TypeNotFoundException |
126 | | - * @throws \Throwable |
127 | | - */ |
128 | | - private function buildWithValue( |
129 | | - NamedTypeNode $statement, |
130 | | - TypeRepositoryInterface $types, |
131 | | - TypeParserInterface $parser, |
132 | | - ): ArrayType { |
133 | | - $arguments = $statement->arguments->items ?? []; |
134 | | - |
135 | | - assert(\array_key_exists(0, $arguments)); |
136 | | - |
137 | | - /** @var TemplateArgumentNode $value */ |
138 | | - $value = $arguments[0]; |
139 | | - |
140 | | - $this->expectNoTemplateArgumentHint($statement, $value); |
141 | | - |
142 | | - /** @phpstan-ignore-next-line : It's too difficult for PHPStan to calculate the specified type */ |
143 | | - return new ArrayType( |
144 | | - key: $types->getTypeByStatement( |
145 | | - statement: $parser->getStatementByDefinition($this->keyType), |
146 | | - ), |
147 | | - value: $types->getTypeByStatement($value->value), |
148 | | - ); |
| 19 | + return new ArrayType($key, $value); |
149 | 20 | } |
150 | 21 | } |
0 commit comments