1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PrinsFrank\PhpGeoSVG\Html\Elements; |
6
|
|
|
|
7
|
|
|
use PrinsFrank\PhpGeoSVG\Html\Elements\Definition\ForeignElement; |
8
|
|
|
use PrinsFrank\PhpGeoSVG\Html\Elements\Text\TextContent; |
9
|
|
|
|
10
|
|
|
abstract class Element |
11
|
|
|
{ |
12
|
|
|
/** @var Element[] */ |
13
|
|
|
protected array $childElements = []; |
14
|
|
|
|
15
|
|
|
/** @var array<string, string> */ |
16
|
|
|
protected array $attributes = []; |
17
|
|
|
private ?TextContent $textContent = null; |
18
|
|
|
|
19
|
1 |
|
public function addChildElement(Element $childElement): self |
20
|
|
|
{ |
21
|
1 |
|
$this->childElements[] = $childElement; |
22
|
|
|
|
23
|
1 |
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return Element[] |
28
|
|
|
*/ |
29
|
1 |
|
public function getChildElements(): array |
30
|
|
|
{ |
31
|
1 |
|
return $this->childElements; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return array<string, string> |
36
|
|
|
*/ |
37
|
1 |
|
public function getAttributes(): array |
38
|
|
|
{ |
39
|
1 |
|
return $this->attributes; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function setAttribute(string $name, mixed $value): self |
43
|
|
|
{ |
44
|
1 |
|
$this->attributes[$name] = (string) $value; |
45
|
|
|
|
46
|
1 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function setTextContent(?TextContent $textContent): self |
50
|
|
|
{ |
51
|
1 |
|
$this->textContent = $textContent; |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function getTextContent(): ?TextContent |
57
|
|
|
{ |
58
|
1 |
|
return $this->textContent; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see https://html.spec.whatwg.org/#self-closing-start-tag-state |
63
|
|
|
* |
64
|
|
|
* Foreign elements must either have a start tag and an end tag, or a start tag that is marked as self-closing, |
65
|
|
|
* in which case they must not have an end tag. |
66
|
|
|
*/ |
67
|
4 |
|
public function canSelfClose(): bool |
68
|
|
|
{ |
69
|
4 |
|
if (false === $this instanceof ForeignElement) { |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
if ([] !== $this->childElements) { |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
if (null !== $this->textContent) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
abstract public function getTagName(): string; |
85
|
|
|
} |
86
|
|
|
|