Failed Conditions
Push — master ( 392b56...c3d69c )
by Vladimir
03:59
created

InputObjectType::assertValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
namespace GraphQL\Type\Definition;
3
4
use GraphQL\Error\InvariantViolation;
5
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
6
use GraphQL\Utils\Utils;
7
8
/**
9
 * Class InputObjectType
10
 * @package GraphQL\Type\Definition
11
 */
12
class InputObjectType extends Type implements InputType, NamedType
13
{
14
    /**
15
     * @var InputObjectField[]
16
     */
17
    private $fields;
18
19
    /**
20
     * @var InputObjectTypeDefinitionNode|null
21
     */
22
    public $astNode;
23
24
    /**
25
     * InputObjectType constructor.
26
     * @param array $config
27
     */
28 564
    public function __construct(array $config)
29
    {
30 564
        if (!isset($config['name'])) {
31 1
            $config['name'] = $this->tryInferName();
32
        }
33
34 564
        Utils::invariant(is_string($config['name']), 'Must provide name.');
35
36 564
        $this->config = $config;
37 564
        $this->name = $config['name'];
38 564
        $this->astNode = isset($config['astNode']) ? $config['astNode'] : null;
39 564
        $this->description = isset($config['description']) ? $config['description'] : null;
40 564
    }
41
42
    /**
43
     * @return InputObjectField[]
44
     */
45 433
    public function getFields()
46
    {
47 433
        if (null === $this->fields) {
48 433
            $this->fields = [];
49 433
            $fields = isset($this->config['fields']) ? $this->config['fields'] : [];
50 433
            $fields = is_callable($fields) ? call_user_func($fields) : $fields;
51
52 433
            if (!is_array($fields)) {
53
                throw new InvariantViolation(
54
                    "{$this->name} fields must be an array or a callable which returns such an array."
55
                );
56
            }
57
58 433
            foreach ($fields as $name => $field) {
59 430
                if ($field instanceof Type) {
60 16
                    $field = ['type' => $field];
61
                }
62 430
                $field = new InputObjectField($field + ['name' => $name]);
63 430
                $this->fields[$field->name] = $field;
64
            }
65
        }
66
67 433
        return $this->fields;
68
    }
69
70
    /**
71
     * @param string $name
72
     * @return InputObjectField
73
     * @throws \Exception
74
     */
75 4
    public function getField($name)
76
    {
77 4
        if (null === $this->fields) {
78
            $this->getFields();
79
        }
80 4
        Utils::invariant(isset($this->fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->name);
81 4
        return $this->fields[$name];
82
    }
83
84
    /**
85
     * Validates type config and throws if one of type options is invalid.
86
     * Note: this method is shallow, it won't validate object fields and their arguments.
87
     *
88
     * @throws InvariantViolation
89
     */
90 6
    public function assertValid()
91
    {
92 6
        parent::assertValid();
93
94 6
        Utils::invariant(
95 6
            !empty($this->getFields()),
96 6
            "{$this->name} fields must be an associative array with field names as keys or a " .
97 6
            "callable which returns such an array."
98
        );
99
100 4
        foreach ($this->getFields() as $field) {
101 4
            $field->assertValid($this);
102
        }
103 2
    }
104
}
105