Passed
Push — main ( 42b4d9...c70814 )
by Frank
01:56
created

Element::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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