21
21
class ArrayTypeBuilder extends NamedTypeBuilder
22
22
{
23
23
/**
24
+ * @var non-empty-lowercase-string
25
+ */
26
+ public const DEFAULT_INNER_KEY_TYPE = 'array-key ' ;
27
+
28
+ /**
29
+ * @var non-empty-lowercase-string
30
+ */
31
+ public const DEFAULT_INNER_VALUE_TYPE = 'mixed ' ;
32
+
33
+ /**
34
+ * @param non-empty-array<non-empty-string>|non-empty-string $names
35
+ * @param non-empty-string $keyType
36
+ * @param non-empty-string $valueType
37
+ */
38
+ public function __construct (
39
+ array |string $ names ,
40
+ protected readonly string $ keyType = self ::DEFAULT_INNER_KEY_TYPE ,
41
+ protected readonly string $ valueType = self ::DEFAULT_INNER_VALUE_TYPE ,
42
+ ) {
43
+ parent ::__construct ($ names );
44
+ }
45
+
46
+ /**
47
+ * @throws ShapeFieldsNotSupportedException
24
48
* @throws TemplateArgumentHintsNotSupportedException
25
49
* @throws TooManyTemplateArgumentsException
26
50
* @throws TypeNotFoundException
27
- * @throws ShapeFieldsNotSupportedException
51
+ * @throws \Throwable
28
52
*/
29
53
public function build (
30
54
TypeStatement $ statement ,
@@ -36,9 +60,9 @@ public function build(
36
60
$ arguments = $ statement ->arguments ->items ?? [];
37
61
38
62
return match (\count ($ arguments )) {
39
- 0 => new ArrayType ( ),
40
- 1 => $ this ->buildByValue ($ statement , $ types ),
41
- 2 => $ this ->buildByKeyValue ($ statement , $ types ),
63
+ 0 => $ this -> buildWithNoKeyValue ( $ types , $ parser ),
64
+ 1 => $ this ->buildWithValue ($ statement , $ types, $ parser ),
65
+ 2 => $ this ->buildWithKeyValue ($ statement , $ types ),
42
66
default => throw TooManyTemplateArgumentsException::becauseTemplateArgumentsRangeOverflows (
43
67
passedArgumentsCount: \count ($ arguments ),
44
68
minSupportedArgumentsCount: 0 ,
@@ -48,12 +72,32 @@ public function build(
48
72
};
49
73
}
50
74
75
+ /**
76
+ * @throws TypeNotFoundException
77
+ * @throws \Throwable
78
+ */
79
+ private function buildWithNoKeyValue (TypeRepositoryInterface $ types , TypeParserInterface $ parser ): ArrayType
80
+ {
81
+ return new ArrayType (
82
+ key: $ types ->getTypeByStatement (
83
+ statement: $ parser ->getStatementByDefinition (
84
+ definition: $ this ->keyType ,
85
+ ),
86
+ ),
87
+ value: $ types ->getTypeByStatement (
88
+ statement: $ parser ->getStatementByDefinition (
89
+ definition: $ this ->valueType ,
90
+ ),
91
+ ),
92
+ );
93
+ }
94
+
51
95
/**
52
96
* @throws TemplateArgumentHintsNotSupportedException
53
97
* @throws TypeNotFoundException
54
98
* @throws \Throwable
55
99
*/
56
- private function buildByKeyValue (NamedTypeNode $ statement , TypeRepositoryInterface $ types ): ArrayType
100
+ private function buildWithKeyValue (NamedTypeNode $ statement , TypeRepositoryInterface $ types ): ArrayType
57
101
{
58
102
$ arguments = $ statement ->arguments ->items ?? [];
59
103
@@ -79,8 +123,11 @@ private function buildByKeyValue(NamedTypeNode $statement, TypeRepositoryInterfa
79
123
* @throws TypeNotFoundException
80
124
* @throws \Throwable
81
125
*/
82
- private function buildByValue (NamedTypeNode $ statement , TypeRepositoryInterface $ types ): ArrayType
83
- {
126
+ private function buildWithValue (
127
+ NamedTypeNode $ statement ,
128
+ TypeRepositoryInterface $ types ,
129
+ TypeParserInterface $ parser ,
130
+ ): ArrayType {
84
131
$ arguments = $ statement ->arguments ->items ?? [];
85
132
86
133
assert (\array_key_exists (0 , $ arguments ));
@@ -91,6 +138,11 @@ private function buildByValue(NamedTypeNode $statement, TypeRepositoryInterface
91
138
$ this ->expectNoTemplateArgumentHint ($ statement , $ value );
92
139
93
140
return new ArrayType (
141
+ key: $ types ->getTypeByStatement (
142
+ statement: $ parser ->getStatementByDefinition (
143
+ definition: $ this ->keyType ,
144
+ ),
145
+ ),
94
146
value: $ types ->getTypeByStatement ($ value ->value ),
95
147
);
96
148
}
0 commit comments