Passed
Pull Request — master (#351)
by Kirill
02:55
created

ArgumentsTrait::getArgument()   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 1
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 GraphQL\Contracts\TypeSystem\ArgumentInterface;
7
use function Digia\GraphQL\Type\isAssocArray;
8
use function Digia\GraphQL\Type\newArgument;
9
use GraphQL\Contracts\TypeSystem\Common\ArgumentsAwareInterface as ArgumentsAwareContract;
10
11
/**
12
 * @mixin ArgumentsAwareContract
13
 */
14
trait ArgumentsTrait
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $rawArguments = [];
20
21
    /**
22
     * @var ArgumentInterface[]
23
     */
24
    protected $arguments;
25
26
    /**
27
     * @return string
28
     */
29
    abstract public function getName(): string;
30
31
    /**
32
     * @return array
33
     */
34
    public function getRawArguments(): array
35
    {
36
        return $this->rawArguments;
37
    }
38
39
    /**
40
     * @return bool
41
     */
42
    public function hasArguments(): bool
43
    {
44
        return !empty($this->arguments);
45
    }
46
47
    /**
48
     * @return Argument[]
49
     */
50
    public function getArguments(): array
51
    {
52
        return $this->arguments;
53
    }
54
55
    /**
56
     * @param string $typeName
57
     * @param array  $rawArguments
58
     * @return Argument[]
59
     * @throws InvariantException
60
     */
61
    protected function buildArguments(string $typeName, array $rawArguments): array
62
    {
63
        if (!isAssocArray($rawArguments)) {
64
            throw new InvariantException(\sprintf(
65
                '%s.%s args must be an object with argument names as keys.',
66
                $typeName,
67
                $this->getName()
68
            ));
69
        }
70
71
        $arguments = [];
72
73
        foreach ($rawArguments as $argumentName => $argumentConfig) {
74
            $argumentConfig['name'] = $argumentName;
75
76
            $arguments[] = newArgument($argumentConfig);
77
        }
78
79
        return $arguments;
80
    }
81
82
    /**
83
     * @param string $name
84
     * @return ArgumentInterface|null
85
     */
86
    public function getArgument(string $name): ?ArgumentInterface
87
    {
88
        return $this->arguments[$name] ?? null;
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return bool
94
     */
95
    public function hasArgument(string $name): bool
96
    {
97
        return isset($this->arguments[$name]);
98
    }
99
}
100