/
Wrapper.class.php
184 lines (160 loc) · 5.53 KB
/
Wrapper.class.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
173
174
175
176
177
178
179
180
181
182
183
184
<?php
abstract class Wrapper implements IteratorAggregate {
public abstract function &getValue();
public abstract function setValue($value);
public abstract function setValueRef(&$value);
public function isNull() {
return $this->getValue() == null;
}
public function isArray() {
$value = $this->getValue();
return $value != null && is_array($value);
}
public function size() {
return count($this->getValue());
}
public function hasKey($key) {
$data = &$this->getValue();
if ($data != null && is_array($data))
return array_key_exists($key, $data);
return false;
}
public function has($value) {
static::unwrapValue($value);
$data = &$this->getValue();
if ($data != null && is_array($data))
return in_array($value, $data);
return false;
}
public function set($key, $value, $returnRaw = false) {
static::unwrapValue($value);
$data = &$this->getValue();
if ($data == null || !is_array($data)) {
$data = array();
$data[$key] = $value;
$this->setValue($data);
} else
$data[$key] = $value;
if ($returnRaw)
return $data;
return $this;
}
public function setRef($key, &$value, $returnRaw = false) {
static::unwrapValue($value);
$data = &$this->getValue();
if ($data == null || !is_array($data)) {
$data = array();
$data[$key] = &$value;
$this->setValue($data);
} else
$data[$key] = &$value;
if ($returnRaw)
return $data;
return $this;
}
public function push($value, $returnRaw = false) {
static::unwrapValue($value);
$data = &$this->getValue();
if ($data == null || !is_array($data)) {
$data = array();
$data[] = $value;
$this->setValue($data);
} else
$data[] = $value;
if ($returnRaw)
return $data;
return $this;
}
public function pushRef(&$value, $returnRaw = false) {
static::unwrapValue($value);
$data = &$this->getValue();
if ($data == null || !is_array($data)) {
$data = array();
$data[] = &$value;
$this->setValue($data);
} else
$data[] = &$value;
if ($returnRaw)
return $data;
return $this;
}
public function &get($key, $default = null) {
static::unwrapValue($default);
$data = &$this->getValue();
if ($data == null || !array_key_exists($key, $data))
return $default;
return $data[$key];
}
public function delete($key, $returnRaw = false) {
$data = &$this->getValue();
if (is_array($data) && array_key_exists($key, $data))
unset($data[$key]);
if ($returnRaw)
return $data;
return $this;
}
public function getWrapped($key, $create = true, $setParent = false) {
if ($create || $this->getValue() != null)
return new DataWrapper($this, $key, $setParent);
return null;
}
public function setValueComplex($complexKey, $value) {
return $this->getWrappedComplex($complexKey)->setValue($value);
}
public function getWrappedComplex($complexKey, $create = true, $setParent = false) {
$keys = explode('.', $complexKey);
return array_reduce($keys, function($obj, $key) use ($create, $setParent) {
return is_null($obj) ? $obj : $obj->getWrapped($key, $create, $setParent);
}, $this);
}
public function filter($callback) {
$data = &$this->getValue();
if ($data != null && is_array($data))
$toFilter = &$data;
else
$toFilter = array($data);
return new ValueWrapper(array_filter($toFilter, function($value, $key) use ($callback) {
return $callback($key, $value, new DataWrapper($this, $key), new ValueWrapperRef($value));
}, ARRAY_FILTER_USE_BOTH));
}
public function map($callback) {
$data = &$this->getValue();
if ($data != null && is_array($data))
$toMap = &$data;
else
$toMap = array($data);
$mapped = new ValueWrapper(array());
foreach($toMap as $key => $value) {
$mappedVal = $callback($key, $value, new DataWrapper($this, $key), new ValueWrapperRef($value));
static::unwrapValue($mappedVal);
$mapped->set($key, $mappedVal);
}
return $mapped;
}
public function sort($callback) {
$data = &$this->getValue();
if ($data != null && is_array($data))
$toSort = $data;
else
$toSort = array($data);
usort($toSort, function($v1, $v2) use ($callback) {
return $callback(new ValueWrapperRef($v1), new ValueWrapperRef($v2));
});
return new ValueWrapper($toSort);
}
protected static function unwrapValue(&$value) {
if ($value != null && is_object($value) && is_subclass_of($value, 'Wrapper'))
$value = $value->getValue();
}
/**
* Modifying the value of the current wrapped object while iterating will result in undefined behaviour.
*/
public function getIterator() {
$data = &$this->getValue();
if ($data != null && is_array($data)) {
foreach($data as $key => $value)
yield $key => (new DataWrapper($this, $key));
}
}
}
?>