Passed
Push — master ( b48009...a47442 )
by Pol
02:39
created

AttributeNode::setAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\phptree\Node;
6
7
use loophp\phptree\Traverser\TraverserInterface;
8
9
/**
10
 * Class AttributeNode.
11
 */
12
class AttributeNode extends NaryNode implements AttributeNodeInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $attributes = [];
18
19
    /**
20
     * ValueNode constructor.
21
     *
22
     * @param array<int|string, mixed> $attributes
23
     * @param int|null $capacity
24
     * @param TraverserInterface|null $traverser
25
     * @param NodeInterface|null $parent
26
     */
27 10
    public function __construct(
28
        array $attributes = [],
29
        ?int $capacity = 0,
30
        ?TraverserInterface $traverser = null,
31
        ?NodeInterface $parent = null
32
    ) {
33 10
        parent::__construct($capacity, $traverser, $parent);
34
35 10
        $this->attributes = $attributes;
36 10
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 4
    public function getAttribute(string $key)
42
    {
43 4
        return $this->getAttributes()[$key] ?? null;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 4
    public function getAttributes(): array
50
    {
51 4
        return $this->attributes;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function label(): string
58
    {
59 2
        return (string) $this->getAttribute('label');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function setAttribute(string $key, $value): AttributeNodeInterface
66
    {
67 2
        $this->attributes[$key] = $value;
68
69 2
        return $this;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function setAttributes(array $attributes): AttributeNodeInterface
76
    {
77 1
        $this->attributes = $attributes;
78
79 1
        return $this;
80
    }
81
}
82