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\Doctrine\Types; |
10
|
|
|
use GraphQL\Type\Definition\Type; |
11
|
|
|
use ReflectionMethod; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A factory to create a configuration for all fields of an entity |
15
|
|
|
*/ |
16
|
|
|
class InputFieldsConfigurationFactory extends AbstractFieldsConfigurationFactory |
17
|
|
|
{ |
18
|
2 |
|
protected function getMethodPattern(): string |
19
|
|
|
{ |
20
|
2 |
|
return '~^set[A-Z]~'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the entire configuration for a method |
25
|
|
|
* @param ReflectionMethod $method |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
2 |
|
protected function methodToConfiguration(ReflectionMethod $method): ?array |
29
|
|
|
{ |
30
|
|
|
// Silently ignore setter with anything than exactly 1 parameter |
31
|
2 |
|
$params = $method->getParameters(); |
32
|
2 |
|
if (count($params) !== 1) { |
33
|
1 |
|
return null; |
34
|
|
|
} |
35
|
2 |
|
$param = reset($params); |
36
|
|
|
|
37
|
|
|
// First get user specified values |
38
|
2 |
|
$field = $this->getInputFieldFromAnnotation($method); |
39
|
|
|
|
40
|
2 |
|
$fieldName = lcfirst(preg_replace('~^set~', '', $method->getName())); |
41
|
2 |
|
if (!$field->name) { |
42
|
2 |
|
$field->name = $fieldName; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
$docBlock = new DocBlockReader($method); |
46
|
2 |
|
if (!$field->description) { |
47
|
2 |
|
$field->description = $docBlock->getMethodDescription(); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
if (!isset($field->defaultValue) && $param->isDefaultValueAvailable()) { |
51
|
1 |
|
$field->defaultValue = $param->getDefaultValue(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// If still no type, look for docblock |
55
|
2 |
|
if (!$field->type) { |
56
|
2 |
|
$typeDeclaration = $docBlock->getParameterType($param); |
|
|
|
|
57
|
2 |
|
$this->throwIfArray($param, $typeDeclaration); |
|
|
|
|
58
|
2 |
|
$field->type = $this->getTypeFromPhpDeclaration($method, $typeDeclaration, true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// If still no type, look for type hint |
62
|
2 |
|
$type = $param->getType(); |
63
|
2 |
|
if (!$field->type && $type) { |
64
|
1 |
|
$this->throwIfArray($param, (string) $type); |
|
|
|
|
65
|
1 |
|
$field->type = $this->refelectionTypeToType($type, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$field->type = $this->nonNullIfHasDefault($param, $field->type); |
|
|
|
|
69
|
|
|
|
70
|
|
|
// If still no type, cannot continue |
71
|
2 |
|
$this->throwIfNotInputType($param, $field->type, 'Input'); |
|
|
|
|
72
|
|
|
|
73
|
1 |
|
return $field->toArray(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get a field from annotation, or an empty one |
78
|
|
|
* All its types will be converted from string to real instance of Type |
79
|
|
|
* |
80
|
|
|
* @param ReflectionMethod $method |
81
|
|
|
* @return Input |
82
|
|
|
*/ |
83
|
2 |
|
private function getInputFieldFromAnnotation(ReflectionMethod $method): Input |
84
|
|
|
{ |
85
|
2 |
|
$field = $this->getAnnotationReader()->getMethodAnnotation($method, Input::class) ?? new Input(); |
86
|
2 |
|
$field->type = $this->getTypeFromPhpDeclaration($method, $field->type); |
87
|
|
|
|
88
|
2 |
|
return $field; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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
.