1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arthem\GraphQLMapper\Mapping; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type as GQLType; |
6
|
|
|
|
7
|
|
|
class Field extends AbstractType |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The model property name if different from the name |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $property; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $type; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var callable|GQLType |
23
|
|
|
*/ |
24
|
|
|
private $resolvedType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var callable |
28
|
|
|
*/ |
29
|
|
|
private $resolve; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $resolveConfig = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Field[] |
38
|
|
|
*/ |
39
|
|
|
private $arguments = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getProperty() |
45
|
|
|
{ |
46
|
|
|
return $this->property; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $property |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function setProperty($property) |
54
|
|
|
{ |
55
|
|
|
$this->property = $property; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getType() |
64
|
|
|
{ |
65
|
|
|
return $this->type; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $type |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setType($type) |
73
|
|
|
{ |
74
|
|
|
$this->type = $type; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return callable|GQLType |
81
|
|
|
*/ |
82
|
|
|
public function getResolvedType() |
83
|
|
|
{ |
84
|
|
|
return $this->resolvedType; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param callable|GQLType $resolvedType |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setResolvedType($resolvedType) |
92
|
|
|
{ |
93
|
|
|
$this->resolvedType = $resolvedType; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $resolveConfig |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function mergeResolveConfig(array $resolveConfig) |
103
|
|
|
{ |
104
|
|
|
$this->resolveConfig = array_merge($resolveConfig, $this->resolveConfig); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return callable |
111
|
|
|
*/ |
112
|
|
|
public function getResolve() |
113
|
|
|
{ |
114
|
|
|
return $this->resolve; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param callable $resolve |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setResolve($resolve) |
122
|
|
|
{ |
123
|
|
|
$this->resolve = $resolve; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getResolveConfig() |
132
|
|
|
{ |
133
|
|
|
return $this->resolveConfig; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $resolveConfig |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function setResolveConfig(array $resolveConfig) |
141
|
|
|
{ |
142
|
|
|
$this->resolveConfig = $resolveConfig; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $config |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function mergeRevolveConfig(array $config) |
152
|
|
|
{ |
153
|
|
|
$this->resolveConfig = array_merge($config, $this->resolveConfig); |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return Field[] |
160
|
|
|
*/ |
161
|
|
|
public function getArguments() |
162
|
|
|
{ |
163
|
|
|
return $this->arguments; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Field[] $arguments |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function setArguments(array $arguments) |
171
|
|
|
{ |
172
|
|
|
$this->arguments = $arguments; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function toMapping() |
181
|
|
|
{ |
182
|
|
|
$mapping = [ |
183
|
|
|
'type' => $this->resolvedType, |
184
|
|
|
'resolve' => $this->resolve, |
185
|
|
|
'resolve_config' => $this->resolveConfig, |
186
|
|
|
] + parent::toMapping(); |
187
|
|
|
|
188
|
|
|
if (!empty($this->arguments)) { |
189
|
|
|
$mapping['args'] = []; |
190
|
|
|
foreach ($this->arguments as $argument) { |
191
|
|
|
$mapping['args'][$argument->getName()] = $argument->toMapping(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $mapping; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.