|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Mappers; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use function get_parent_class; |
|
8
|
|
|
use GraphQL\Type\Definition\InputType; |
|
9
|
|
|
use GraphQL\Type\Definition\OutputType; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class wraps a TypeMapperInterface into a RecursiveTypeMapperInterface. |
|
13
|
|
|
* While the wrapped class does only tests one given class, the recursive type mapper |
|
14
|
|
|
* tests the class and all its parents. |
|
15
|
|
|
*/ |
|
16
|
|
|
class RecursiveTypeMapper implements RecursiveTypeMapperInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var TypeMapperInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $typeMapper; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(TypeMapperInterface $typeMapper) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->typeMapper = $typeMapper; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL type. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $className The class name to look for (this function looks into parent classes if the class does not match a type). |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function canMapClassToType(string $className): bool |
|
35
|
|
|
{ |
|
36
|
|
|
do { |
|
37
|
|
|
if ($this->typeMapper->canMapClassToType($className)) { |
|
38
|
|
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
} while ($className = get_parent_class($className)); |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Maps a PHP fully qualified class name to a GraphQL type. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $className The class name to look for (this function looks into parent classes if the class does not match a type). |
|
48
|
|
|
* @return OutputType&Type |
|
49
|
|
|
* @throws CannotMapTypeException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function mapClassToType(string $className): OutputType |
|
52
|
|
|
{ |
|
53
|
|
|
do { |
|
54
|
|
|
if ($this->typeMapper->canMapClassToType($className)) { |
|
55
|
|
|
return $this->typeMapper->mapClassToType($className); |
|
56
|
|
|
} |
|
57
|
|
|
} while ($className = get_parent_class($className)); |
|
58
|
|
|
throw CannotMapTypeException::createForType($className); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns true if this type mapper can map the $className FQCN to a GraphQL input type. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $className |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
public function canMapClassToInputType(string $className): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->typeMapper->canMapClassToInputType($className); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Maps a PHP fully qualified class name to a GraphQL input type. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $className |
|
76
|
|
|
* @return InputType&Type |
|
77
|
|
|
* @throws CannotMapTypeException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function mapClassToInputType(string $className): InputType |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->typeMapper->mapClassToInputType($className); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|