Element   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 1
dl 0
loc 276
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A copy() 0 4 1
A addClass() 0 5 1
A removeClass() 0 5 1
A val() 0 5 1
A init() 0 12 2
A attr() 0 12 4
A removeAttr() 0 8 3
B _editClass() 0 27 7
A text() 0 5 1
A render() 0 29 5
1
<?php
2
namespace Redaxscript\Html;
3
4
use function array_diff;
5
use function array_filter;
6
use function array_key_exists;
7
use function array_map;
8
use function array_merge;
9
use function array_unique;
10
use function explode;
11
use function implode;
12
use function in_array;
13
use function is_array;
14
use function strip_tags;
15
use function strlen;
16
use function strtolower;
17
use function trim;
18
19
/**
20
 * children class to create a element
21
 *
22
 * @since 2.6.0
23
 *
24
 * @package Redaxscript
25
 * @category Html
26
 * @author Henry Ruhs
27
 */
28
29
class Element extends HtmlAbstract
30
{
31
	/**
32
	 * tag of the element
33
	 *
34
	 * @var string
35
	 */
36
37
	protected $_tag;
38
39
	/**
40
	 * array of singleton tags
41
	 *
42
	 * @var array
43
	 */
44
45
	protected $_singletonTags =
46
	[
47
		'area',
48
		'base',
49
		'br',
50
		'col',
51
		'command',
52
		'embed',
53
		'hr',
54
		'img',
55
		'input',
56
		'keygen',
57
		'link',
58
		'meta',
59
		'param',
60
		'source',
61
		'track',
62
		'wbr',
63
	];
64
65
	/**
66
	 * attributes of the element
67
	 *
68
	 * @var array
69
	 */
70
71
	protected $_attributeArray = [];
72
73
	/**
74
	 * stringify the element
75
	 *
76
	 * @since 2.2.0
77
	 *
78
	 * @return string
79
	 */
80
81 11
	public function __toString() : string
82
	{
83 11
		return $this->render();
84
	}
85
86
	/**
87
	 * init the class
88
	 *
89
	 * @since 2.2.0
90
	 *
91
	 * @param string $tag tag of the element
92
	 * @param array $attributeArray attributes of the element
93
	 *
94
	 * @return self
95
	 */
96
97 18
	public function init(string $tag = null, array $attributeArray = []) : self
98
	{
99 18
		$this->_tag = strtolower($tag);
100
101
		/* process attributes */
102
103 18
		if (is_array($attributeArray))
104
		{
105 18
			$this->attr($attributeArray);
106
		}
107 18
		return $this;
108
	}
109
110
	/**
111
	 * copy the element
112
	 *
113
	 * @since 2.2.0
114
	 *
115
	 * @return self
116
	 */
117
118 1
	public function copy() : self
119
	{
120 1
		return clone $this;
121
	}
122
123
	/**
124
	 * set the attribute to element
125
	 *
126
	 * @since 2.2.0
127
	 *
128
	 * @param string|array $attribute key or array of attributes
129
	 * @param string $value value of the attribute
130
	 *
131
	 * @return self
132
	 */
133
134 18
	public function attr($attribute = null, string $value = null) : self
135
	{
136 18
		if (is_array($attribute))
137
		{
138 18
			$this->_attributeArray = array_merge($this->_attributeArray, array_map('trim', $attribute));
139
		}
140 4
		else if (strlen($attribute) && strlen($value))
141
		{
142 3
			$this->_attributeArray[$attribute] = trim($value);
143
		}
144 18
		return $this;
145
	}
146
147
	/**
148
	 * remove the attribute from element
149
	 *
150
	 * @since 2.2.0
151
	 *
152
	 * @param string $attribute name of attributes
153
	 *
154
	 * @return self
155
	 */
156
157 3
	public function removeAttr(string $attribute = null) : self
158
	{
159 3
		if (is_array($this->_attributeArray) && array_key_exists($attribute, $this->_attributeArray))
160
		{
161 1
			unset($this->_attributeArray[$attribute]);
162
		}
163 3
		return $this;
164
	}
165
166
	/**
167
	 * add the class to element
168
	 *
169
	 * @since 2.2.0
170
	 *
171
	 * @param string $className name of the classes
172
	 *
173
	 * @return self
174
	 */
175
176 3
	public function addClass(string $className = null) : self
177
	{
178 3
		$this->_editClass($className, 'add');
179 3
		return $this;
180
	}
181
182
	/**
183
	 * remove the class from element
184
	 *
185
	 * @since 2.2.0
186
	 *
187
	 * @param string $className name of the classes
188
	 *
189
	 * @return self
190
	 */
191
192 3
	public function removeClass(string $className = null) : self
193
	{
194 3
		$this->_editClass($className, 'remove');
195 3
		return $this;
196
	}
197
198
	/**
199
	 * edit class helper
200
	 *
201
	 * @since 2.2.0
202
	 *
203
	 * @param string $className name of the classes
204
	 * @param string $type add or remove
205
	 */
206
207 3
	protected function _editClass(string $className = null, string $type = null) : void
208
	{
209 3
		$classArray = array_filter(explode(' ', $className));
210 3
		if (is_array($this->_attributeArray) && array_key_exists('class', $this->_attributeArray))
211
		{
212 3
			$attributeClassArray = array_filter(explode(' ', $this->_attributeArray['class']));
213
		}
214
		else
215
		{
216 3
			$attributeClassArray = [];
217
		}
218
219
		/* add or remove */
220
221 3
		if (is_array($attributeClassArray) && is_array($classArray))
222
		{
223 3
			if ($type === 'add')
224
			{
225 3
				$attributeClassArray = array_merge($attributeClassArray, $classArray);
226
			}
227 3
			else if ($type === 'remove')
228
			{
229 3
				$attributeClassArray = array_diff($attributeClassArray, $classArray);
230
			}
231 3
			$this->_attributeArray['class'] = implode(' ', array_unique($attributeClassArray));
232
		}
233 3
	}
234
235
	/**
236
	 * set the value to element
237
	 *
238
	 * @since 2.2.0
239
	 *
240
	 * @param string|int $value value of the element
241
	 *
242
	 * @return self
243
	 */
244
245 3
	public function val($value = null) : self
246
	{
247 3
		$this->_attributeArray['value'] = trim($value);
248 3
		return $this;
249
	}
250
251
	/**
252
	 * set the text to element
253
	 *
254
	 * @since 4.0.0
255
	 *
256
	 * @param string|int $text text of the element
257
	 *
258
	 * @return self
259
	 */
260
261 4
	public function text($text = null) : self
262
	{
263 4
		$this->_html = strip_tags($text);
264 4
		return $this;
265
	}
266
267
	/**
268
	 * render the element
269
	 *
270
	 * @since 2.2.0
271
	 *
272
	 * @return string
273
	 */
274
275 17
	public function render() : string
276
	{
277 17
		$output = '<' . $this->_tag;
278
279
		/* process attributes */
280
281 17
		foreach ($this->_attributeArray as $attribute => $value)
282
		{
283 10
			if (strlen($attribute) && strlen($value))
284
			{
285 6
				$output .= ' ' . $attribute . '="' . $value . '"';
286
			}
287
		}
288
289
		/* singleton tag */
290
291 17
		if (in_array($this->_tag, $this->_singletonTags))
292
		{
293 5
			$output .= ' />';
294
		}
295
296
		/* else normal tag */
297
298
		else
299
		{
300 12
			$output .= '>' . $this->_html . '</' . $this->_tag . '>';
301
		}
302 17
		return $output;
303
	}
304
}
305