|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
|
5
|
|
|
use Youshido\GraphQL\Type\InputTypeInterface; |
|
6
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A simple implementation of the TypeMapperInterface that expects mapping to be passed in a setter. |
|
10
|
|
|
* |
|
11
|
|
|
* Note: no constructor argument as this results in a loop most of the time. |
|
12
|
|
|
*/ |
|
13
|
|
|
final class StaticTypeMapper implements TypeMapperInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array<string,TypeInterface> |
|
17
|
|
|
*/ |
|
18
|
|
|
private $types; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* An array mapping a fully qualified class name to the matching TypeInterface |
|
22
|
|
|
* |
|
23
|
|
|
* @param array<string,TypeInterface> $types |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setTypes(array $types) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->types = $types; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array<string,InputTypeInterface> |
|
32
|
|
|
*/ |
|
33
|
|
|
private $inputTypes; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* An array mapping a fully qualified class name to the matching InputTypeInterface |
|
37
|
|
|
* |
|
38
|
|
|
* @param array<string,InputTypeInterface> $types |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setInputTypes(array $inputTypes) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->inputTypes = $inputTypes; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL type. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $className |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function canMapClassToType(string $className): bool |
|
52
|
|
|
{ |
|
53
|
|
|
return isset($this->types[$className]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Maps a PHP fully qualified class name to a GraphQL type. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $className |
|
60
|
|
|
* @return TypeInterface |
|
61
|
|
|
* @throws CannotMapTypeException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function mapClassToType(string $className): TypeInterface |
|
64
|
|
|
{ |
|
65
|
|
|
if (isset($this->types[$className])) { |
|
66
|
|
|
return $this->types[$className]; |
|
67
|
|
|
} |
|
68
|
|
|
throw CannotMapTypeException::createForType($className); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $className |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function canMapClassToInputType(string $className): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return isset($this->inputTypes[$className]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Maps a PHP fully qualified class name to a GraphQL input type. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $className |
|
86
|
|
|
* @return InputTypeInterface |
|
87
|
|
|
* @throws CannotMapTypeException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function mapClassToInputType(string $className): InputTypeInterface |
|
90
|
|
|
{ |
|
91
|
|
|
if (isset($this->inputTypes[$className])) { |
|
92
|
|
|
return $this->inputTypes[$className]; |
|
93
|
|
|
} |
|
94
|
|
|
throw CannotMapTypeException::createForInputType($className); |
|
95
|
|
|
} |
|
96
|
|
|
} |