-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathType.php
270 lines (236 loc) · 6.14 KB
/
Type.php
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
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Introspection;
use GraphQL\Utils\Utils;
/**
* Registry of standard GraphQL types and base class for all other types.
*/
abstract class Type implements \JsonSerializable
{
public const INT = 'Int';
public const FLOAT = 'Float';
public const STRING = 'String';
public const BOOLEAN = 'Boolean';
public const ID = 'ID';
public const STANDARD_TYPE_NAMES = [
self::INT,
self::FLOAT,
self::STRING,
self::BOOLEAN,
self::ID,
];
public const BUILT_IN_TYPE_NAMES = [
...self::STANDARD_TYPE_NAMES,
...Introspection::TYPE_NAMES,
];
/** @var array<string, ScalarType>|null */
protected static ?array $standardTypes;
/** @var array<string, Type&NamedType>|null */
protected static ?array $builtInTypes;
/**
* @api
*
* @throws InvariantViolation
*/
public static function int(): ScalarType
{
return static::$standardTypes[self::INT] ??= new IntType();
}
/**
* @api
*
* @throws InvariantViolation
*/
public static function float(): ScalarType
{
return static::$standardTypes[self::FLOAT] ??= new FloatType();
}
/**
* @api
*
* @throws InvariantViolation
*/
public static function string(): ScalarType
{
return static::$standardTypes[self::STRING] ??= new StringType();
}
/**
* @api
*
* @throws InvariantViolation
*/
public static function boolean(): ScalarType
{
return static::$standardTypes[self::BOOLEAN] ??= new BooleanType();
}
/**
* @api
*
* @throws InvariantViolation
*/
public static function id(): ScalarType
{
return static::$standardTypes[self::ID] ??= new IDType();
}
/**
* @template T of Type
*
* @param T|callable():T $type
*
* @return ListOfType<T>
*
* @api
*/
public static function listOf($type): ListOfType
{
return new ListOfType($type);
}
/**
* @param (NullableType&Type)|callable():(NullableType&Type) $type
*
* @api
*/
public static function nonNull($type): NonNull
{
return new NonNull($type);
}
/**
* Returns all builtin in types including base scalar and introspection types.
*
* @throws InvariantViolation
*
* @return array<string, Type&NamedType>
*/
public static function builtInTypes(): array
{
return self::$builtInTypes ??= \array_merge(
Introspection::getTypes(),
self::getStandardTypes()
);
}
/**
* Returns all builtin scalar types.
*
* @throws InvariantViolation
*
* @return array<string, ScalarType>
*/
public static function getStandardTypes(): array
{
return [
self::INT => static::int(),
self::FLOAT => static::float(),
self::STRING => static::string(),
self::BOOLEAN => static::boolean(),
self::ID => static::id(),
];
}
/**
* @param array<ScalarType> $types
*
* @throws InvariantViolation
*/
public static function overrideStandardTypes(array $types): void
{
// Reset caches that might contain instances of standard types
static::$builtInTypes = null;
Introspection::resetCachedInstances();
Directive::resetCachedInstances();
foreach ($types as $type) {
// @phpstan-ignore-next-line generic type is not enforced by PHP
if (! $type instanceof ScalarType) {
$typeClass = ScalarType::class;
$notType = Utils::printSafe($type);
throw new InvariantViolation("Expecting instance of {$typeClass}, got {$notType}");
}
if (! in_array($type->name, self::STANDARD_TYPE_NAMES, true)) {
$standardTypeNames = \implode(', ', self::STANDARD_TYPE_NAMES);
$notStandardTypeName = Utils::printSafe($type->name);
throw new InvariantViolation("Expecting one of the following names for a standard type: {$standardTypeNames}; got {$notStandardTypeName}");
}
static::$standardTypes[$type->name] = $type;
}
}
/**
* @param mixed $type
*
* @api
*/
public static function isInputType($type): bool
{
return self::getNamedType($type) instanceof InputType;
}
/**
* @return (Type&NamedType)|null
*
* @api
*/
public static function getNamedType(?Type $type): ?Type
{
if ($type instanceof WrappingType) {
return $type->getInnermostType();
}
assert($type === null || $type instanceof NamedType, 'only other option');
return $type;
}
/**
* @param mixed $type
*
* @api
*/
public static function isOutputType($type): bool
{
return self::getNamedType($type) instanceof OutputType;
}
/**
* @param mixed $type
*
* @api
*/
public static function isLeafType($type): bool
{
return $type instanceof LeafType;
}
/**
* @param mixed $type
*
* @api
*/
public static function isCompositeType($type): bool
{
return $type instanceof CompositeType;
}
/**
* @param mixed $type
*
* @api
*/
public static function isAbstractType($type): bool
{
return $type instanceof AbstractType;
}
/**
* @return Type&NullableType
*
* @api
*/
public static function getNullableType(Type $type): Type
{
if ($type instanceof NonNull) {
return $type->getWrappedType();
}
assert($type instanceof NullableType, 'only other option');
return $type;
}
abstract public function toString(): string;
public function __toString(): string
{
return $this->toString();
}
#[\ReturnTypeWillChange]
public function jsonSerialize(): string
{
return $this->toString();
}
}