1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fubhy\GraphQL\Type\Definition\Types; |
4
|
|
|
|
5
|
|
|
use Fubhy\GraphQL\Type\Definition\FieldDefinition; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Interface Type Definition. |
9
|
|
|
* |
10
|
|
|
* When a field can return one of a heterogeneous set of types, a Interface type |
11
|
|
|
* is used to describe what types are possible, what fields are in common across |
12
|
|
|
* all types, as well as a function to determine which type is actually used |
13
|
|
|
* when the field is resolved. |
14
|
|
|
*/ |
15
|
|
|
class InterfaceType extends AbstractType implements OutputTypeInterface, CompositeTypeInterface, NullableTypeInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $fields; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var callable|null |
24
|
|
|
*/ |
25
|
|
|
protected $typeResolver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\Types\TypeInterface[] |
29
|
|
|
*/ |
30
|
|
|
protected $types = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Fubhy\GraphQL\Type\Definition\FieldDefinition[] |
34
|
|
|
*/ |
35
|
|
|
protected $fieldMap; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param string $name |
41
|
|
|
* @param array $fields |
42
|
|
|
* @param callable|null $typeResolver |
43
|
|
|
* @param string|null $description |
44
|
|
|
*/ |
45
|
87 |
|
public function __construct($name, array $fields = [], callable $typeResolver = NULL, $description = NULL) |
46
|
|
|
{ |
47
|
87 |
|
parent::__construct($name, $description); |
48
|
|
|
|
49
|
87 |
|
$this->fields = $fields; |
50
|
87 |
|
$this->typeResolver = $typeResolver; |
51
|
87 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
54 |
|
public function getName() |
57
|
|
|
{ |
58
|
54 |
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getDescription() |
65
|
|
|
{ |
66
|
|
|
return $this->description; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Fubhy\GraphQL\Type\Definition\FieldDefinition[] |
71
|
|
|
*/ |
72
|
48 |
View Code Duplication |
public function getFields() |
|
|
|
|
73
|
|
|
{ |
74
|
48 |
|
if (!isset($this->fieldMap)) { |
75
|
48 |
|
$this->fieldMap = []; |
76
|
48 |
|
foreach ($this->fields as $name => $field) { |
77
|
45 |
|
if (!isset($field['name'])) { |
78
|
45 |
|
$field['name'] = $name; |
79
|
45 |
|
} |
80
|
|
|
|
81
|
45 |
|
$this->fieldMap[$name] = new FieldDefinition($field); |
82
|
48 |
|
} |
83
|
48 |
|
|
84
|
|
|
unset($this->fields); |
85
|
48 |
|
} |
86
|
|
|
|
87
|
|
|
return $this->fieldMap; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
51 |
|
* @param array $fields |
92
|
|
|
*/ |
93
|
51 |
|
public function setFields(array $fields) { |
94
|
|
|
unset($this->fields); |
95
|
|
|
$this->fieldMap = $fields; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
84 |
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
84 |
|
public function getPossibleTypes() |
102
|
84 |
|
{ |
103
|
|
|
return $this->types; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
3 |
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
3 |
|
public function addPossibleType(ObjectType $type) |
110
|
|
|
{ |
111
|
|
|
$this->types = array_unique(array_merge($this->types, [$type])); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
21 |
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
21 |
|
public function isPossibleType(ObjectType $type) |
118
|
12 |
|
{ |
119
|
|
|
return in_array($type, $this->types, TRUE); |
120
|
|
|
} |
121
|
9 |
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function resolveType($value) |
126
|
|
|
{ |
127
|
|
|
if (isset($this->typeResolver)) { |
128
|
|
|
return call_user_func($this->typeResolver, $value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->getTypeOf($value); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.