forked from BaylorRae/HTML-Class-For-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.class.php
195 lines (166 loc) · 4.2 KB
/
html.class.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
<?php
/**
* A class that makes building
* html elements easy, and is used
* similar to jQuery
*
* @package default
* @author Baylor Rae'
*/
class html {
private $tag;
private $self_closing = false;
private $attrs = array();
private $self_closing_list = array('input', 'img', 'hr', 'br', 'meta', 'link');
/**
* Create an html element
*
* If you leave $self_closing blank it will
* determine whether or not to auto close
*
* @param string $tag - The tag's name div, input, form
* @param array $attrs - Attributes class, id
* @param boolean $self_closing
* @author Baylor Rae'
*/
function __construct($tag, $attrs = array(), $self_closing = null) {
$this->tag = $tag;
// force this tag to self close?
if( is_null($self_closing) )
$this->self_closing = in_array($tag, $this->self_closing_list);
else
$this->self_closing = $self_closing;
// Make sure text is set
$attrs['text'] = (empty($attrs['text'])) ? '' : $attrs['text'];
$this->attrs = $attrs;
}
/**
* Build the html element
*
* @see $this->build()
* @return void
* @author Baylor Rae'
*/
public function __toString() {
return $this->build();
}
/**
* Add an attribute to the element
* or multiple attributes if the first param is an array
*
* @param mixed $attr
* @param string $value
* @return void
* @author Baylor Rae'
*/
public function attr($attr, $value = null) {
if( is_array($attr) )
$this->attrs = array_merge($this->attrs, $attr);
else
$this->attrs = array_merge($this->attrs, array($attr => $value));
}
/**
* Creates the html element's opening and closing tags
* as well as the attributes.
*
* @return void
* @author Baylor Rae'
*/
public function build() {
// Start the tag
$output = '<' . $this->tag;
// Add the attributes
foreach( $this->attrs as $attr => $value ) {
if( $attr == 'text' )
continue;
if( is_integer($attr) )
$attr = $value;
$output .= ' ' . $attr . '="' . $value . '"';
}
// Close the tag
if( $this->self_closing )
$output .= ' />';
else
$output .= '>' . $this->attrs['text'] . '</' . $this->tag . '>';
return $output;
}
/**
* Clone the current element
* to prevent effecting the original
*
* @return new html object
* @author Baylor Rae'
*/
public function _clone() {
return new html($this->tag, $this->attrs, $this->self_closing);
}
/**
* Check if the object being used
* in the methods below, is part of the html class
*
* @package default
* @author Baylor Rae'
*/
private function check_class($obj) {
return (@get_class($obj) == __class__);
}
/**
* Append an element to the current
* or for multiple use each element
* as a parameter
*
* @return $this (for chaining)
* @author Baylor Rae'
*/
public function append() {
$elems = func_get_args();
foreach( $elems as $obj ) {
if( !$this->check_class($obj) )
continue;
$this->attrs['text'] .= $obj->build();
}
return $this;
}
/**
* Prepend an element to the current
* or for multiple use each element
* as a parameter
*
* @return $this (for chaining)
* @author Baylor Rae'
*/
public function prepend() {
$elems = func_get_args();
$elems = array_reverse($elems);
foreach( $elems as $obj ) {
if( !$this->check_class($obj) )
continue;
$this->attrs['text'] = $obj->build() . $this->attrs['text'];
}
return $this;
}
/**
* Append this element onto another
*
* @param object $obj
* @return void
* @author Baylor Rae'
*/
public function appendTo($obj) {
if( !$this->check_class($obj) )
return false;
$obj->attrs['text'] .= $this->build();
}
/**
* Prepend this element onto another
*
* @param object $obj
* @return void
* @author Baylor Rae'
*/
public function prependTo($obj) {
if( !$this->check_class($obj) )
return false;
$obj->attrs['text'] = $this->build() . $obj->attrs['text'];
}
}