-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElement.php
More file actions
executable file
·165 lines (149 loc) · 3.58 KB
/
Element.php
File metadata and controls
executable file
·165 lines (149 loc) · 3.58 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
<?php
/**
* @Package: MaplePHP - DOM Element class
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
* @Version: 1.0.0
*/
namespace MaplePHP\Output\Dom;
use MaplePHP\Output\Interfaces\ElementInterface;
use BadMethodCallException;
class Element extends Document implements ElementInterface
{
private $elem;
private $attr = [];
private $snippet;
private $value;
private $hideEmptyTag = false;
//private $node;
//private $inst;
public function __construct(string $elem, ?string $value, bool $snippet = false)
{
$this->elem = $elem;
$this->value = $value;
$this->snippet = $snippet;
}
/**
* Overwrite the current element
* @param string $elem HTML Tag name
*/
public function setElement(string $elem): self
{
$this->elem = $elem;
return $this;
}
/**
* Set html attribute
* @param string $key attr key
* @param string|null $val attr value
* @return self
*/
public function attr(string $key, ?string $val = null): self
{
$this->attr[$key] = $val;
return $this;
}
/**
* Set multiple html attributes
* @param array [key => value]
* @return self
*/
public function attrArr(?array $arr): self
{
if (is_array($arr)) {
$this->attr = array_merge($this->attr, $arr);
}
return $this;
}
/**
* Hide html tag if its value is empty
* @param bool $bool
* @return self
*/
public function hideEmptyTag(bool $bool): self
{
$this->hideEmptyTag = $bool;
return $this;
}
/**
* Validate hide tag value
* @return bool
*/
protected function hideTagValid(): bool
{
return (($this->hideEmptyTag && !$this->value));
}
/**
* Add value to attr
* @param string $key
* @param string $value
* @param string $sep
* @return self
*/
public function addAttr(string $key, string $value, string $sep = " "): self
{
if (isset($this->attr[$key])) {
$this->attr[$key] .= "{$sep}{$value}";
} else {
$this->attr[$key] = $value;
}
return $this;
}
/**
* Set elem value <elem>[VALUE]</elem>
* @param string|null null value can be used to auto skip HTML tag
* @return self
*/
public function setValue(?string $value): self
{
$this->value = $value;
return $this;
}
/**
* Set elem value
* @return string
*/
public function getValue(): string
{
return (string)$this->value;
}
/**
* Get elem/HTML tag
* @return string
*/
public function getEl(): string
{
return $this->elem;
}
/**
* With cloned element or new element if is specifed
* @param string|null $elem
* @return self
*/
public function withElement(?string $elem = null): self
{
$inst = clone $this;
if ($elem !== null) {
$inst->elem = $elem;
}
return $inst;
}
/**
* Array attr to string
* @return string
*/
protected function buildAttr(): string
{
$attr = "";
if (count($this->attr) > 0) {
foreach ($this->attr as $k => $v) {
$attr .= " {$k}";
if ($v !== null) {
$attr .= "=\"{$v}\"";
}
}
}
return $attr;
}
}