Passed
Push — master ( fa923b...ffa870 )
by Christoffer
02:28
created

ArgumentsTrait::buildArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Type\Definition;
4
5
use Digia\GraphQL\Error\InvariantException;
6
use function Digia\GraphQL\Type\isAssocArray;
7
use function Digia\GraphQL\Util\invariant;
8
9
trait ArgumentsTrait
10
{
11
    /**
12
     * @var Argument[]
13
     */
14
    protected $arguments = [];
15
16
    /**
17
     * @return bool
18
     */
19
    public function hasArguments(): bool
20
    {
21
        return !empty($this->arguments);
22
    }
23
24
    /**
25
     * @return Argument[]
26
     */
27
    public function getArguments(): array
28
    {
29
        return $this->arguments;
30
    }
31
32
    /**
33
     * @param array|Argument[] $arguments
34
     * @return $this
35
     * @throws InvariantException
36
     */
37
    protected function buildArguments(array $arguments)
38
    {
39
        invariant(
40
            isAssocArray($arguments),
41
            'Args must be an associative array with argument names as keys.'
42
        );
43
44
        foreach ($arguments as $argumentName => $argument) {
45
            $this->arguments[] = $argument instanceof Argument
46
                ? $argument
47
                : new Argument(
48
                    $argumentName,
49
                    $argument['type'] ?? null,
50
                    $argument['defaultValue'] ?? null,
51
                    $argument['description'] ?? null,
52
                    $argument['astNode'] ?? null
53
                );
54
        }
55
56
        return $this;
57
    }
58
}
59