1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Type\Definition; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\InvariantViolation; |
8
|
|
|
use GraphQL\Language\AST\InputValueDefinitionNode; |
9
|
|
|
use GraphQL\Utils\Utils; |
10
|
|
|
use function array_key_exists; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_string; |
13
|
|
|
use function sprintf; |
14
|
|
|
|
15
|
|
|
class FieldArgument |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
public $name; |
19
|
|
|
|
20
|
|
|
/** @var mixed */ |
21
|
|
|
public $defaultValue; |
22
|
|
|
|
23
|
|
|
/** @var string|null */ |
24
|
|
|
public $description; |
25
|
|
|
|
26
|
|
|
/** @var InputValueDefinitionNode|null */ |
27
|
|
|
public $astNode; |
28
|
|
|
|
29
|
|
|
/** @var mixed[] */ |
30
|
|
|
public $config; |
31
|
|
|
|
32
|
|
|
/** @var InputType&Type */ |
33
|
|
|
private $type; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param mixed[] $def |
37
|
|
|
*/ |
38
|
646 |
|
public function __construct(array $def) |
39
|
|
|
{ |
40
|
646 |
|
foreach ($def as $key => $value) { |
41
|
|
|
switch ($key) { |
42
|
646 |
|
case 'type': |
43
|
646 |
|
$this->type = $value; |
44
|
646 |
|
break; |
45
|
646 |
|
case 'name': |
46
|
646 |
|
$this->name = $value; |
47
|
646 |
|
break; |
48
|
542 |
|
case 'defaultValue': |
49
|
476 |
|
$this->defaultValue = $value; |
50
|
476 |
|
break; |
51
|
129 |
|
case 'description': |
52
|
129 |
|
$this->description = $value; |
53
|
129 |
|
break; |
54
|
100 |
|
case 'astNode': |
55
|
100 |
|
$this->astNode = $value; |
56
|
646 |
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
646 |
|
$this->config = $def; |
60
|
646 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed[] $config |
64
|
|
|
* |
65
|
|
|
* @return FieldArgument[] |
66
|
|
|
*/ |
67
|
715 |
|
public static function createMap(array $config) : array |
68
|
|
|
{ |
69
|
715 |
|
$map = []; |
70
|
715 |
|
foreach ($config as $name => $argConfig) { |
71
|
637 |
|
if (! is_array($argConfig)) { |
72
|
11 |
|
$argConfig = ['type' => $argConfig]; |
73
|
|
|
} |
74
|
637 |
|
$map[] = new self($argConfig + ['name' => $name]); |
75
|
|
|
} |
76
|
|
|
|
77
|
715 |
|
return $map; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return InputType&Type |
82
|
|
|
*/ |
83
|
909 |
|
public function getType() : Type |
84
|
|
|
{ |
85
|
909 |
|
return $this->type; |
86
|
|
|
} |
87
|
|
|
|
88
|
337 |
|
public function defaultValueExists() : bool |
89
|
|
|
{ |
90
|
337 |
|
return array_key_exists('defaultValue', $this->config); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function assertValid(FieldDefinition $parentField, Type $parentType) |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
Utils::assertValidName($this->name); |
97
|
|
|
} catch (InvariantViolation $e) { |
98
|
|
|
throw new InvariantViolation( |
99
|
|
|
sprintf('%s.%s(%s:) %s', $parentType->name, $parentField->name, $this->name, $e->getMessage()) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
$type = $this->type; |
103
|
|
|
if ($type instanceof WrappingType) { |
104
|
|
|
$type = $type->getWrappedType(true); |
105
|
|
|
} |
106
|
|
|
Utils::invariant( |
107
|
|
|
$type instanceof InputType, |
108
|
|
|
sprintf( |
109
|
|
|
'%s.%s(%s): argument type must be Input Type but got: %s', |
110
|
|
|
$parentType->name, |
111
|
|
|
$parentField->name, |
112
|
|
|
$this->name, |
113
|
|
|
Utils::printSafe($this->type) |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
Utils::invariant( |
117
|
|
|
$this->description === null || is_string($this->description), |
118
|
|
|
sprintf( |
119
|
|
|
'%s.%s(%s): argument description type must be string but got: %s', |
120
|
|
|
$parentType->name, |
121
|
|
|
$parentField->name, |
122
|
|
|
$this->name, |
123
|
|
|
Utils::printSafe($this->description) |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|