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