-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.php
More file actions
executable file
·237 lines (212 loc) · 6.01 KB
/
Document.php
File metadata and controls
executable file
·237 lines (212 loc) · 6.01 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* @Package: MaplePHP - DOM Main class
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Output\Dom;
use MaplePHP\Output\Interfaces\DocumentInterface;
use MaplePHP\Output\Interfaces\ElementInterface;
class Document implements DocumentInterface
{
public const TAG_NO_ENDING = [
"meta",
"link",
"img",
"br",
"hr",
"input",
"keygen",
"param",
"source",
"track",
"embed"
];
protected $elements;
private $html;
private $elem;
private static $inst;
/**
* Will output get
* @return string
*/
public function __toString(): string
{
return $this->get();
}
/**
* Get get Dom/document (Will only trigger execute once per instance)
* @return string
*/
public function get(): string
{
if ($this->html === null) {
$this->execute();
}
return $this->html;
}
/**
* Init DOM instance
* @param string $key DOM access key
* @return self
*/
public static function dom(string $key): self
{
if (empty(self::$inst[$key])) {
self::$inst[$key] = self::withDom($key);
}
return self::$inst[$key];
}
/**
* Init DOM instance
* @param string $key DOM access key
* @return self
*/
public static function withDom(string $key): self
{
self::$inst[$key] = new self();
return self::$inst[$key];
}
/**
* Create and bind tag to a key so it can be overwritten
* @param string $tag HTML tag (without brackets)
* @param string $key Bind tag to key
* @param bool|boolean $prepend Prepend instead of append
* @return ElementInterface
*/
public function bindTag(string $tag, string $key, bool $prepend = false): ElementInterface
{
if ($prepend) {
$this->elem = $this->createPrepend($tag, null, $key);
} else {
$this->elem = $this->create($tag, null, $key);
}
return $this->elem;
}
/**
* Create (append) element
*
* @param string $element HTML tag (without brackets)
* @param null $value add value to tag
* @param string|null $bind
* @return ElementInterface
*/
public function create($element, $value = null, ?string $bind = null): ElementInterface
{
$inst = new Element($element, $value);
if ($bind !== null) {
$this->elements[$bind] = $inst;
} else {
$this->elements[] = $inst;
}
return $inst;
}
/**
* Prepend element first
* @param string $element HTML tag (without brackets)
* @param string $value add value to tag
* @return ElementInterface
*/
public function createPrepend(string $element, ?string $value = null, ?string $bind = null): ElementInterface
{
$inst = new Element($element, $value);
if ($this->elements === null) {
$this->elements = [];
}
if ($bind !== null) {
//$new[$bind] = $inst;
$this->elements = array_merge([$bind => $inst], $this->elements);
} else {
$this->elements = array_merge([$inst], $this->elements);
}
return $inst;
}
/**
* Get one element from key
* @return ElementInterface|null
*/
public function getElement(string $key): ?ElementInterface
{
return ($this->elements[$key] ?? null);
}
/**
* Get all elements
* @return array
*/
public function getElements(): array
{
return $this->elements;
}
/**
* Get html tag
* @param string $key
* @return string|null
*/
public function getTag(string $key): ?string
{
return ($this->el[$key] ?? null);
}
/**
* Execute and get Dom/document
* @param callable|null $call Can be used to manipulate element within feed
* @return string
*/
public function execute(?callable $call = null): string
{
$this->html = "";
if ($this->elements === null) {
if (method_exists($this, "withElement")) {
$inst = $this->withElement();
$this->elements[] = $inst;
}
}
if (is_array($this->elements)) {
$this->build($this->elements, $call);
}
return $this->html;
}
/**
* Build document
* @param array $arr elements
* @param callable|null $call Can be used to manipulate element within feed
* @return void
*/
private function build(array $arr, ?callable $call = null): void
{
foreach ($arr as $key => $elemObj) {
$hasNoEnding = $this->elemHasEnding($elemObj->getEl());
$this->buildCallable($elemObj, $key, $hasNoEnding, $call);
if (!$elemObj->hideTagValid()) {
$this->html .= "\t<" . $elemObj->getEl() . $elemObj->buildAttr() . ">";
}
if (!$hasNoEnding) {
$this->html .= $elemObj->getValue();
}
if (isset($elemObj->elements)) {
$this->build($elemObj->elements, $call);
}
if (!$hasNoEnding && !$elemObj->hideTagValid()) {
$this->html .= "</" . $elemObj->getEl() . ">\n";
}
if ($hasNoEnding && !$elemObj->hideTagValid()) {
$this->html .= "\n";
}
}
}
private function buildCallable($elemObj, $key, $hasNoEnding, ?callable $call): void
{
if ($call !== null) {
$call($elemObj, $key, $hasNoEnding);
}
}
/**
* Validate if element has ending
* @param string $elem
* @return bool
*/
final protected function elemHasEnding(string $elem): bool
{
return (in_array($elem, $this::TAG_NO_ENDING));
}
}