-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathIsElement.php
More file actions
181 lines (133 loc) · 4.05 KB
/
IsElement.php
File metadata and controls
181 lines (133 loc) · 4.05 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace Tempest\View\Elements;
use Tempest\View\Element;
use Tempest\View\View;
use Tempest\View\WrapsElement;
/** @phpstan-require-implements \Tempest\View\Element */
trait IsElement
{
private View $view;
/** @var Element[] */
private array $children = [];
private ?Element $parent = null;
private ?Element $previous = null;
private array $attributes = [];
private array $rawAttributes = [];
public function getAttributes(): array
{
if ($this instanceof WrapsElement) {
$wrappingAttributes = $this->getWrappingElement()->getAttributes();
} else {
$wrappingAttributes = [];
}
$attributes = [...$this->attributes, ...$wrappingAttributes];
$tailingAttributes = [];
foreach ($attributes as $name => $value) {
if ($name === ':foreach' || $name === ':if') {
unset($attributes[$name]);
$tailingAttributes[$name] = $value;
}
}
// Tailing attributes are reversed because they need to be applied in reverse order
return [...$attributes, ...array_reverse($tailingAttributes)];
}
public function hasAttribute(string $name): bool
{
$name = ltrim($name, ':');
$attributes = $this->getAttributes();
return array_key_exists(":{$name}", $attributes) || array_key_exists($name, $attributes);
}
public function getAttribute(string $name): ?string
{
$attributes = $this->getAttributes();
return $attributes[$name] ?? null;
}
public function setAttribute(string $name, string $value): self
{
if ($this instanceof WrapsElement) {
$this->getWrappingElement()->setAttribute($name, $value);
}
$this->attributes[$name] = $value;
return $this;
}
public function addRawAttribute(string $attribute): self
{
$this->rawAttributes[] = $attribute;
return $this;
}
public function consumeAttribute(string $name): ?string
{
$value = $this->getAttribute($name);
$this->unsetAttribute($name);
return $value;
}
public function unsetAttribute(string $name): self
{
if ($this instanceof WrapsElement) {
$this->getWrappingElement()->unsetAttribute($name);
}
unset($this->attributes[$name]);
return $this;
}
public function setPrevious(?Element $previous): self
{
$this->previous = $previous;
return $this;
}
public function getPrevious(): ?Element
{
if ($this->previous instanceof WhitespaceElement) {
return $this->previous->getPrevious();
}
return $this->previous;
}
public function setParent(?Element $parent): self
{
$this->parent = $parent;
$this->parent->setChildren([...$this->parent->getChildren(), $this]);
return $this;
}
public function getParent(): ?Element
{
return $this->parent;
}
/** @return \Tempest\View\Element[] */
public function getChildren(): array
{
return $this->children;
}
/** @param \Tempest\View\Element[] $children */
public function setChildren(array $children): self
{
$previous = null;
foreach ($children as $child) {
$child->setPrevious($previous);
$previous = $child;
}
$this->children = $children;
return $this;
}
/**
* @template T of \Tempest\View\Element
* @param class-string<T> $elementClass
* @return T|null
*/
public function unwrap(string $elementClass): ?Element
{
if ($this instanceof $elementClass) {
return $this;
}
if ($this instanceof WrapsElement) {
return $this->getWrappingElement()->unwrap($elementClass);
}
return null;
}
public function getImports(): array
{
if ($this->parent) {
return $this->parent->getImports();
}
return [];
}
}