1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQLite\Types\JSONScalar; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\InputType; |
6
|
|
|
use GraphQL\Type\Definition\NamedType; |
7
|
|
|
use GraphQL\Type\Definition\OutputType; |
8
|
|
|
use phpDocumentor\Reflection\DocBlock; |
9
|
|
|
use phpDocumentor\Reflection\Type; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use ReflectionProperty; |
12
|
|
|
use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperInterface; |
13
|
|
|
|
14
|
|
|
class JSONScalarTypeMapper implements RootTypeMapperInterface |
15
|
|
|
{ |
16
|
|
|
/** @var RootTypeMapperInterface */ |
17
|
|
|
private $next; |
18
|
|
|
|
19
|
|
|
public function __construct(RootTypeMapperInterface $next) |
20
|
|
|
{ |
21
|
|
|
$this->next = $next; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function toGraphQLOutputType( |
25
|
|
|
Type $type, |
26
|
|
|
?OutputType $subType, |
27
|
|
|
$reflector, |
28
|
|
|
DocBlock $docBlockObj |
29
|
|
|
): OutputType { |
30
|
|
|
if ($this->matchReturnType($docBlockObj)) { |
31
|
|
|
return JSONScalarType::getInstance(); |
32
|
|
|
} |
33
|
|
|
return $this->next->toGraphQLOutputType($type, $subType, $reflector, $docBlockObj); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function toGraphQLInputType( |
37
|
|
|
Type $type, |
38
|
|
|
?InputType $subType, |
39
|
|
|
string $argumentName, |
40
|
|
|
$reflector, |
41
|
|
|
DocBlock $docBlockObj |
42
|
|
|
): InputType { |
43
|
|
|
if ($this->matchInputType($argumentName, $reflector, $docBlockObj)) { |
44
|
|
|
return JSONScalarType::getInstance(); |
45
|
|
|
} |
46
|
|
|
return $this->next->toGraphQLInputType($type, $subType, $argumentName, $reflector, $docBlockObj); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $typeName The name of the GraphQL type |
51
|
|
|
* @return NamedType |
52
|
|
|
*/ |
53
|
|
|
public function mapNameToType(string $typeName): NamedType |
54
|
|
|
{ |
55
|
|
|
if ($typeName === JSONScalarType::NAME) { |
56
|
|
|
return JSONScalarType::getInstance(); |
57
|
|
|
} |
58
|
|
|
return $this->next->mapNameToType($typeName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param DocBlock $docBlockObj |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
private function matchReturnType(DocBlock $docBlockObj): bool |
66
|
|
|
{ |
67
|
|
|
$paramText = $docBlockObj->hasTag('return') ? |
68
|
|
|
$docBlockObj->getTagsByName('return')[0]->__toString() : |
69
|
|
|
null; |
70
|
|
|
return strpos($paramText, JSONScalarType::NAME) > 0; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $argumentName |
75
|
|
|
* @param ReflectionMethod|ReflectionProperty $reflector |
76
|
|
|
* @param DocBlock $docBlockObj |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
private function matchInputType(string $argumentName, $reflector, DocBlock $docBlockObj): bool |
80
|
|
|
{ |
81
|
|
|
$params = $docBlockObj->getTagsByName('param'); |
82
|
|
|
for ($i = 0; $i < count($params); $i++) { |
|
|
|
|
83
|
|
|
$paramText = $params[$i]->__toString(); |
84
|
|
|
if ($reflector instanceof ReflectionMethod) { |
85
|
|
|
$refParams = $reflector->getParameters(); |
86
|
|
|
if (strpos($paramText, JSONScalarType::NAME) > 0 && $refParams[$i]->getName() === $argumentName) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|