1
1
<?php
2
2
namespace GraphQL \Type \Definition ;
3
3
4
+ use GraphQL \Error \InvariantViolation ;
4
5
use GraphQL \Language \AST \EnumValueNode ;
5
6
use GraphQL \Utils \MixedStore ;
6
7
use GraphQL \Utils \Utils ;
@@ -26,6 +27,11 @@ class EnumType extends Type implements InputType, OutputType, LeafType
26
27
*/
27
28
private $ nameLookup ;
28
29
30
+ /**
31
+ * @var array
32
+ */
33
+ public $ config ;
34
+
29
35
public function __construct ($ config )
30
36
{
31
37
if (!isset ($ config ['name ' ])) {
@@ -47,28 +53,41 @@ public function __construct($config)
47
53
48
54
$ this ->name = $ config ['name ' ];
49
55
$ this ->description = isset ($ config ['description ' ]) ? $ config ['description ' ] : null ;
50
- $ this ->values = [];
56
+ $ this ->config = $ config ;
57
+ }
58
+
59
+ /**
60
+ * @return EnumValueDefinition[]
61
+ */
62
+ public function getValues ()
63
+ {
64
+ if ($ this ->values === null ) {
65
+ $ this ->values = [];
66
+ $ config = $ this ->config ;
51
67
52
- if (!empty ($ config ['values ' ])) {
53
- foreach ($ config ['values ' ] as $ name => $ value ) {
54
- if (!is_array ($ value )) {
68
+ if (isset ($ config ['values ' ])) {
69
+ if (!is_array ($ config ['values ' ])) {
70
+ throw new InvariantViolation ("{$ this ->name } values must be an array " );
71
+ }
72
+ foreach ($ config ['values ' ] as $ name => $ value ) {
55
73
if (is_string ($ name )) {
56
- $ value = ['name ' => $ name , 'value ' => $ value ];
74
+ if (!is_array ($ value )) {
75
+ throw new InvariantViolation (
76
+ "{$ this ->name }. $ name must refer to an associative array with a " .
77
+ '"value" key representing an internal value but got: ' . Utils::printSafe ($ value )
78
+ );
79
+ }
80
+ $ value += ['name ' => $ name , 'value ' => $ name ];
57
81
} else if (is_int ($ name ) && is_string ($ value )) {
58
82
$ value = ['name ' => $ value , 'value ' => $ value ];
83
+ } else {
84
+ throw new InvariantViolation ("{$ this ->name } values must be an array with value names as keys. " );
59
85
}
86
+ $ this ->values [] = new EnumValueDefinition ($ value );
60
87
}
61
- // value will be equal to name only if 'value' is not set in definition
62
- $ this ->values [] = new EnumValueDefinition ($ value + ['name ' => $ name , 'value ' => $ name ]);
63
88
}
64
89
}
65
- }
66
90
67
- /**
68
- * @return EnumValueDefinition[]
69
- */
70
- public function getValues ()
71
- {
72
91
return $ this ->values ;
73
92
}
74
93
@@ -168,4 +187,42 @@ private function getNameLookup()
168
187
}
169
188
return $ this ->nameLookup ;
170
189
}
190
+
191
+ /**
192
+ * @throws InvariantViolation
193
+ */
194
+ public function assertValid ()
195
+ {
196
+ parent ::assertValid ();
197
+
198
+ Utils::invariant (
199
+ isset ($ this ->config ['values ' ]),
200
+ "{$ this ->name } values must be an array. "
201
+ );
202
+
203
+ $ values = $ this ->getValues ();
204
+
205
+ Utils::invariant (
206
+ !empty ($ values ),
207
+ "{$ this ->name } values must be not empty. "
208
+ );
209
+ foreach ($ values as $ value ) {
210
+ try {
211
+ Utils::assertValidName ($ value ->name );
212
+ } catch (InvariantViolation $ e ) {
213
+ throw new InvariantViolation (
214
+ "{$ this ->name } has value with invalid name: " .
215
+ Utils::printSafe ($ value ->name ) . " ( {$ e ->getMessage ()}) "
216
+ );
217
+ }
218
+ Utils::invariant (
219
+ !in_array ($ value ->name , ['true ' , 'false ' , 'null ' ]),
220
+ "{$ this ->name }: \"{$ value ->name }\" can not be used as an Enum value. "
221
+ );
222
+ Utils::invariant (
223
+ !isset ($ value ->config ['isDeprecated ' ]),
224
+ "{$ this ->name }. {$ value ->name } should provide \"deprecationReason \" instead of \"isDeprecated \". "
225
+ );
226
+ }
227
+ }
171
228
}
0 commit comments