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