Passed
Pull Request — master (#145)
by Christoffer
03:59 queued 01:32
created

AbstractNodeBuilder::buildOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 2
1
<?php
2
3
namespace Digia\GraphQL\Language\NodeBuilder;
4
5
use Digia\GraphQL\Language\Location;
6
7
abstract class AbstractNodeBuilder implements NodeBuilderInterface
8
{
9
    /**
10
     * @var NodeDirectorInterface
11
     */
12
    protected $director;
13
14
    /**
15
     * @param NodeDirectorInterface $director
16
     * @return $this
17
     */
18
    public function setDirector(NodeDirectorInterface $director)
19
    {
20
        $this->director = $director;
21
        return $this;
22
    }
23
24
    /**
25
     * Creates a location object.
26
     *
27
     * @param array $ast
28
     * @return Location|null
29
     */
30
    protected function createLocation(array $ast): ?Location
31
    {
32
        return isset($ast['loc']['start'], $ast['loc']['end'])
33
            ? new Location($ast['loc']['start'], $ast['loc']['end'], $ast['loc']['source'] ?? null)
34
            : null;
35
    }
36
37
    /**
38
     * Returns the value of a single property in the given AST.
39
     *
40
     * @param array  $ast
41
     * @param string $propertyName
42
     * @param null   $defaultValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $defaultValue is correct as it would always require null to be passed?
Loading history...
43
     * @return mixed|null
44
     */
45
    protected function get(array $ast, string $propertyName, $defaultValue = null)
46
    {
47
        return $ast[$propertyName] ?? $defaultValue;
48
    }
49
50
    /**
51
     * Builds a single item from the given AST.
52
     *
53
     * @param array  $ast
54
     * @param string $propertyName
55
     * @return mixed|null
56
     */
57
    protected function buildOne(array $ast, string $propertyName)
58
    {
59
        return isset($ast[$propertyName]) ? $this->director->build($ast[$propertyName]) : null;
60
    }
61
62
    /**
63
     * Builds many items from the given AST.
64
     *
65
     * @param array  $ast
66
     * @param string $propertyName
67
     * @return array
68
     */
69
    protected function buildMany(array $ast, string $propertyName): array
70
    {
71
        $array = [];
72
73
        if (isset($ast[$propertyName])) {
74
            foreach ($ast[$propertyName] as $subAst) {
75
                $array[] = $this->director->build($subAst);
76
            }
77
        }
78
79
        return $array;
80
    }
81
}
82