-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.php
96 lines (84 loc) · 2.15 KB
/
UI.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
<?php
/**
* UI/HTMLElement - makes HTML outputting using PHP a breeze.
*
* @author Stig Rune Grønnestad
* @copyright Stig Rune Grønnestad @ 2013
* @version 1.1
*
*/
/**
* This class is used only as a HTMLElement factory,
* the name is short to make our code less bloat.
*/
class UI {
/**
* HTMLElement fabrication. This class makes it possible to initialize
* objects using UI::div(), UI::table() and so forth.
* @param [string] $function "Function" or name of the element to fabricate
* @param [type] $vars not used
* @return [HTMLElement]
*/
public static function __callStatic($function, $vars) {
return new HTMLElement($function);
}
}
/**
* HTMLElement represents a single html element.
*/
class HTMLElement {
/**
* Constructor for the base HTMLElement
* @param [string] $tag tag name
*/
public function __construct($tag) {
$this->tag = $tag;
$this->inner = "";
$this->children = [];
$this->attributes = [];
return $this;
}
/**
* Override call function to have it set attributes on the element instead
* @param [string] $name attribute name
* @param [mixed] $arguments attribute value
* @return [HTMLElement] self
*/
public function __call($name, $arguments) {
$name = strtolower($name);
$reserved = array("tag", "inner", "children");
if (in_array($name, $reserved)) {
// Handle local properties.
$this->$name = $arguments[0];
} else {
// Handle attributes on the element.
if (trim($arguments[0])) {
$name = str_replace("_", "-", $name);
$this->attributes[$name] = $arguments[0];
}
}
return $this;
}
/**
* Print out the HTML for the element when used as string
* @return string HTML of element
*/
public function __toString() {
$html = "<{$this->tag}";
// Add all the attributes
foreach ($this->attributes as $key => $value) {
$html .= " {$key}=\"{$value}\"";
}
$html .= ">";
// Add the inner html
$html .= $this->inner;
// Add children
foreach ($this->children as $element) {
$html .= $element;
}
// Close the tag
$html .= "</{$this->tag}>";
return $html;
}
}
?>