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