1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Tag; |
4
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
6
|
|
|
use drupol\htmltag\Attributes\AttributesInterface; |
7
|
|
|
use drupol\htmltag\StringableInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Tag. |
11
|
|
|
*/ |
12
|
|
|
class Tag extends AbstractBaseHtmlTagObject implements TagInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The tag name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $tag; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The tag attributes. |
23
|
|
|
* |
24
|
|
|
* @var \drupol\htmltag\Attributes\AttributesInterface |
25
|
|
|
*/ |
26
|
|
|
private $attributes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The tag content. |
30
|
|
|
* |
31
|
|
|
* @var mixed[]|null |
32
|
|
|
*/ |
33
|
|
|
private $content; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tag constructor. |
37
|
|
|
* |
38
|
|
|
* @param \drupol\htmltag\Attributes\AttributesInterface $attributes |
39
|
|
|
* The attributes object. |
40
|
|
|
* @param string $name |
41
|
|
|
* The tag name. |
42
|
|
|
* @param mixed $content |
43
|
|
|
* The content. |
44
|
|
|
*/ |
45
|
14 |
|
public function __construct(AttributesInterface $attributes, $name, $content = null) |
46
|
|
|
{ |
47
|
14 |
|
$this->tag = $name; |
48
|
14 |
|
$this->attributes = $attributes; |
49
|
14 |
|
$this->content($content); |
50
|
14 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param array $arguments |
55
|
|
|
* |
56
|
|
|
* @return \drupol\htmltag\Tag\TagInterface |
57
|
|
|
*/ |
58
|
1 |
|
public static function __callStatic($name, array $arguments = []) |
59
|
|
|
{ |
60
|
1 |
|
return new static($arguments[0], $name); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
2 |
|
public function __toString() |
67
|
|
|
{ |
68
|
2 |
|
return $this->render(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
3 |
|
public function attr($name = null, ...$value) |
75
|
|
|
{ |
76
|
3 |
|
if (null == $name) { |
|
|
|
|
77
|
1 |
|
return $this->attributes->render(); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
if ([] == $value) { |
81
|
2 |
|
return $this->attributes[$name]; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this->attributes[$name]->set($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
14 |
|
public function content(...$data) |
91
|
|
|
{ |
92
|
14 |
|
if ([] != $data) { |
93
|
14 |
|
if (\reset($data) === null) { |
94
|
11 |
|
$data = null; |
95
|
|
|
} |
96
|
|
|
|
97
|
14 |
|
$this->content = $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
14 |
|
return $this->renderContent(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
10 |
|
public function render() |
107
|
|
|
{ |
108
|
10 |
|
return [] == $this->content ? |
109
|
6 |
|
\sprintf('<%s%s/>', $this->tag, $this->attributes->render()): |
110
|
10 |
|
\sprintf('<%s%s>%s</%s>', $this->tag, $this->attributes->render(), $this->renderContent(), $this->tag); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Render the tag content. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
14 |
|
protected function renderContent() |
119
|
|
|
{ |
120
|
14 |
|
return \implode( |
121
|
14 |
|
'', |
122
|
14 |
|
$this->escapeValues( |
123
|
14 |
|
$this->preprocess( |
124
|
14 |
|
$this->ensureFlatArray((array) $this->content) |
125
|
|
|
) |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
1 |
|
public function serialize() |
134
|
|
|
{ |
135
|
1 |
|
return \serialize([ |
136
|
1 |
|
'tag' => $this->tag, |
137
|
1 |
|
'attributes' => $this->attributes->getValuesAsArray(), |
138
|
1 |
|
'content' => $this->renderContent(), |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
1 |
|
public function unserialize($serialized) |
146
|
|
|
{ |
147
|
1 |
|
$unserialize = \unserialize($serialized); |
148
|
|
|
|
149
|
1 |
|
$this->tag = $unserialize['tag']; |
150
|
1 |
|
$this->attributes = $this->attributes->import($unserialize['attributes']); |
151
|
1 |
|
$this->content = $unserialize['content']; |
152
|
1 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Alter the values of an object. |
156
|
|
|
* |
157
|
|
|
* @param callable ...$closures |
158
|
|
|
* A closure. |
159
|
|
|
* |
160
|
|
|
* @return object |
161
|
|
|
* The object. |
162
|
|
|
*/ |
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
1 |
|
public function alter(callable ...$closures) |
167
|
|
|
{ |
168
|
1 |
|
foreach ($closures as $closure) { |
169
|
1 |
|
$this->content = $closure( |
170
|
1 |
|
$this->ensureFlatArray((array) $this->content) |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
10 |
|
protected function escape($value) |
181
|
|
|
{ |
182
|
10 |
|
$return = $this->ensureString($value); |
183
|
|
|
|
184
|
10 |
|
if ($value instanceof StringableInterface) { |
185
|
4 |
|
return $return; |
186
|
|
|
} |
187
|
|
|
|
188
|
10 |
|
return null == $return ? |
|
|
|
|
189
|
3 |
|
$return: |
190
|
10 |
|
\htmlentities($return); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array|\drupol\htmltag\Attribute\AttributeInterface[] |
195
|
|
|
*/ |
196
|
1 |
|
public function getContentAsArray() |
197
|
|
|
{ |
198
|
1 |
|
return $this->preprocess( |
199
|
1 |
|
$this->ensureFlatArray((array) $this->content) |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|