-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightElementNode.php
More file actions
69 lines (54 loc) · 1.98 KB
/
Copy pathLightElementNode.php
File metadata and controls
69 lines (54 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
require_once 'LightNode.php';
require_once 'EventListenerInterface.php';
class LightElementNode extends LightNode {
private string $tagName;
private string $displayType;
private bool $isSelfClosing;
private array $cssClasses = [];
private array $children = [];
private array $listeners = [];
public function __construct(string $tagName, string $displayType = 'block', bool $isSelfClosing = false, array $cssClasses = []) {
$this->tagName = $tagName;
$this->displayType = $displayType;
$this->isSelfClosing = $isSelfClosing;
$this->cssClasses = $cssClasses;
}
public function getTagName(): string {
return $this->tagName;
}
public function addChild(LightNode $node): void {
$this->children[] = $node;
}
public function getChildrenCount(): int {
return count($this->children);
}
public function addEventListener(string $eventType, EventListenerInterface $listener): void {
if (!isset($this->listeners[$eventType])) {
$this->listeners[$eventType] = [];
}
$this->listeners[$eventType][] = $listener;
}
public function triggerEvent(string $eventType): void {
if (isset($this->listeners[$eventType])) {
foreach ($this->listeners[$eventType] as $listener) {
$listener->update($eventType, $this);
}
}
}
public function renderInnerHTML(): string {
$html = '';
foreach ($this->children as $child) {
$html .= $child->renderOuterHTML();
}
return $html;
}
public function renderOuterHTML(): string {
$classesStr = !empty($this->cssClasses) ? ' class="' . implode(' ', $this->cssClasses) . '"' : '';
if ($this->isSelfClosing) {
return "<{$this->tagName}{$classesStr} />";
}
$innerHTML = $this->renderInnerHTML();
return "<{$this->tagName}{$classesStr}>{$innerHTML}</{$this->tagName}>";
}
}