Skip to content

Commit e75b91b

Browse files
authored
Merge pull request #1 from Piagrammist/main
Code improvements Convert HtmlClass __construct from array to variadic
2 parents 9bb2c0d + c7f10d4 commit e75b91b

File tree

12 files changed

+105
-66
lines changed

12 files changed

+105
-66
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ phar5
3535

3636
# Vscode
3737
.vscode/*
38-
.vscode
38+
!.vscode/settings.json
39+
!.vscode/extensions.json
3940
.php_cs.cache
4041
.phpdoc_cache
4142
*bak
@@ -49,4 +50,4 @@ config.json
4950
/Caddyfile
5051
/changelog
5152

52-
# Project
53+
# Project

examples/1- SimpleMaker.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22

3-
include 'vendor/autoload.php';
3+
require __DIR__ . '/../vendor/autoload.php';
44

55
use AhjDev\PhpTagMaker\TagMaker;
66
use AhjDev\PhpTagMaker\HtmlClass;
77
use AhjDev\PhpTagMaker\Node\HtmlTag;
88

99
$output = TagMaker::build(
1010
HtmlTag::div(
11-
'My Class Name',
11+
'my-class-name',
1212
HtmlTag::pre('A Pre Tag'),
1313
HtmlTag::div(
14-
new HtmlClass(['Class 1', 'Class 2'])
14+
new HtmlClass('class-1', 'class-2')
1515
)
1616
)
1717
);
18-
print($output);
18+
print($output);

examples/2- FormatOutput.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
include 'vendor/autoload.php';
3+
require __DIR__ . '/../vendor/autoload.php';
44

55
use AhjDev\PhpTagMaker\TagMaker;
66
use AhjDev\PhpTagMaker\Node\HtmlTag;
@@ -9,25 +9,27 @@
99
use AhjDev\PhpTagMaker\Node\HtmlTagMulti;
1010

1111
$maker = new TagMaker();
12-
$output = $maker->formatOutput(true)->run(
13-
HtmlTag::span(
14-
HtmlTag::a(
15-
'github.com/ahjdev',
16-
'My Github',
17-
),
18-
'Can also use a string',
19-
HtmlTag::ul(
20-
HtmlTag::li('one'),
21-
HtmlTag::li('two'),
22-
HtmlTag::li('three')->setClass('Class 1', 'Class 2'),
23-
),
24-
HtmlText::make('<a> without escape'),
25-
HtmlTag::br(),
26-
EscapedText::make('<a> with escape'),
27-
HtmlTag::br(),
28-
HtmlTagMulti::make('Multi tag', ['a', 'b', 'code']),
29-
)
30-
);
12+
$output = $maker
13+
->formatOutput()
14+
->run(
15+
HtmlTag::span(
16+
HtmlTag::a(
17+
'github.com/ahjdev',
18+
'My Github',
19+
),
20+
'Can also use a string',
21+
HtmlTag::ul(
22+
HtmlTag::li('one'),
23+
HtmlTag::li('two'),
24+
HtmlTag::li('three')->setClass('Class 1', 'Class 2'),
25+
),
26+
HtmlText::make('<a> without escape'),
27+
HtmlTag::br(),
28+
EscapedText::make('<a> with escape'),
29+
HtmlTag::br(),
30+
HtmlTagMulti::make('Multi tag', ['a', 'b', 'code']),
31+
)
32+
);
3133
print($output);
3234

3335
$myTag = HtmlTag::make('myTag', 'myValue');
@@ -53,8 +55,8 @@
5355
$myTag->setName('h1');
5456

5557
// Iterate attributes
56-
foreach ($myTag->getAttributes() as $attr) {
58+
foreach ($myTag->iterAttributes() as $attr) {
5759
// $attr is instanceof DOMAttr
5860
// var_dump($attr);
5961
}
60-
print($maker->run($myTag));
62+
print($maker->run($myTag));

src/HtmlClass.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,66 @@
77

88
final class HtmlClass implements Stringable, IteratorAggregate
99
{
10-
public function __construct(private array $class)
10+
private array $classList = [];
11+
12+
public function __construct(string ...$classes)
13+
{
14+
$this->classList = array_map(static fn ($e) => trim($e), $classes);
15+
}
16+
17+
public function __toString(): string
1118
{
19+
return implode(' ', $this->classList);
1220
}
1321

14-
public function __tostring()
22+
public function has(string $class): bool
1523
{
16-
return implode(' ', $this->class);
24+
return in_array(trim($class), $this->classList, true);
1725
}
1826

19-
public function hasClass(string $class)
27+
public function add(string $class): self
2028
{
21-
return in_array($class, $this->class);
29+
$class = trim($class);
30+
if (!(empty($class) || $this->has($class))) {
31+
$this->classList[] = $class;
32+
}
33+
return $this;
2234
}
2335

24-
public function addClass(string $class)
36+
public function remove(string $class): self
2537
{
26-
if (!$this->hasClass($class))
27-
$this->class[] = $class;
38+
if (($pos = array_search(trim($class), $this->classList, true)) !== false) {
39+
unset($this->classList[$pos]);
40+
}
2841
return $this;
2942
}
3043

31-
public function removeClass(string $class)
44+
public function merge(string|self ...$classes): self
3245
{
33-
if (($s = array_search($class, $this->class)) !== false)
34-
unset($this->class[$s]);
46+
foreach ($classes as $class) {
47+
if ($class instanceof self) {
48+
$this->classList = array_merge($this->classList, $class->asArray());
49+
array_shift($classes);
50+
}
51+
}
52+
// leftover strings
53+
if ($classes) {
54+
$this->classList = array_merge(
55+
$this->classList,
56+
array_map(static fn ($e) => trim($e), $classes)
57+
);
58+
}
59+
$this->classList = array_filter($this->classList);
3560
return $this;
3661
}
3762

63+
public function asArray(): array
64+
{
65+
return $this->classList;
66+
}
67+
3868
public function getIterator(): \Generator
3969
{
40-
return yield from $this->class;
70+
return yield from $this->classList;
4171
}
4272
}

src/Node/EscapedText.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(string $text)
1414
$this->text = new DOMCdataSection($text);
1515
}
1616

17-
public static function make($text)
17+
public static function make($text): self
1818
{
19-
return new static($text);
19+
return new self($text);
2020
}
2121

2222
public function toDomNode(): DOMCdataSection

src/Node/HtmlTag.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
use DOMElement;
66
use AhjDev\PhpTagMaker\Node;
7+
use AhjDev\PhpTagMaker\HtmlClass;
78

89
final class HtmlTag extends Node
910
{
1011
use Internal\Attributes;
1112
use Internal\DefaultTags;
1213

14+
private array $values = [];
1315
private DOMElement $domElement;
14-
private array $values = [];
16+
public readonly HtmlClass $class;
1517

1618
public function __construct(private string $tag, Node|string ...$value)
1719
{
1820
$this->domElement = new DOMElement($tag);
1921
$this->values = array_map(static fn ($v) => is_string($v) ? new HtmlText($v) : $v, $value);
22+
$this->class = new HtmlClass;
2023
// $this->domElement->getElementsByTagName();
2124
// $this->domElement->insertAdjacentElement();
2225
// $this->domElement->insertAdjacentText();
@@ -26,7 +29,7 @@ public function __construct(private string $tag, Node|string ...$value)
2629

2730
public static function make(string $tag, Node|string ...$value): self
2831
{
29-
return new static($tag, ...$value);
32+
return new self($tag, ...$value);
3033
}
3134

3235
public function getName()
@@ -54,8 +57,9 @@ public function setName(string $tag): self
5457
public function toDomNode(): DOMElement
5558
{
5659
$element = $this->domElement->cloneNode(true);
60+
$element->setAttribute('class', (string)$this->class);
5761
array_map(
58-
fn (Node $v) => $element->append(
62+
static fn (Node $v) => $element->append(
5963
$v->toDomNode()
6064
),
6165
$this->values

src/Node/HtmlTagMulti.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public function __construct(private string $text, private array $tags)
1414
{
1515
}
1616

17-
public static function make(string $text, array $tags)
17+
public static function make(string $text, array $tags): self
1818
{
19-
return new static($text, $tags);
19+
return new self($text, $tags);
2020
}
2121

22-
public function getIterator(): \Traversable
22+
public function getIterator(): \Generator
2323
{
2424
return yield from $this->tags;
2525
}

src/Node/HtmlText.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public function __construct(string $text)
1414
$this->domText = new DOMText($text);
1515
}
1616

17-
public static function make($text)
17+
public static function make($text): self
1818
{
19-
return new static($text);
19+
return new self($text);
2020
}
2121

2222
public function toDomNode(): DOMText

src/Node/Internal/Attributes.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@
1414
*/
1515
trait Attributes
1616
{
17-
public function setClass(string ...$class): self
17+
public function setClass(string ...$classes): self
1818
{
19-
$class = new HtmlClass($class);
20-
return $this->setAttribute('class', (string) $class);
19+
return $this->setAttribute('class', (string)(new HtmlClass(...$classes)));
2120
}
2221

2322
public function getClass(): null|string|array
2423
{
25-
if ($attribute = $this->getAttribute('class'))
26-
{
24+
if ($attribute = $this->getAttribute('class')) {
2725
$attribute = explode(' ', $attribute);
2826
return count($attribute) === 1 ? $attribute[0] : $attribute;
2927
}
3028
return null;
3129
}
3230

33-
public function setId(string $id)
31+
public function setId(string $id): self
3432
{
3533
return $this->setAttribute('id', $id);
3634
}
@@ -65,7 +63,7 @@ public function getAttribute(string $qualifiedName): ?string
6563
/**
6664
* @return ArrayIterator<DOMAttr>
6765
*/
68-
public function getAttributes(): Iterator
66+
public function iterAttributes(): Iterator
6967
{
7068
return new ArrayIterator(
7169
array_map(

0 commit comments

Comments
 (0)