-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeaf.php
More file actions
182 lines (148 loc) · 3.73 KB
/
Leaf.php
File metadata and controls
182 lines (148 loc) · 3.73 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Undercloud;
class Leaf
{
private $format = false;
private $indent = ' ';
private $level = 0;
private $tags_stack = array();
private $fill_stack = array();
private $selfclosing = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');
private $content = '';
public function __construct(array $o = array())
{
if (isset($o['selfclosing'])) {
$this->selfclosing = $o['selfclosing'];
}
if (isset($o['format'])) {
$this->format = $o['format'];
}
if (isset($o['indent'])) {
$this->indent = $o['indent'];
}
}
public static function init(array $o = array())
{
return new self($o);
}
public static function escape($a, $de = false)
{
return htmlentities($a, ENT_QUOTES, 'UTF-8', $de);
}
public function el($tag)
{
$attr = array();
$text = null;
$args = func_get_args();
$lclass = array();
switch (count($args)) {
case 2:
if (is_array($args[1])) {
$attr = $args[1];
} else {
$text = $args[1];
}
break;
case 3:
$attr = $args[1];
$text = $args[2];
break;
}
$tag = preg_replace_callback(
'/(\.|\#|\:)([A-Za-z0-9\-\_]+)/',
function ($m) use (&$attr, &$lclass) {
switch ($m[1]) {
case '.':
$lclass[] = $m[2];
break;
case '#':
$attr['id'] = $m[2];
break;
case ':':
if (in_array($m[2], array('button', 'checkbox', 'file', 'hidden', 'image', 'password', 'radio', 'reset', 'submit', 'text'))) {
$attr['type'] = $m[2];
} else if (in_array($m[2], array('checked', 'disabled', 'readonly', 'required', 'multiple'))) {
$attr[$m[2]] = $m[2];
}
break;
}
return;
},
$tag
);
if ($attr) {
if (isset($attr['class']) and $attr['class']) {
$attr['class'] = implode(' ', array_merge(
$lclass,
explode(' ', $attr['class'])
));
} else if ($lclass) {
$attr['class'] = implode(' ', $lclass);
}
array_walk($attr, function (&$a, $b) {
if (is_integer($b)) {
$a = static::escape($a);
} else {
if ('style' == $b and is_array($a)) {
array_walk($a, function (&$x, $y) {
$x = ($y . ':' . $x);
});
$a = implode(';', $a);
}
$a = $b . '="' . static::escape($a) . '"';
}
});
}
$space = '';
if (true === $this->format) {
$indent = str_repeat($this->indent, $this->level);
}
$closed = in_array($tag, $this->selfclosing);
if (false == $closed) {
$this->tags_stack[] = $tag;
$this->level++;
}
if (true === $this->format) {
$this->fill_stack[] = (false == $closed and $text !== null);
$space = ($this->level > 1 ? (PHP_EOL . $indent) : '');
}
$this->content .= $space . '<' . $tag . ($attr ? (' ' . implode(' ', $attr)) : '') . ($closed ? ' />' : '>') . static::escape($text);
return $this;
}
public function text($t)
{
$this->content .= static::escape($t);
return $this;
}
public function raw($r)
{
$this->content .= $r;
return $this;
}
public function __get($p)
{
if ($p == 'end') {
return $this->end();
} else {
throw new \Exception(
sprintf('Undefined property %s', $p)
);
}
}
public function end()
{
$space = '';
$this->level--;
if (true === $this->format) {
$indent = str_repeat($this->indent, (($this->level >= 0) ? $this->level : 0));
$space = (false == array_pop($this->fill_stack) ? (PHP_EOL . $indent) : '');
}
$this->content .= $space . '</' . array_pop($this->tags_stack) . '>';
return $this;
}
public function __toString()
{
return $this->content;
}
}
?>