aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/htmltag.php
blob: 79d5db15a6750077ea06b3db0a7dc089d9e6044c (plain)
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
<?php namespace HashOver;

// Copyright (C) 2015-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class HTMLTag
{
	protected $tag;
	protected $usesPrettyPrint = true;
	protected $isSingleton;
	protected $attributes = array ();
	protected $children = array ();

	public function __construct ($tag = '', $attributes = '', $pretty = true, $singleton = false, $spaced = true)
	{
		if (!is_string ($tag) or !$this->isWord ($tag)) {
			$this->throwError ('Tag must have a single word String value.');
			return;
		}

		if (!is_bool ($singleton)) {
			$this->throwError ('Singleton parameter must have a Boolean value.');
			return;
		}

		if (!is_bool ($pretty)) {
			$this->throwError ('Pretty Print parameter must have a Boolean value.');
			return;
		}

		$this->tag = !empty ($tag) ? $tag : 'span';
		$this->usesPrettyPrint = $pretty;
		$this->isSingleton = $singleton;

		switch (gettype ($attributes)) {
			case 'object': {
				$this->appendChild ($attributes);
				break;
			}

			case 'array': {
				$this->createAttributes ($attributes, $spaced);
				break;
			}

			default: {
				$this->innerHTML ($attributes);
				break;
			}
		}
	}

	public function __get ($name)
	{
		switch ($name) {
			case 'innerHTML': {
				return $this->getInnerHTML ();
			}
		}
	}

	protected function isWord ($string)
	{
		if (empty ($string)) {
			return false;
		}

		if (!preg_match ('/[a-z0-9:-_.]+/iS', $string)) {
			return false;
		}

		return true;
	}

	protected function throwError ($error)
	{
		$backtrace = debug_backtrace ();
		$line = $backtrace[1]['line'];

		throw new \Exception ('Error on line ' . $line . ': ' . $error);
	}

	public function getInnerHTML ($indention = '')
	{
		$inner_html = array ();

		foreach ($this->children as $child) {
			if (is_object ($child)) {
				$inner_html[] = $child->asHTML ($indention);
				continue;
			}

			$inner_html[] = $child;
		}

		return implode (PHP_EOL . $indention, $inner_html);
	}

	public function createAttribute ($name = '', $value = '', $spaced = true)
	{
		if (!is_string ($name) or !$this->isWord ($name)) {
			$this->throwError ('Attribute name must have a single word String value.');
			return false;
		}

		if (is_array ($value)) {
			$glue = ($spaced !== false) ? ' ' : '';
			$this->attributes[$name] = implode ($glue, $value);
			return true;
		}

		$this->attributes[$name] = $value;
		return true;
	}

	public function innerHTML ($html = '')
	{
		if ($this->isSingleton === true) {
			$this->throwError ('Singleton tags do not have innerHTML.');
			return false;
		}

		if (!empty ($html)) {
			$this->children = array ($html);
		}

		return true;
	}

	public function appendInnerHTML ($html = '')
	{
		if ($this->isSingleton === true) {
			$this->throwError ('Singleton tags do not have innerHTML.');
			return false;
		}

		if (!empty ($html)) {
			$this->children[] = $html;
		}

		return true;
	}

	public function createAttributes (array $attributes, $spaced = true)
	{
		if (!is_array ($attributes)) {
			$this->throwError ('Attributes parameter must have an Array value.');
			return;
		}

		foreach ($attributes as $key => $value) {
			switch ($key) {
				case 'children': {
					if (is_array ($value)) {
						for ($i = 0, $il = count ($value); $i < $il; $i++) {
							$this->appendChild ($value[$i]);
						}
					}

					break;
				}

				case 'innerHTML': {
					$this->innerHTML ($value);
					break;
				}

				default: {
					$this->createAttribute ($key, $value, $spaced);
					break;
				}
			}
		}
	}

	public function appendAttribute ($name = '', $value = '', $spaced = true)
	{
		if (!is_string ($name) or !$this->isWord ($name)) {
			$this->throwError ('Attribute name must have a single word String value.');
			return false;
		}

		if (!empty ($this->attributes[$name])) {
			if ($spaced !== false) {
				$this->attributes[$name] .= ' ';
			}
		} else {
			$this->attributes[$name] = '';
		}

		if (is_array ($value)) {
			$glue = ($spaced !== false) ? ' ' : '';
			$this->attributes[$name] .= implode ($glue, $value);
			return true;
		}

		$this->attributes[$name] .= $value;
		return true;
	}

	public function appendAttributes (array $attributes, $spaced = true)
	{
		if (!is_array ($attributes)) {
			$this->throwError ('Attributes parameter must have an Array value.');
			return;
		}

		foreach ($attributes as $key => $value) {
			if ($key === 'innerHTML') {
				$this->appendInnerHTML ($value);
				continue;
			}

			$this->appendAttribute ($key, $value, $spaced);
		}
	}

	public function appendChild (HTMLTag $object)
	{
		if ($this->isSingleton === true) {
			$this->throwError ('Singleton tags do not have children.');
			return false;
		}

		if (!is_object ($object)) {
			$given_type = ucwords (gettype ($object));
			$this->throwError ($given_type . ' given, when Object is expected.');
			return false;
		}

		$this->children[] = $object;
		return true;
	}

	public function asHTML ($indention = '')
	{
		$attributes = '';

		foreach ($this->attributes as $name => $value) {
			$value = str_replace ('"', '&quot;', $value);
			$attributes .= ' ' . $name . '="' . $value . '"';
		}

		$tag = '<' . $this->tag . $attributes . '>';

		if ($this->isSingleton === false) {
			if (!empty ($this->children)) {
				$inner_html = $this->getInnerHTML ();

				if ($this->usesPrettyPrint !== false) {
					$tag .= PHP_EOL . "\t";
					$tag .= str_replace (PHP_EOL, PHP_EOL . "\t", $inner_html);
					$tag .= PHP_EOL;
				} else {
					$tag .= $inner_html;
				}
			}

			$tag .= '</' . $this->tag . '>';
		}

		if (!empty ($indention)) {
			return str_replace (PHP_EOL, PHP_EOL . $indention, $tag);
		}

		return $tag;
	}
}