Passed
Branch main (a3ac80)
by Frank
02:08
created

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