-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRAGMENT.php
More file actions
142 lines (106 loc) · 3.4 KB
/
FRAGMENT.php
File metadata and controls
142 lines (106 loc) · 3.4 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
<?
/*
* PHP HTML Engine
*
* Copyright (C) 2015 Charles Johannisen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
class FRAGMENT extends HTML{
public $__config = array();
public $__id = -1;
public $__lastid = 0;
public function __construct(){
}
public function __call( $name, $args ){
$attributes = array();
$addto = null;
$id = null;
$content = "";
if( count($args) > 0 ){
foreach( $args as $arg ){
if( !ctype_digit( $arg ) && !is_array( $arg ) && $this->startswith( trim( $arg ), "{" ) ){
$attributes = json_decode( $arg, true );
}elseif( is_int( $arg ) && $arg > 0 ){
$id = $arg;
}elseif( is_array( $arg ) && count( $arg ) == 1 ){
$addto = $arg[0];
}
}
}
if( $id === $addto ){
$id = null;
}
$element = array( "id"=>$id, "name"=>$name, "attributes"=>$attributes, "content"=>$content, "addto"=>$this->parent_id( $addto ) );
$this->__id++;
if( $this->__id == 0 ){
$element['id'] = 0;
$element['addto'] = -1;
}
$this->__config[$this->__id] = $element;
return $this;
}
public function parent_id( $id ){
$__id = $this->__id;
foreach( $this->__config as $parentid=>$value ){
if( is_int($value["id"]) && $id === $value["id"] ){
$__id = $parentid;
}
}
return $__id;
}
public function find_id( $id ){
foreach( $this->__config as $findid=>$value ){
if( $id == $value["id"] ){
return true;
}
}
return false;
}
public function startswith( $str, $prefix ){
return strpos( $str, $prefix ) === 0;
}
public function content( $content ){
$this->__config[$this->__id]['content'] = $content;
return $this;
}
public function render(){
$__list = array();
foreach( $this->__config as $id=>$value ){
$__list[] = $value['addto'];
}
$__list = array_unique($__list);
arsort($__list);
$__list = array_values($__list);
return $this->render_r( $__list, 0 );
}
private function render_r( $list, $currentid ){
$contents = array();
$parent = $list[$currentid];
foreach( $this->__config as $id=>$element ){
if( $element['addto'] == $parent ){
$contents[] = isset($element['rendered']) ? $element['rendered'] : self::element( $element['name'], $element['attributes'], $element['content'] );
}
}
$this->__config[$parent]['rendered'] = self::element( $this->__config[$parent]['name'], $this->__config[$parent]['attributes'], implode( "\n", $contents )."\n".$this->__config[$parent]['content'] );
if( $parent == 0 ){
return $this->__config[$parent]['rendered'];
}else{
$currentid++;
return $this->render_r( $list, $currentid );
}
}
}