Skip to content

Commit 5f17a7e

Browse files
committed
Make HtmlTagMulti varidiac
1 parent ea90d8a commit 5f17a7e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

examples/2- FormatOutput.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
HtmlTag::br(),
2828
EscapedText::make('<a> with escape'),
2929
HtmlTag::br(),
30-
HtmlTagMulti::make('Multi tag', ['a', 'b', 'code']),
30+
HtmlTagMulti::make(['a', 'b', 'code'], 'Multi tag'),
31+
HtmlTag::br(),
32+
HtmlTagMulti::make(
33+
['c', 'r', 'y'],
34+
HtmlTag::b('inside tag'),
35+
' ',
36+
'Just text'
37+
)
3138
)
3239
);
3340
print($output);

src/Node/HtmlTagMulti.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
*/
1111
final class HtmlTagMulti extends Node implements IteratorAggregate
1212
{
13-
public function __construct(private string $text, private array $tags)
13+
/** @var list<Node> */
14+
private array $values = [];
15+
16+
public function __construct(private array $tags, Node|string ...$value)
1417
{
18+
$this->values = array_map(static fn ($v) => is_string($v) ? new HtmlText($v) : $v, $value);
1519
}
1620

17-
public static function make(string $text, array $tags): self
21+
public static function make(array $tags, Node|string ...$value): self
1822
{
19-
return new self($text, $tags);
23+
return new self($tags, ...$value);
2024
}
2125

2226
public function getIterator(): \Generator
@@ -26,10 +30,10 @@ public function getIterator(): \Generator
2630

2731
public function toDomNode(): \DomNode
2832
{
29-
$before = $this->text;
33+
$before = $this->values;
3034
foreach (array_reverse($this->tags) as $tag) {
31-
$tag = new HtmlTag($tag, $before);
32-
$before = $tag;
35+
$tag = new HtmlTag($tag, ...$before);
36+
$before = [$tag];
3337
// foreach ($attributes as $name => $value)
3438
// $tag->setAttribute($name, $value);
3539
}

0 commit comments

Comments
 (0)