AstArgumentsTrait::getKeyValueArguments()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 2/5/17 11:31 AM
7
*/
8
9
namespace Youshido\GraphQL\Parser\Ast;
10
11
12
trait AstArgumentsTrait
13
{
14
15
    /** @var Argument[] */
16
    protected $arguments;
17
18
    private $argumentsCache = null;
19
20
21 3
    public function hasArguments()
22
    {
23 3
        return (bool)count($this->arguments);
24
    }
25
26
    public function hasArgument($name)
27
    {
28
        return array_key_exists($name, $this->arguments);
29
    }
30
31
    /**
32
     * @return Argument[]
33
     */
34 70
    public function getArguments()
35
    {
36 70
        return $this->arguments;
37
    }
38
39
    /**
40
     * @param $name
41
     *
42
     * @return null|Argument
43
     */
44 1
    public function getArgument($name)
45
    {
46 1
        $argument = null;
47 1
        if (isset($this->arguments[$name])) {
48 1
            $argument = $this->arguments[$name];
49 1
        }
50
51 1
        return $argument;
52
    }
53
54 1
    public function getArgumentValue($name)
55
    {
56 1
        $argument = $this->getArgument($name);
57
58 1
        return $argument ? $argument->getValue()->getValue() : null;
59
    }
60
61
    /**
62
     * @param $arguments Argument[]
63
     */
64 99
    public function setArguments(array $arguments)
65
    {
66 99
        $this->arguments = [];
67 99
        $this->argumentsCache = null;
68
69 99
        foreach ($arguments as $argument) {
70 54
            $this->addArgument($argument);
71 99
        }
72 99
    }
73
74 55
    public function addArgument(Argument $argument)
75
    {
76 55
        $this->arguments[$argument->getName()] = $argument;
77 55
    }
78
79 4
    public function getKeyValueArguments()
80
    {
81 4
        if ($this->argumentsCache !== null) {
82 1
            return $this->argumentsCache;
83
        }
84
85 4
        $this->argumentsCache = [];
86
87 4
        foreach ($this->getArguments() as $argument) {
88 4
            $this->argumentsCache[$argument->getName()] = $argument->getValue()->getValue();
89 4
        }
90
91 4
        return $this->argumentsCache;
92
    }
93
}