-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElement.php
More file actions
360 lines (324 loc) · 9.39 KB
/
Copy pathElement.php
File metadata and controls
360 lines (324 loc) · 9.39 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Uhupi\Layout;
/**
* Main Class for Layout to include on Framework
*
* This is a PHP Utility for Frontend Developers.
*
* PHP version 7.4+
*
* @category Frontend & Backend
* @package PHP-OOP-HTML-Tag-Element
* @author Santino Lange <santino@uhupi.com>
* @copyright 2015-2023 Uhupi
* @license MIT
*/
class Element {
protected string $tag = 'div'; /* Html Element Type */
protected array $attrs = []; /* Html Element attributes */
protected array $classes = []; /* Html CSS Class. */
protected array $styles = []; /* Set Style CSS properties for Element */
protected bool $isSingleton = false; /* Defines if Element is an Html Tag or not */
protected ?string $prepend = null; /* Set Content before Element */
protected ?string $content = null; /* Content for Element */
protected ?string $append = null; /* Set Content before Element */
/**
* MAGIC FUNCTION
* Collects or sets basic information about the Element
*
* @param string $tag The html element you want to use
* @param mixed $content Preset or previous content in your element
* @param string $class Class or classes you want to preset in your element
*/
public function __construct(string $tag = 'div', $content = null, ?string $class = null)
{
$this->setTag($tag);
if ($content) {
$this->setContent($content);
}
if ($class) {
$this->setClass($class);
}
}
/**
* Set the html element you want to use
*/
public function setTag(string $tag): self
{
$this->tag = $tag;
return $this;
}
/**
* Get the current html element tag
*/
public function getTag(): string
{
return $this->tag;
}
/**
* Glue another Element or content after this Element
*
* @param mixed $content Element or String you want to append
* @return $this;
*/
public function append(?string $content): self
{
$this->append = $content;
return $this;
}
/**
* Glue another Element or content before this Element
*
* @param mixed $content Element or String you want to prepend
* @return $this;
*/
public function prepend(?string $content): self
{
$this->prepend = $content;
return $this;
}
/**
* ATTRIBUTE MANAGEMENT
* Set a new attribute for this Element
*/
public function setAttr(string $name, string $value): self
{
$this->attrs[$name] = $value;
return $this;
}
/**
* ATTRIBUTE MANAGEMENT
* Get a new attribute from this Element
*/
public function getAttr(string $name) : ?string
{
if (isset($this->attrs[$name])) {
return $this->attrs[$name];
}
return null;
}
/**
* ATTRIBUTE MANAGEMENT
* Remove one attr from the Element
*/
public function removeAttr(string $attr) : self
{
unset($this->attrs[$attr]);
return $this;
}
/**
* ATTRIBUTE MANAGEMENT
* Fill the attribute "title"
*/
public function setTitle(string $title): self
{
$this->setAttr('title', $title);
return $this;
}
/**
* CONTENT MANAGEMENT
* Set or appends more content into the Element
*
* @param mixed $content Element object or String to render in Element
*/
public function setContent(mixed $content, bool $reset = false): self
{
if (is_object($content) && method_exists($content, 'render')) {
$content = $content->render();
}
if ($reset) {
$this->content = $content;
} else {
$this->content.= $content;
}
return $this;
}
/**
* CONTENT MANAGEMENT
* Allowes the possibility of running a php include and append it as content
*/
public function setInclude(string $file, string $path = __DIR__): self
{
$realPath = realpath($path . '/' . $file);
$realBase = realpath($path);
if ($realPath === false || $realBase === false || strpos($realPath, $realBase . DIRECTORY_SEPARATOR) !== 0) {
throw new \InvalidArgumentException('Invalid or disallowed file path.');
}
ob_start();
include $realPath;
$this->setContent(ob_get_clean());
return $this;
}
/**
* CONTENT MANAGEMENT
* Get the collected content so far
*/
public function getContent(bool $reset = false): ?string
{
$content = $this->content;
if ($reset) {
$this->content = null;
}
return $content;
}
/**
* CLASS MANAGEMENT
* Add one or more classes separate by spaces to the Element
*/
public function addClass(string $classes): self
{
foreach (explode(' ', $classes) as $class) {
$this->setClass($class);
}
return $this;
}
/**
* CLASS MANAGEMENT
* Set one class to the Element
*/
public function setClass(string $class): self
{
$this->classes[$class] = $class;
return $this;
}
/**
* CLASS MANAGEMENT
* Remove one class from the Element
*/
public function removeClass(string $class): self
{
unset($this->classes[$class]);
return $this;
}
/**
* CLASS MANAGEMENT
* Reset all classes from the Element
*/
public function resetClasses(): self
{
$this->classes = [];
return $this;
}
/**
* CLASS MANAGEMENT
* Check if the Element has a class
*/
public function hasClass(string $class): bool
{
return !empty($this->classes[$class]);
}
/**
* DATA ATTRIBUTE MANAGEMENT
* The data name will be complile with "data-"
*/
public function setData(string $name, string $value): self
{
$this->attrs['data-' . $name] = $value;
return $this;
}
/**
* DATA ATTRIBUTE MANAGEMENT
* Return the value of a data attribute. The data name will be complile with "data-"
*/
public function getData(string $name): ?string
{
return $this->attrs['data-' . $name] ?? null;
}
/**
* ARIA ATTRIBUTE MANAGEMENT
* The aria name will be complile with "aria-"
*/
public function setAria(string $name, string $value = ''): self
{
$this->attrs['aria-' . $name] = $value;
return $this;
}
/**
* ARIA ATTRIBUTE MANAGEMENT
* Return the value of an aria attribute. The aria name will be complile with "aria-"
*/
public function getAria(string $name): ?string
{
return $this->attrs['aria-' . $name] ?? null;
}
/**
* CSS ATTRIBUTE MANAGEMENT
* This allowes to use inline CSS styling on your element
*
* NOTE:
* Inlining CSS attributes on HTML elements (e.g., <p style=...>)
* should be avoided where possible, as this often leads to unnecessary
* code duplication. Further, inline CSS on HTML elements is blocked by
* default with Content Security Policy (CSP).
*/
public function setStyle(string $property, string $value): self
{
$this->styles[$property] = $value;
return $this;
}
/**
* DATA ATTRIBUTE MANAGEMENT
* Render all attibutes in this Element
* Al the attributes glue toguether in a string
*/
private function renderAttr(): string
{
$display = '';
if (count($this->classes)) {
$this->setAttr('class', implode(' ', $this->classes));
}
$css = '';
if (count($this->styles)) {
foreach ($this->styles as $key => $value) {
$css .= $key . ': ' . $value . '; ';
}
$this->setAttr('style', rtrim($css));
}
foreach ($this->attrs as $key => $value) {
if ($value === false) {
$display .= $key . ' ';
} else {
$display .= $key . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '" ';
}
}
return $display !== '' ? ' ' . trim($display) : '';
}
/**
* Define if this Element is only a Tag. (e.g., <br>, <hr>, etc...)
*/
public function isSingleton(bool $isSingleton = true): self
{
$this->isSingleton = $isSingleton;
return $this;
}
/**
* render
*
* Render object into an string
*
* @return string $display All the attributes glue together in a string
*/
public function render(): string
{
$opener = '<';
$closer = '>';
$end = '/';
$attrs = $this->renderAttr();
$display = null;
$display.= $this->prepend;
$display.= $opener . $this->tag . $attrs . (($this->isSingleton) ? $end : null) . $closer;
$display.= $this->content;
$display.= ($this->isSingleton) ? null : $opener . $end . $this->tag . $closer ;
$display.= $this->append;
return ($this->content || $attrs || $this->isSingleton) ? $display : '';
}
/**
* MAGIC FUNCTION
* Render object when it is treated like a string
* $this->render() All the attributes glue together into a string
*/
public function __toString(): string
{
return $this->render();
}
}