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\InputFieldInterface; |
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
|
8 |
View Code Duplication |
public function getConfig() |
|
|
|
|
29
|
|
|
{ |
30
|
8 |
|
if (!$this->isBuilt) { |
31
|
8 |
|
$this->isBuilt = true; |
32
|
8 |
|
$this->build($this->config); |
|
|
|
|
33
|
8 |
|
} |
34
|
|
|
|
35
|
8 |
|
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
|
1 |
|
]; |
44
|
1 |
|
} |
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 (InputFieldInterface $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()->getValidationError($valueItem) ?: '(no details available)'; |
81
|
3 |
|
$this->lastValidationError = 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
|
5 |
|
} |
88
|
5 |
|
} |
89
|
5 |
|
if (count($requiredFields)) { |
90
|
1 |
|
$this->lastValidationError = sprintf('%s %s required on %s', implode(', ', array_keys($requiredFields)), count($requiredFields) > 1 ? 'are' : 'is', $typeConfig->getName()); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
5 |
|
return !(count($requiredFields) > 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
9 |
|
public function getKind() |
97
|
|
|
{ |
98
|
9 |
|
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
|
4 |
|
} |
124
|
|
|
|
125
|
4 |
|
return $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.