|
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
|
1 |
|
public function __construct($config = []) |
|
27
|
|
|
{ |
|
28
|
1 |
|
if (empty($config)) { |
|
29
|
|
|
$config = [ |
|
30
|
1 |
|
'name' => $this->getName() |
|
31
|
1 |
|
]; |
|
32
|
1 |
|
} |
|
33
|
1 |
|
$this->config = new InputObjectTypeConfig($config, $this); |
|
34
|
1 |
|
$this->build($this->config); |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param InputObjectTypeConfig $config |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract public function build($config); |
|
42
|
|
|
|
|
43
|
3 |
|
public function isValidValue($value) |
|
44
|
|
|
{ |
|
45
|
3 |
|
if($value instanceof InputObject) { |
|
46
|
2 |
|
$value = $value->getValue(); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
if (!is_array($value)) { |
|
50
|
1 |
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
$typeConfig = $this->getConfig(); |
|
54
|
3 |
|
$requiredFields = array_filter($typeConfig->getFields(), function (FieldInterface $field) { |
|
55
|
3 |
|
return $field->getType()->getKind() == TypeMap::KIND_NON_NULL; |
|
56
|
3 |
|
}); |
|
57
|
|
|
|
|
58
|
3 |
|
foreach ($value as $valueKey => $valueItem) { |
|
59
|
3 |
|
if($valueItem instanceof Variable) { |
|
60
|
|
|
$valueItem = $valueItem->getValue(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
if (!$typeConfig->hasField($valueKey) || !$typeConfig->getField($valueKey)->getType()->isValidValue($valueItem)) { |
|
64
|
2 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
if (array_key_exists($valueKey, $requiredFields)) { |
|
68
|
3 |
|
unset($requiredFields[$valueKey]); |
|
69
|
3 |
|
} |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return !(count($requiredFields) > 0); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function getKind() |
|
76
|
|
|
{ |
|
77
|
3 |
|
return TypeMap::KIND_INPUT_OBJECT; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function isInputType() |
|
81
|
|
|
{ |
|
82
|
1 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function parseValue($value) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$typeConfig = $this->getConfig(); |
|
88
|
1 |
|
foreach ($value as $valueKey => $item) { |
|
89
|
1 |
|
if($item instanceof Variable) { |
|
90
|
|
|
$item = $item->getValue(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$value[$valueKey] = $typeConfig->getField($valueKey)->getType()->parseValue($item); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return $value; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|