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

AttributeNode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 68
ccs 16
cts 16
cp 1
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getAttribute() 0 3 1
A getAttributes() 0 3 1
A setAttribute() 0 5 1
A setAttributes() 0 5 1
A label() 0 3 1
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