1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of graphql-youshido project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 12/2/15 9:00 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Type\InputObject; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Config\Object\InputObjectTypeConfig; |
13
|
|
|
use Youshido\GraphQL\Field\FieldInterface; |
14
|
|
|
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject; |
15
|
|
|
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable; |
16
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
17
|
|
|
use Youshido\GraphQL\Type\Traits\AutoNameTrait; |
18
|
|
|
use Youshido\GraphQL\Type\Traits\FieldsAwareObjectTrait; |
19
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
20
|
|
|
|
21
|
|
|
abstract class AbstractInputObjectType extends AbstractType |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
use AutoNameTrait, FieldsAwareObjectTrait; |
25
|
|
|
|
26
|
|
|
protected $isBuilt = false; |
27
|
|
|
|
28
|
7 |
|
public function getConfig() |
29
|
|
|
{ |
30
|
7 |
|
if (!$this->isBuilt) { |
31
|
7 |
|
$this->isBuilt = true; |
32
|
7 |
|
$this->build($this->config); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
return $this->config; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function __construct($config = []) |
39
|
|
|
{ |
40
|
1 |
|
if (empty($config)) { |
41
|
|
|
$config = [ |
42
|
1 |
|
'name' => $this->getName() |
43
|
|
|
]; |
44
|
|
|
} |
45
|
1 |
|
$this->config = new InputObjectTypeConfig($config, $this); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param InputObjectTypeConfig $config |
50
|
|
|
*/ |
51
|
|
|
abstract public function build($config); |
52
|
|
|
|
53
|
7 |
|
public function isValidValue($value) |
54
|
|
|
{ |
55
|
7 |
|
if ($value instanceof InputObject) { |
56
|
|
|
$value = $value->getValue(); |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
if (empty($value)) { |
60
|
1 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
if (!is_array($value)) { |
64
|
1 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$typeConfig = $this->getConfig(); |
68
|
6 |
|
$requiredFields = array_filter($typeConfig->getFields(), function (FieldInterface $field) { |
|
|
|
|
69
|
6 |
|
return $field->getType()->getKind() == TypeMap::KIND_NON_NULL; |
70
|
6 |
|
}); |
71
|
|
|
|
72
|
6 |
|
foreach ($value as $valueKey => $valueItem) { |
73
|
6 |
|
if (!$typeConfig->hasField($valueKey)) { |
|
|
|
|
74
|
|
|
// Schema validation will generate the error message for us. |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$field = $typeConfig->getField($valueKey); |
|
|
|
|
79
|
6 |
|
if (!$field->getType()->isValidValue($valueItem)) { |
80
|
3 |
|
$error = $field->getType()->getLastError() ?: '(no details available)'; |
81
|
3 |
|
$this->lastError = sprintf('Not valid type for field "%s" in input type "%s": %s', $field->getName(), $this->getName(), $error); |
82
|
3 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
if (array_key_exists($valueKey, $requiredFields)) { |
86
|
5 |
|
unset($requiredFields[$valueKey]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
5 |
|
if (count($requiredFields)) { |
90
|
1 |
|
$this->lastError = sprintf('%s %s required on %s', implode(', ', array_keys($requiredFields)), count($requiredFields) > 1 ? 'are' : 'is', $typeConfig->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
return !(count($requiredFields) > 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
public function getKind() |
97
|
|
|
{ |
98
|
8 |
|
return TypeMap::KIND_INPUT_OBJECT; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
public function isInputType() |
102
|
|
|
{ |
103
|
2 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
public function parseValue($value) |
107
|
|
|
{ |
108
|
4 |
|
if (is_null($value)) return null; |
109
|
4 |
|
if($value instanceof InputObject) { |
110
|
|
|
$value = $value->getValue(); |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
$typeConfig = $this->getConfig(); |
114
|
4 |
|
foreach ($value as $valueKey => $item) { |
115
|
3 |
|
if ($item instanceof Variable) { |
116
|
|
|
$item = $item->getValue(); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
if (!($inputField = $typeConfig->getField($valueKey))) { |
|
|
|
|
120
|
|
|
throw new \Exception(sprintf('Invalid field "%s" on %s', $valueKey, $typeConfig->getName())); |
121
|
|
|
} |
122
|
3 |
|
$value[$valueKey] = $inputField->getType()->parseValue($item); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
return $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: