1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use function array_map; |
8
|
|
|
use function array_merge; |
9
|
|
|
use function array_unique; |
10
|
|
|
use GraphQL\Type\Definition\InputType; |
11
|
|
|
use GraphQL\Type\Definition\ObjectType; |
12
|
|
|
use GraphQL\Type\Definition\OutputType; |
13
|
|
|
use GraphQL\Type\Definition\Type; |
14
|
|
|
use function is_array; |
15
|
|
|
use function iterator_to_array; |
16
|
|
|
|
17
|
|
|
class CompositeTypeMapper implements TypeMapperInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var TypeMapperInterface[] |
21
|
|
|
*/ |
22
|
|
|
private $typeMappers; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The cache of supported classes. |
26
|
|
|
* |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
private $supportedClasses; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param TypeMapperInterface[] $typeMappers |
33
|
|
|
*/ |
34
|
|
|
public function __construct(iterable $typeMappers) |
35
|
|
|
{ |
36
|
|
|
$this->typeMappers = is_array($typeMappers) ? $typeMappers: iterator_to_array($typeMappers); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL type. |
41
|
|
|
* |
42
|
|
|
* @param string $className |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function canMapClassToType(string $className): bool |
46
|
|
|
{ |
47
|
|
|
foreach ($this->typeMappers as $typeMapper) { |
48
|
|
|
if ($typeMapper->canMapClassToType($className)) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Maps a PHP fully qualified class name to a GraphQL type. |
57
|
|
|
* |
58
|
|
|
* @param string $className |
59
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
60
|
|
|
* @return ObjectType |
61
|
|
|
* @throws CannotMapTypeException |
62
|
|
|
*/ |
63
|
|
|
public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType |
64
|
|
|
{ |
65
|
|
|
foreach ($this->typeMappers as $typeMapper) { |
66
|
|
|
if ($typeMapper->canMapClassToType($className)) { |
67
|
|
|
return $typeMapper->mapClassToType($className, $recursiveTypeMapper); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
throw CannotMapTypeException::createForType($className); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the list of classes that have matching input GraphQL types. |
75
|
|
|
* |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
|
|
public function getSupportedClasses(): array |
79
|
|
|
{ |
80
|
|
|
if ($this->supportedClasses === null) { |
81
|
|
|
$supportedClassesArrays = array_map(function(TypeMapperInterface $typeMapper) { return $typeMapper->getSupportedClasses(); }, $this->typeMappers); |
82
|
|
|
$this->supportedClasses = array_unique(array_merge(...$supportedClassesArrays)); |
83
|
|
|
} |
84
|
|
|
return $this->supportedClasses; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
89
|
|
|
* |
90
|
|
|
* @param string $className |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function canMapClassToInputType(string $className): bool |
94
|
|
|
{ |
95
|
|
|
foreach ($this->typeMappers as $typeMapper) { |
96
|
|
|
if ($typeMapper->canMapClassToInputType($className)) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Maps a PHP fully qualified class name to a GraphQL input type. |
105
|
|
|
* |
106
|
|
|
* @param string $className |
107
|
|
|
* @return InputType |
108
|
|
|
* @throws CannotMapTypeException |
109
|
|
|
*/ |
110
|
|
|
public function mapClassToInputType(string $className): InputType |
111
|
|
|
{ |
112
|
|
|
foreach ($this->typeMappers as $typeMapper) { |
113
|
|
|
if ($typeMapper->canMapClassToInputType($className)) { |
114
|
|
|
return $typeMapper->mapClassToInputType($className); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
throw CannotMapTypeException::createForInputType($className); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a GraphQL type by name (can be either an input or output type) |
122
|
|
|
* |
123
|
|
|
* @param string $typeName The name of the GraphQL type |
124
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
125
|
|
|
* @return Type&(InputType|OutputType) |
126
|
|
|
*/ |
127
|
|
|
public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type |
128
|
|
|
{ |
129
|
|
|
// TODO: Implement mapNameToType() method. |
130
|
|
|
} |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: