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\Object; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Type\Config\InputTypeConfigInterface; |
13
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
14
|
|
|
use Youshido\GraphQL\Type\Config\Object\InputObjectTypeConfig; |
15
|
|
|
use Youshido\GraphQL\Type\Field\Field; |
16
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
17
|
|
|
|
18
|
|
|
abstract class AbstractInputObjectType extends AbstractType |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* ObjectType constructor. |
22
|
|
|
* @param $config |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct($config = []) |
25
|
|
|
{ |
26
|
1 |
|
if (empty($config) && (get_class($this) != 'Youshido\GraphQL\Type\Object\InputObjectType')) { |
27
|
|
|
$config['name'] = $this->getName(); |
28
|
|
|
$config['output'] = $this->getOutputType(); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
$this->config = new InputObjectTypeConfig($config, $this); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
abstract protected function getOutputType(); |
35
|
|
|
|
36
|
|
|
public function resolve($value = null, $args = []) |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
protected function build(InputTypeConfigInterface $config) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
|
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
public function isValidValue($value) |
47
|
|
|
{ |
48
|
|
|
if (!is_array($value)) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$requiredFields = array_filter($this->getConfig()->getFields(), function (Field $field) { |
53
|
|
|
return $field->getConfig()->get('required'); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
foreach ($value as $valueKey => $valueItem) { |
57
|
|
|
if (!$this->getConfig()->hasField($valueKey) || !$this->getConfig()->getField($valueKey)->getType()->isValidValue($valueItem)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (array_key_exists($valueKey, $requiredFields)) { |
62
|
|
|
unset($requiredFields[$valueKey]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return !(count($requiredFields) > 0); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function checkBuild() |
70
|
|
|
{ |
71
|
1 |
|
if (!$this->isBuild) { |
72
|
1 |
|
$this->isBuild = true; |
73
|
1 |
|
$this->build($this->config); |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
public function getKind() |
78
|
|
|
{ |
79
|
|
|
return TypeMap::KIND_INPUT_OBJECT; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.