Completed
Push — master ( 15a7c5...a9adfd )
by Kevin
02:15
created

Element::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
class Element extends AbstractToken
6
{
7
    /** @var array */
8
    private $attributes;
9
10
    /** @var array[Token] */
11
    private $children;
12
13
    /** @var string */
14
    private $name;
15
16
    /**
17
     * Constructor
18
     */
19
    public function __construct(Token $parent = null, $name = null, array $attributes = array())
20
    {
21
        parent::__construct(Token::ELEMENT, $parent);
22
23
        $this->attributes = $attributes;
24
        $this->children = array();
25
        $this->setName($name);
26
    }
27
28
    /**
29
     * Getter for 'attributes'.
30
     */
31
    public function getAttributes()
32
    {
33
        return $this->attributes;
34
    }
35
36
    public function addAttribute($key, $value)
37
    {
38
        $this->attributes[$key] = $value;
39
40
        return $this;
41
    }
42
43
    public function removeAttribute($key)
44
    {
45
        if (isset($this->attributes[$key])) {
46
            unset($this->attributes[$key]);
47
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * Getter for 'children'.
56
     */
57
    public function getChildren()
58
    {
59
        return $this->children;
60
    }
61
62
    public function addChild(Token $token)
63
    {
64
        $this->children[] = $token;
65
66
        return $this;
67
    }
68
69
    public function removeChild(Token $token)
70
    {
71
        $key = array_search($token, $this->children);
72
        if ($key !== false) {
73
            unset($this->children[$key]);
74
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Getter for 'name'.
83
     */
84
    public function getName()
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * Chainable setter for 'name'.
91
     */
92
    public function setName($name)
93
    {
94
        if (!is_string($name)) {
95
            throw new \InvalidArgumentException('Element name must be string type.');
96
        }
97
98
        $this->name = $name;
99
100
        return $this;
101
    }
102
103
    public function toString($prefix = '', $suffix = '')
104
    {
105
        $output = $prefix . '<' . $this->name;
106
        foreach ($this->attributes as $key => $value) {
107
            $output .= ' ' . $key;
108
            if (is_string($value)) {
109
                $output .= '="' . $value . '"';
110
            }
111
        }
112
113
        if (empty($this->children)) {
114
            $output .= '/>';
115
        } else {
116
            $output .= '>';
117
        }
118
119
        return $output . $suffix;
120
    }
121
}
122