1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Doctrine\Factory; |
6
|
|
|
|
7
|
|
|
use GraphQL\Doctrine\Annotation\Input; |
8
|
|
|
use GraphQL\Doctrine\DocBlockReader; |
9
|
|
|
use GraphQL\Type\Definition\Type; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use ReflectionParameter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A factory to create a configuration for all setters of an entity |
15
|
|
|
*/ |
16
|
|
|
class InputFieldsConfigurationFactory extends AbstractFieldsConfigurationFactory |
17
|
|
|
{ |
18
|
3 |
|
protected function getMethodPattern(): string |
19
|
|
|
{ |
20
|
3 |
|
return '~^set[A-Z]~'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the entire configuration for a method |
25
|
|
|
* @param ReflectionMethod $method |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
3 |
|
protected function methodToConfiguration(ReflectionMethod $method): ?array |
29
|
|
|
{ |
30
|
|
|
// Silently ignore setter with anything than exactly 1 parameter |
31
|
3 |
|
$params = $method->getParameters(); |
32
|
3 |
|
if (count($params) !== 1) { |
33
|
2 |
|
return null; |
34
|
|
|
} |
35
|
3 |
|
$param = reset($params); |
36
|
|
|
|
37
|
|
|
// Get a field from annotation, or an empty one |
38
|
3 |
|
$field = $this->getAnnotationReader()->getMethodAnnotation($method, Input::class) ?? new Input(); |
39
|
|
|
|
40
|
3 |
|
if (!$field->type instanceof Type) { |
41
|
3 |
|
$this->convertTypeDeclarationsToInstances($method, $field); |
42
|
3 |
|
$this->completeField($method, $param, $field); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
return $field->toArray(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* All its types will be converted from string to real instance of Type |
50
|
|
|
* |
51
|
|
|
* @param ReflectionMethod $method |
52
|
|
|
* @param Field $field |
53
|
|
|
*/ |
54
|
3 |
|
private function convertTypeDeclarationsToInstances(ReflectionMethod $method, Input $field): void |
55
|
|
|
{ |
56
|
3 |
|
$field->type = $this->getTypeFromPhpDeclaration($method, $field->type); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Complete field with info from doc blocks and type hints |
61
|
|
|
* @param ReflectionMethod $method |
62
|
|
|
* @param ReflectionParameter $param |
63
|
|
|
* @param Field $field |
64
|
|
|
*/ |
65
|
3 |
|
private function completeField(ReflectionMethod $method, ReflectionParameter $param, Input $field): void |
66
|
|
|
{ |
67
|
3 |
|
$fieldName = lcfirst(preg_replace('~^set~', '', $method->getName())); |
68
|
3 |
|
if (!$field->name) { |
69
|
3 |
|
$field->name = $fieldName; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
$docBlock = new DocBlockReader($method); |
73
|
3 |
|
if (!$field->description) { |
74
|
3 |
|
$field->description = $docBlock->getMethodDescription(); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
if (!isset($field->defaultValue) && $param->isDefaultValueAvailable()) { |
78
|
2 |
|
$field->defaultValue = $param->getDefaultValue(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// If still no type, look for docblock |
82
|
3 |
|
if (!$field->type) { |
83
|
3 |
|
$typeDeclaration = $docBlock->getParameterType($param); |
84
|
3 |
|
$this->throwIfArray($param, $typeDeclaration); |
85
|
3 |
|
$field->type = $this->getTypeFromPhpDeclaration($method, $typeDeclaration, true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If still no type, look for type hint |
89
|
3 |
|
$type = $param->getType(); |
90
|
3 |
|
if (!$field->type && $type) { |
91
|
2 |
|
$this->throwIfArray($param, (string) $type); |
92
|
2 |
|
$field->type = $this->refelectionTypeToType($type, true); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
$field->type = $this->nonNullIfHasDefault($param, $field->type); |
|
|
|
|
96
|
|
|
|
97
|
|
|
// If still no type, cannot continue |
98
|
3 |
|
$this->throwIfNotInputType($param, $field->type, 'Input'); |
99
|
2 |
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.