|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drupol\htmltag\Tag; |
|
4
|
|
|
|
|
5
|
|
|
use drupol\htmltag\AbstractBaseHtmlTagObject; |
|
6
|
|
|
use drupol\htmltag\Attributes\AttributesInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Tag. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Tag extends AbstractBaseHtmlTagObject implements TagInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The tag name. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $tag; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The tag attributes. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \drupol\htmltag\Attributes\AttributesInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $attributes; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The tag content. |
|
29
|
|
|
* |
|
30
|
|
|
* @var mixed[]|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $content; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Tag constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \drupol\htmltag\Attributes\AttributesInterface $attributes |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* The tag name. |
|
40
|
|
|
* @param mixed $content |
|
41
|
|
|
* The content. |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function __construct(AttributesInterface $attributes, $name, $content = false) |
|
44
|
|
|
{ |
|
45
|
6 |
|
$this->tag = $name; |
|
46
|
6 |
|
$this->attributes = $attributes; |
|
47
|
6 |
|
$this->content($content); |
|
48
|
6 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param array $arguments |
|
53
|
|
|
* |
|
54
|
|
|
* @return \drupol\htmltag\Tag\TagInterface |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public static function __callStatic($name, array $arguments = []) |
|
57
|
|
|
{ |
|
58
|
1 |
|
return new static($arguments[0], $name); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function __toString() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->render(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function attr($name = null, ...$value) |
|
73
|
|
|
{ |
|
74
|
2 |
|
if (null === $name) { |
|
75
|
1 |
|
return $this->attributes->render(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
if ([] === $value) { |
|
79
|
2 |
|
return $this->attributes[$name]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->attributes[$name]->set($value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
6 |
|
public function content(...$content) |
|
89
|
|
|
{ |
|
90
|
6 |
|
if ([] !== $content) { |
|
91
|
6 |
|
if (\reset($content) === false) { |
|
92
|
6 |
|
$content = null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
$this->content = $content; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
return $this->renderContent(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
5 |
|
public function render() |
|
105
|
|
|
{ |
|
106
|
5 |
|
$output = sprintf('<%s%s', $this->tag, $this->attributes); |
|
107
|
|
|
|
|
108
|
5 |
|
$output .= null === $this->content ? |
|
109
|
5 |
|
'/>': |
|
110
|
5 |
|
sprintf('>%s</%s>', $this->renderContent(), $this->tag); |
|
111
|
|
|
|
|
112
|
5 |
|
return $output; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Render the tag content. |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
6 |
|
private function renderContent() |
|
121
|
|
|
{ |
|
122
|
6 |
|
return \implode( |
|
123
|
6 |
|
'', |
|
124
|
6 |
|
\array_filter( |
|
125
|
6 |
|
\array_map( |
|
126
|
|
|
function ($content_item) { |
|
127
|
6 |
|
$output = ''; |
|
128
|
|
|
|
|
129
|
|
|
// Make sure we can 'stringify' the item. |
|
130
|
6 |
|
if (!\is_array($content_item) && |
|
131
|
6 |
|
((!\is_object($content_item) && \settype($content_item, 'string') !== false) || |
|
132
|
6 |
|
(\is_object($content_item) && \method_exists($content_item, '__toString')))) { |
|
133
|
6 |
|
$output = (string) $content_item; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
6 |
|
return $output; |
|
137
|
6 |
|
}, |
|
138
|
6 |
|
$this->ensureFlatArray($this->ensureArray($this->content)) |
|
139
|
|
|
), |
|
140
|
6 |
|
'\strlen' |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|