-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.php
205 lines (177 loc) · 4.21 KB
/
html.php
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
<?php
/**
* HtmlHelper
*
* Simple helper class that allows the user to concatenate
* a fragment of HTML based on the required elements.
*
* Use $instance->open to open an element
* Use $instance->close to close # of (or all) elements
* Use $instance->single to append a self closing element
* Use $instance->element to append an element
* Use $instance->append to append raw content to the HTML
*
* Typecasting $instance to (string) or calling $instance->render()
* will return all previously declared HTML if $instance was created
* using $capture = true (default).
*
* @copyright Copyright (c) Dutchwise V.O.F. (http://dutchwise.nl)
* @author Max van Holten (<[email protected]>)
* @license http://www.opensource.org/licenses/mit-license.php MIT license
*/
if(!class_exists('HtmlHelper')) {
class HtmlHelper {
/**
* Stores tagNames that were opened.
*
* @var array
*/
protected $_openedTags = array();
/**
* Indicates whether to capture strings.
*
* @var boolean
*/
protected $_captureEnabled = false;
/**
* Stores captured strings.
*
* @var string
*/
protected $_output = '';
/**
* Renders the provided attributes.
* - Attributes with values set to NULL are skipped.
* - Attributes with numeric keys are added by value as valueless attributes
*
* @param array $attr
* @return string
*/
protected function _renderAttributes(array $attr = array()) {
$lines = array();
foreach($attr as $name => $value) {
if(is_null($value)) {
continue;
}
if(is_numeric($name)) {
$lines[] = $value;
}
else {
$lines[] = "{$name}=\"{$value}\"";
}
}
$lines = ' ' . implode(' ', $lines);
if(!$attr) {
$lines = trim($lines);
}
return $lines;
}
/**
* Class constructor
*
* @param boolean $capture Enables capturing strings.
*/
public function __construct($capture = true) {
$this->_captureEnabled = $capture;
}
/**
* Opens an element, allowing other code to
* render the contents of the element.
*
* @param string $tagName
* @param array $attr
* @return string
*/
public function open($tagName, array $attr = array()) {
$attr = $this->_renderAttributes($attr);
$this->_openedTags[] = $tagName;
$result = "<{$tagName}{$attr}>";
if($this->_captureEnabled) {
$this->_output .= $result;
}
return $result;
}
/**
* Renders a single self closing element.
*
* @param string $tagName
* @param array $attr
* @return string
*/
public function single($tagName, array $attr = array()) {
$attr = $this->_renderAttributes($attr);
$result = "<{$tagName}{$attr} />";
if($this->_captureEnabled) {
$this->_output .= $result;
}
return $result;
}
/**
* Closes elements opened with the self::open
* method and returns the closing tags.
*
* @param int $amount
* @return string
*/
public function close($amount = INF) {
$open =& $this->_openedTags;
$str = '';
while($amount-- && $open) {
$str .= '</' . array_pop($open) . '>';
}
if($this->_captureEnabled) {
$this->_output .= $str;
}
return $str;
}
/**
* Appends the provided HTML to the
* output string.
*
* @param string $html
* @return void
*/
public function append($html) {
$this->_output .= $html;
}
/**
* Renders a element of the provided
* tagName, attributes and content.
*
* @return string
*/
public function element($tagName, $attr = array(), $content = '') {
if(is_string($attr)) {
$content = $attr;
$attr = array();
}
$attr = $this->_renderAttributes($attr);
$result = "<{$tagName}{$attr}>{$content}</{$tagName}>";
if($this->_captureEnabled) {
$this->_output .= $result;
}
return $result;
}
/**
* Renders previously captured output.
*
* @return string
*/
public function render() {
$output = $this->_output;
$this->_output = '';
return $output;
}
/**
* When this instance gets typecasted to string
* the output will be rendered.
*
* @magic __toString
* @return string
*/
public function __toString() {
return $this->render();
}
}
}
?>