-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathEnumType.php
More file actions
277 lines (234 loc) · 8.07 KB
/
EnumType.php
File metadata and controls
277 lines (234 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\SerializationError;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\EnumTypeDefinitionNode;
use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\EnumValueDefinitionNode;
use GraphQL\Language\AST\EnumValueNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\Printer;
use GraphQL\Utils\MixedStore;
use GraphQL\Utils\Utils;
/**
* @see EnumValueDefinitionNode
*
* @phpstan-type PartialEnumValueConfig array{
* name?: string,
* value?: mixed,
* deprecationReason?: string|null,
* description?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: EnumValueDefinitionNode|null
* }
* @phpstan-type EnumValues iterable<string, PartialEnumValueConfig>|iterable<string, mixed>|iterable<int, string>
* @phpstan-type EnumTypeConfig array{
* name?: string|null,
* description?: string|null,
* values: EnumValues|callable(): EnumValues,
* directives?: array<DirectiveNode>|null,
* astNode?: EnumTypeDefinitionNode|null,
* extensionASTNodes?: array<EnumTypeExtensionNode>|null
* }
*/
class EnumType extends Type implements InputType, OutputType, LeafType, NullableType, NamedType
{
use NamedTypeImplementation;
public ?EnumTypeDefinitionNode $astNode;
/** @var array<EnumTypeExtensionNode> */
public array $extensionASTNodes;
/** @var array<DirectiveNode> */
public array $directives;
/** @phpstan-var EnumTypeConfig */
public array $config;
/**
* Lazily initialized.
*
* @var array<int, EnumValueDefinition>
*/
private array $values;
/**
* Lazily initialized.
*
* @var MixedStore<EnumValueDefinition>
*/
private MixedStore $valueLookup;
/** @var array<string, EnumValueDefinition> */
private array $nameLookup;
/**
* @phpstan-param EnumTypeConfig $config
*
* @throws InvariantViolation
*/
public function __construct(array $config)
{
$this->name = $config['name'] ?? $this->inferName();
$this->description = $config['description'] ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->extensionASTNodes = $config['extensionASTNodes'] ?? [];
$this->directives = $config['directives'] ?? [];
$this->config = $config;
}
/** @throws InvariantViolation */
public function getValue(string $name): ?EnumValueDefinition
{
if (! isset($this->nameLookup)) {
$this->initializeNameLookup();
}
return $this->nameLookup[$name] ?? null;
}
/**
* @throws InvariantViolation
*
* @return array<int, EnumValueDefinition>
*/
public function getValues(): array
{
if (! isset($this->values)) {
$this->values = [];
$values = $this->config['values'];
if (is_callable($values)) {
$values = $values();
}
// We are just assuming the config option is set correctly here, validation happens in assertValid()
foreach ($values as $name => $value) {
if (is_string($name)) {
if (is_array($value)) {
$value += ['name' => $name, 'value' => $name];
} else {
$value = ['name' => $name, 'value' => $value];
}
} elseif (is_string($value)) {
$value = ['name' => $value, 'value' => $value];
} else {
throw new InvariantViolation("{$this->name} values must be an array with value names as keys or values.");
}
// @phpstan-ignore-next-line assume the config matches
$this->values[] = new EnumValueDefinition($value);
}
}
return $this->values;
}
/**
* @throws \InvalidArgumentException
* @throws InvariantViolation
* @throws SerializationError
*/
public function serialize($value)
{
$lookup = $this->getValueLookup();
if (isset($lookup[$value])) {
return $lookup[$value]->name;
}
if ($value instanceof \BackedEnum) {
return $value->name;
}
if ($value instanceof \UnitEnum) {
return $value->name;
}
$safeValue = Utils::printSafe($value);
throw new SerializationError("Cannot serialize value as enum: {$safeValue}");
}
/**
* @throws \InvalidArgumentException
* @throws InvariantViolation
*
* @return MixedStore<EnumValueDefinition>
*/
private function getValueLookup(): MixedStore
{
if (! isset($this->valueLookup)) {
$this->valueLookup = new MixedStore();
foreach ($this->getValues() as $value) {
$this->valueLookup->offsetSet($value->value, $value);
}
}
return $this->valueLookup;
}
/**
* @throws Error
* @throws InvariantViolation
*/
public function parseValue($value)
{
if (! is_string($value)) {
$safeValue = Utils::printSafeJson($value);
throw new Error("Enum \"{$this->name}\" cannot represent non-string value: {$safeValue}.{$this->didYouMean($safeValue)}");
}
if (! isset($this->nameLookup)) {
$this->initializeNameLookup();
}
if (! isset($this->nameLookup[$value])) {
throw new Error("Value \"{$value}\" does not exist in \"{$this->name}\" enum.{$this->didYouMean($value)}");
}
return $this->nameLookup[$value]->value;
}
/**
* @throws \JsonException
* @throws Error
* @throws InvariantViolation
*/
public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if (! $valueNode instanceof EnumValueNode) {
$valueStr = Printer::doPrint($valueNode);
throw new Error("Enum \"{$this->name}\" cannot represent non-enum value: {$valueStr}.{$this->didYouMean($valueStr)}", $valueNode);
}
$name = $valueNode->value;
if (! isset($this->nameLookup)) {
$this->initializeNameLookup();
}
if (isset($this->nameLookup[$name])) {
return $this->nameLookup[$name]->value;
}
$valueStr = Printer::doPrint($valueNode);
throw new Error("Value \"{$valueStr}\" does not exist in \"{$this->name}\" enum.{$this->didYouMean($valueStr)}", $valueNode);
}
/**
* @throws Error
* @throws InvariantViolation
*/
public function assertValid(): void
{
Utils::assertValidName($this->name);
$values = $this->config['values'] ?? null; // @phpstan-ignore nullCoalesce.initializedProperty (unnecessary according to types, but can happen during runtime)
if (! is_iterable($values) && ! is_callable($values)) {
$notIterable = Utils::printSafe($values);
throw new InvariantViolation("{$this->name} values must be an iterable or callable, got: {$notIterable}");
}
$this->getValues();
}
/** @throws InvariantViolation */
private function initializeNameLookup(): void
{
$this->nameLookup = [];
foreach ($this->getValues() as $value) {
$this->nameLookup[$value->name] = $value;
}
}
/** @throws InvariantViolation */
protected function didYouMean(string $unknownValue): ?string
{
$suggestions = Utils::suggestionList(
$unknownValue,
array_map(
static fn (EnumValueDefinition $value): string => $value->name,
$this->getValues()
)
);
return $suggestions === []
? null
: ' Did you mean the enum value ' . Utils::quotedOrList($suggestions) . '?';
}
public function astNode(): ?EnumTypeDefinitionNode
{
return $this->astNode;
}
/** @return array<EnumTypeExtensionNode> */
public function extensionASTNodes(): array
{
return $this->extensionASTNodes;
}
}