ActiveNode::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\TreeAccess;
4
5
use AmaTeam\TreeAccess\API\AccessorInterface;
6
use AmaTeam\TreeAccess\API\ActiveNodeInterface;
7
8
class ActiveNode extends Node implements ActiveNodeInterface
9
{
10
    /**
11
     * @var AccessorInterface
12
     */
13
    private $accessor;
14
    /**
15
     * @var ActiveNodeInterface|null
16
     */
17
    private $parent;
18
19
    /**
20
     * @param AccessorInterface $accessor
21
     * @param string[] $path
22
     * @param mixed $value
23
     * @param bool $readable
24
     * @param bool $writable
25
     * @param ActiveNodeInterface|null $parent
26
     *
27
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
28
     */
29 4
    public function __construct(
30
        AccessorInterface $accessor,
31
        array $path,
32
        &$value,
33
        $readable = true,
34
        $writable = true,
35
        ActiveNodeInterface $parent = null
36
    ) {
37 4
        parent::__construct($path, $value, $readable, $writable);
38 4
        $this->accessor = $accessor;
39 4
        $this->parent = $parent;
40 4
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 3
    public function setValue(&$value)
46
    {
47 3
        if ($this->parent) {
48 2
            $target = &$this->parent->getValue();
49 2
            $this->accessor->write($target, [$this->getKey()], $value);
50 2
            return $this;
51
        }
52 1
        $this->accessor->write($this->getValue(), [], $value);
53 1
        return $this;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 1
    public function setChild($key, $value)
60
    {
61 1
        $this->accessor->write($this->getValue(), [$key], $value);
62 1
        $node = $this->accessor->getNode($this->getValue(), [$key]);
63 1
        return new ActiveNode(
64 1
            $this->accessor,
65 1
            array_merge($this->getPath(), [$key]),
66 1
            $node->getValue(),
67 1
            $node->isReadable(),
68 1
            $node->isWritable(),
69 1
            $this
70
        );
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function getChild($key)
77
    {
78 1
        $node = $this->accessor->getNode($this->getValue(), [$key]);
79 1
        return new ActiveNode(
80 1
            $this->accessor,
81 1
            array_merge($this->getPath(), [$key]),
82 1
            $node->getValue(),
83 1
            $node->isReadable(),
84 1
            $node->isWritable(),
85 1
            $this
86
        );
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    public function getParent()
93
    {
94
        return $this->parent;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 2
    public function enumerate()
101
    {
102 2
        $target = [];
103 2
        $children = $this->accessor->enumerate($this->getValue(), []);
104 1
        foreach ($children as $key => $child) {
105 1
            $target[$key] = new ActiveNode(
106 1
                $this->accessor,
107 1
                array_merge($this->getPath(), [$key]),
108 1
                $child->getValue(),
109 1
                $child->isReadable(),
110 1
                $child->isWritable(),
111 1
                $this
112
            );
113
        }
114 1
        return $target;
115
    }
116
}
117