-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.php
160 lines (147 loc) · 5.75 KB
/
Menu.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
<?php
// Lucas Tiago de Moraes
// include class DHX
include_once 'DHX.php';
// class Menu
class Menu extends DHX {
private $document; // ref document
private $menu; // ref menu
private $array = array(); // array
private $num = 0;
// auto construct
function __construct($value = ''){
if($value){
$this->encoding($value);
}
$this->document = $this->document();
$this->menu = $this->document->appendChild(new DOMElement('menu'));
}
// public item
public function item(){
$data = func_get_args();
foreach($data as $row){
$total = count($this->array);
$item = '';
if($total > 0){
$item = $this->array[$this->num]->appendChild(new DOMElement('item'));
}else{
$item = $this->menu->appendChild(new DOMElement('item'));
}
foreach($row as $key => $value){
if($key == 'item'){
$this->itemArray($item, $value);
}elseif($key == 'hotkey'){
$hotkey = $item->appendChild(new DOMElement('hotkey'));
$hotkey->appendChild($this->document->createTextNode($value));
}elseif($key == 'userdata'){
if(array_key_exists(0, $value)){
foreach($value as $r){
$userdata = $item->appendChild(new DOMElement('userdata'));
foreach($r as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$userdata = $item->appendChild(new DOMElement('userdata'));
foreach($value as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$item->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// private itemArray
private function itemArray($ref, $data){
foreach($data as $row){
$item = $ref->appendChild(new DOMElement('item'));
foreach($row as $key => $value){
if($key == 'item'){
$this->itemArray($item, $value);
}elseif($key == 'hotkey'){
$hotkey = $item->appendChild(new DOMElement('hotkey'));
$hotkey->appendChild($this->document->createTextNode($value));
}elseif($key == 'userdata'){
if(array_key_exists(0, $value)){
foreach($value as $r){
$userdata = $item->appendChild(new DOMElement('userdata'));
foreach($r as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$userdata = $item->appendChild(new DOMElement('userdata'));
foreach($value as $k => $v){
if($k == 'value'){
$userdata->appendChild($this->document->createTextNode($v));
}else{
$userdata->setAttributeNode(new DOMAttr($k, $v));
}
}
}
}else{
$item->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public userdata
public function userdata(){
$data = func_get_args();
foreach($data as $row){
$total = count($this->array);
$userdata = '';
if($total > 0){
$userdata = $this->array[$this->num]->appendChild(new DOMElement('userdata'));
}else{
$userdata = $this->menu->appendChild(new DOMElement('userdata'));
}
foreach($row as $key => $value){
if($key == 'value'){
$userdata->appendChild($this->document->createTextNode($value));
}else{
$userdata->setAttributeNode(new DOMAttr($key, $value));
}
}
}
}
// public start
public function start($data){
$total = count($this->array);
$item = '';
if($total > 0){
$item = $this->array[$this->num]->appendChild(new DOMElement('item'));
}else{
$item = $this->menu->appendChild(new DOMElement('item'));
}
foreach($data as $key => $value){
$item->setAttributeNode(new DOMAttr($key, $value));
}
$this->num++;
$this->array[$this->num] = $item;
}
// public end
public function end(){
$total = count($this->array);
unset($this->array[$this->num]);
$this->num--;
}
// public result
public function result(){
return $this->document->saveXML();
}
}