|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fubhy\GraphQL\Type\Definition\Types; |
|
4
|
|
|
|
|
5
|
|
|
use Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType; |
|
6
|
|
|
use Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType; |
|
7
|
|
|
use Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType; |
|
8
|
|
|
use Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType; |
|
9
|
|
|
use Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* These are all of the possible kinds of types. |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class Type implements TypeInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $boolean; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $float; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $integer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $string; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $name; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $description; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\BooleanType |
|
53
|
|
|
*/ |
|
54
|
246 |
|
public static function booleanType() |
|
55
|
|
|
{ |
|
56
|
246 |
|
if (!isset(static::$boolean)) { |
|
57
|
3 |
|
static::$boolean = new BooleanType(); |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
246 |
|
return static::$boolean; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\FloatType |
|
65
|
|
|
*/ |
|
66
|
180 |
|
public static function floatType() |
|
67
|
|
|
{ |
|
68
|
180 |
|
if (!isset(static::$float)) { |
|
69
|
3 |
|
static::$float = new FloatType(); |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
180 |
|
return static::$float; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\IdType |
|
77
|
|
|
*/ |
|
78
|
153 |
|
public static function idType() |
|
79
|
|
|
{ |
|
80
|
153 |
|
if (!isset(static::$id)) { |
|
81
|
3 |
|
static::$id = new IdType(); |
|
82
|
3 |
|
} |
|
83
|
|
|
|
|
84
|
153 |
|
return static::$id; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\IntType |
|
89
|
|
|
*/ |
|
90
|
303 |
|
public static function intType() |
|
91
|
|
|
{ |
|
92
|
303 |
|
if (!isset(static::$integer)) { |
|
93
|
|
|
static::$integer = new IntType(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
303 |
|
return static::$integer; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\Types\Scalars\StringType |
|
101
|
|
|
*/ |
|
102
|
348 |
|
public static function stringType() |
|
103
|
|
|
{ |
|
104
|
348 |
|
if (!isset(static::$string)) { |
|
105
|
3 |
|
static::$string = new StringType(); |
|
106
|
3 |
|
} |
|
107
|
|
|
|
|
108
|
348 |
|
return static::$string; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Constructor. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $name |
|
115
|
|
|
* @param string|null $description |
|
116
|
|
|
*/ |
|
117
|
369 |
|
public function __construct($name, $description = NULL) |
|
118
|
|
|
{ |
|
119
|
369 |
|
$this->name = $name; |
|
120
|
369 |
|
$this->description = $description; |
|
121
|
369 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
219 |
|
public function getName() |
|
127
|
|
|
{ |
|
128
|
219 |
|
return $this->name; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
9 |
|
public function getDescription() |
|
135
|
|
|
{ |
|
136
|
9 |
|
return $this->description; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
111 |
|
public function __toString() |
|
143
|
|
|
{ |
|
144
|
111 |
|
return $this->getName(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
18 |
|
public function isInputType() |
|
151
|
|
|
{ |
|
152
|
18 |
|
return $this->getUnmodifiedType() instanceof InputTypeInterface; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
18 |
|
public function isOutputType() |
|
159
|
|
|
{ |
|
160
|
18 |
|
return $this->getUnmodifiedType() instanceof OutputTypeInterface; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function isLeafType() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->getUnmodifiedType() instanceof LeafTypeInterface; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
|
|
public function isCompositeType() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->getUnmodifiedType() instanceof CompositeTypeInterface; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function isAbstractType() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->getUnmodifiedType() instanceof AbstractTypeInterface; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getNullableType() |
|
191
|
|
|
{ |
|
192
|
|
|
if ($this instanceof NonNullModifier) { |
|
193
|
|
|
return $this->getWrappedType(); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritdoc} |
|
201
|
|
|
*/ |
|
202
|
36 |
|
public function getUnmodifiedType() |
|
203
|
|
|
{ |
|
204
|
36 |
|
$return = $this; |
|
205
|
36 |
|
while ($return instanceof ListModifier || $return instanceof NonNullModifier) { |
|
206
|
36 |
|
$return = $return->getWrappedType(); |
|
207
|
36 |
|
} |
|
208
|
|
|
|
|
209
|
36 |
|
return $return; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: