|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_templates; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_templates\HtmlElement\TCss; |
|
7
|
|
|
use kalanis\kw_templates\HtmlElement\TStyles; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class HtmlElement |
|
12
|
|
|
* @package kalanis\kw_templates |
|
13
|
|
|
* Basic html element - for render simple nodes |
|
14
|
|
|
* @author Adam Dornak original |
|
15
|
|
|
* @author Petr Plsek refactored |
|
16
|
|
|
*/ |
|
17
|
|
|
class HtmlElement extends AHtmlElement |
|
18
|
|
|
{ |
|
19
|
|
|
use TCss, TStyles; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string[] */ |
|
22
|
|
|
protected static array $emptyElements = ['img','hr','br','input','meta','area','embed','keygen','link','param','frame']; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string element's name */ |
|
25
|
|
|
private string $name = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @param array<string, string|int|array<string>> $attributes |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public static function init(string $name, array $attributes = []): self |
|
33
|
|
|
{ |
|
34
|
|
|
// @phpstan-ignore-next-line |
|
35
|
2 |
|
return new self($name, $attributes); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param array<string, string|int|array<string>> $attributes |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function __construct(string $name, array $attributes = []) |
|
43
|
|
|
{ |
|
44
|
2 |
|
$name = strval(str_ireplace(['<','>'],'', $name)); |
|
45
|
2 |
|
$parts = explode(' ', $name, 2); |
|
46
|
2 |
|
$name = $parts[0]; |
|
47
|
|
|
|
|
48
|
2 |
|
$this->name = $parts[0]; |
|
49
|
|
|
|
|
50
|
2 |
|
if (in_array($this->name, static::$emptyElements)) { |
|
51
|
1 |
|
$this->template = "<{$name}%1\$s />"; |
|
52
|
|
|
} else { |
|
53
|
1 |
|
$this->template = "<{$name}%1\$s>%2\$s</{$name}>"; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$this->addAttributes($attributes); |
|
57
|
|
|
|
|
58
|
2 |
|
if (isset($parts[1])) { |
|
59
|
1 |
|
$this->addAttributes($parts[1]); |
|
60
|
|
|
} |
|
61
|
2 |
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|