This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexArrayWrapper.php
172 lines (142 loc) · 3.54 KB
/
ComplexArrayWrapper.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
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Class ComplexArrayWrapper
*/
class ComplexArrayWrapper {
/**
* @var array
*/
public $arrays;
/**
* @var array
*/
public $indices = array();
/**
* Name of the current array
*
* @var string
*/
public $array_name;
/**
* Create a new ComplexArray class. Equivalent to $class = new ComplexArrayWrapper();.
*
* @signature ComplexArrayWrapper newFromVoid();
* @return ComplexArrayWrapper
*/
public static function newFromVoid() {
return new ComplexArrayWrapper();
}
/**
* ComplexArrayWrapper constructor.
*/
public function __construct() {
global $wfDefinedArraysGlobal;
$this->arrays =& $wfDefinedArraysGlobal;
}
/**
* Set query array to $array_name. Returns false on failure.
*
* @signature $this|bool from( string $array_name );
* @param $array_name
* @return $this|bool
*/
public function on($array_name) {
if(!is_string($array_name)) {
return false;
}
if(!isset($this->arrays[$array_name])) {
$this->arrays[$array_name] = new ComplexArray([]);
}
$this->reset();
$this->array_name = $array_name;
$this->indices = [];
return $this;
}
/**
* Set the index.
*
* @param array $indices
* @return $this
*/
public function in(array $indices) {
$this->indices = $indices;
return $this;
}
/**
* Returns the value of the array.
*
* @signature array|bool getArrayValue( array $indices );
* @return array|bool
*/
public function get() {
if(!$this->array_name) {
return false;
}
if(!isset($this->arrays[$this->array_name])) {
return false;
}
$array = $this->arrays[$this->array_name]->getArray();
if(!$this->indices) {
return $array;
}
foreach($this->indices as $index) {
if(isset($array[$index])) {
$array = $array[$index];
} else {
return false;
}
}
return $array;
}
/**
* Define a new or overwrite an existing array, or set a value of a sub array, given indices.
*
* @signature bool set( mixed $value );
* @param $value
* @return bool
*/
public function set($value) {
if(!$this->array_name) {
return false;
}
if(!$this->indices) {
$this->arrays[$this->array_name] = new ComplexArray( $value );
return true;
}
$array = $this->arrays[$this->array_name]->getArray();
$temp = &$array;
foreach($this->indices as $index) {
if(!isset($temp[$index])) {
$temp[$index] = [];
}
$temp =& $temp[$index];
}
$temp = $value;
$this->arrays[$this->array_name] = new ComplexArray( $array );
return true;
}
/**
* Resets values in class.
*
* @signature $this reset();
* @return $this
*/
public function reset() {
unset($this->array_name);
unset($this->indices);
return $this;
}
/**
* Unsets the array.
*
* @signature bool unsetArray();
* @return bool
*/
public function unsetArray() {
if(!$this->array_name || $this->indices) {
return false;
}
unset($this->arrays[$this->array_name]);
return true;
}
}