Passed
Push — master ( 41f82c...2e19c8 )
by Christoffer
02:41
created

ArgumentsTrait::getRawArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Type\newArgument;
8
use function Digia\GraphQL\Util\invariant;
9
10
trait ArgumentsTrait
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $rawArguments = [];
16
17
    /**
18
     * @var Argument[]
19
     */
20
    protected $arguments;
21
22
    /**
23
     * @return null|string
24
     */
25
    public abstract function getName(): ?string;
26
27
    /**
28
     * @return array
29
     */
30
    public function getRawArguments(): array
31
    {
32
        return $this->rawArguments;
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    public function hasArguments(): bool
39
    {
40
        return !empty($this->arguments);
41
    }
42
43
    /**
44
     * @return Argument[]
45
     * @throws InvariantException
46
     */
47
    public function getArguments(): array
48
    {
49
        return $this->arguments;
50
    }
51
52
    /**
53
     * @param string $typeName
54
     * @param array  $rawArguments
55
     * @return Argument[]
56
     * @throws InvariantException
57
     */
58
    protected function buildArguments(string $typeName, array $rawArguments): array
59
    {
60
        invariant(
61
            isAssocArray($rawArguments),
62
            \sprintf(
63
                '%s.%s args must be an object with argument names as keys.',
64
                $typeName,
65
                $this->getName()
66
            )
67
        );
68
69
        $arguments = [];
70
71
        foreach ($rawArguments as $argumentName => $argumentConfig) {
72
            $argumentConfig['name'] = $argumentName;
73
74
            $arguments[] = newArgument($argumentConfig);
75
        }
76
77
        return $arguments;
78
    }
79
}
80