Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.32% covered (success)
90.32%
28 / 31
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
HtmlElementBase
90.32% covered (success)
90.32%
28 / 31
62.50% covered (warning)
62.50%
5 / 8
15.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createTagElement
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 createText
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 createComment
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 appendChild
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 addTagElement
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addComment
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addText
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 path
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Html;
6
7use DOMNode;
8use DOMException;
9use PeServer\Core\Html\HtmlDocument;
10use PeServer\Core\Html\HtmlNodeBase;
11use PeServer\Core\Throws\HtmlException;
12use PeServer\Core\Throws\HtmlTagElementException;
13use PeServer\Core\Throws\Throws;
14
15abstract class HtmlElementBase extends HtmlNodeBase
16{
17    protected function __construct(HtmlDocument $document, DOMNode $current)
18    {
19        parent::__construct($document, $current);
20    }
21
22    #region function
23
24    public function createTagElement(string $tagName): HtmlTagElement
25    {
26        try {
27            $element = $this->document->raw->createElement($tagName);
28        } catch (DOMException $ex) {
29            Throws::reThrow(HtmlException::class, $ex);
30        }
31
32        if ($element === false) { // @phpstan-ignore-line
33            throw new HtmlException();
34        }
35
36        return new HtmlTagElement($this->document, $element);
37    }
38
39    public function createText(string $text): HtmlTextElement
40    {
41        $node = $this->document->raw->createTextNode($text);
42        if ($node === false) { // @phpstan-ignore-line
43            throw new HtmlException();
44        }
45
46        return new HtmlTextElement($this->document, $node);
47    }
48
49    public function createComment(string $text): HtmlCommentElement
50    {
51        $node = $this->document->raw->createComment($text);
52        if ($node === false) { // @phpstan-ignore-line
53            throw new HtmlException();
54        }
55
56        return new HtmlCommentElement($this->document, $node);
57    }
58
59    public function appendChild(HtmlTagElement|HtmlTextElement|HtmlCommentElement|DOMNode $node): void
60    {
61        if ($node instanceof HtmlTagElement) {
62            $node = $node->raw;
63        } elseif ($node instanceof HtmlTextElement) {
64            $node = $node->raw;
65        } elseif ($node instanceof HtmlCommentElement) {
66            $node = $node->raw;
67        }
68
69        $this->currentNode->appendChild($node);
70    }
71
72    /**
73     * HTML要素を作って追加する。
74     *
75     * @param string $tagName
76     * @return HtmlTagElement
77     */
78    public function addTagElement(string $tagName): HtmlTagElement
79    {
80        $node = $this->createTagElement($tagName);
81
82        $this->appendChild($node);
83
84        return $node;
85    }
86
87    public function addComment(string $comment): HtmlCommentElement
88    {
89        $node = $this->createComment($comment);
90
91        $this->appendChild($node);
92
93        return $node;
94    }
95
96    public function addText(string $text): HtmlTextElement
97    {
98        $node = $this->createText($text);
99
100        $this->appendChild($node);
101
102        return $node;
103    }
104
105    abstract public function path(): HtmlXPath;
106
107    #endregion
108}