Passed
Push — dev ( 0679fe...0efacf )
by Fike
05:23
created

ActiveNode::setChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
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
            $value,
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 2
    public function enumerate()
93
    {
94 2
        $target = [];
95 2
        $children = $this->accessor->enumerate($this->getValue(), []);
96 1
        foreach ($children as $key => $child) {
97 1
            $target[$key] = new ActiveNode(
98 1
                $this->accessor,
99 1
                array_merge($this->getPath(), [$key]),
100 1
                $child->getValue(),
101 1
                $child->isReadable(),
102 1
                $child->isWritable(),
103 1
                $this
104
            );
105
        }
106 1
        return $target;
107
    }
108
}
109