1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright (c) 2019 Mendel <[email protected]> |
4
|
|
|
* @license see license.txt |
5
|
|
|
*/ |
6
|
|
|
namespace drycart\data; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Wrapper for pretty access to field |
10
|
|
|
* Used for deep access to some data at some unknown data |
11
|
|
|
*/ |
12
|
|
|
class DataWrapper implements ModelInterface, \IteratorAggregate, \ArrayAccess |
13
|
|
|
{ |
14
|
|
|
protected $data; |
15
|
|
|
protected $safe = true; |
16
|
|
|
protected $titleKey = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param mixed $data Data for we access. Array, object etc... |
20
|
|
|
* @param bool $safe if true - Exception for not exist fields, else NULL |
21
|
|
|
* @param string|null $titleKey if not null - used as key for title method |
22
|
|
|
* @throws \UnexpectedValueException |
23
|
|
|
*/ |
24
|
|
|
public function __construct($data, bool $safe = true, ?string $titleKey = null) |
25
|
|
|
{ |
26
|
|
|
if(!is_array($data) and ! is_object($data)) { |
27
|
|
|
throw new \UnexpectedValueException('DataWraper can wrap only array or object'); |
28
|
|
|
} |
29
|
|
|
$this->data = $data; |
30
|
|
|
$this->safe = $safe; |
31
|
|
|
$this->titleKey = $titleKey; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Magic proxy call to data |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @param array $arguments |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function __call($name, $arguments) |
42
|
|
|
{ |
43
|
|
|
// @2DO: check if method exist |
44
|
|
|
return call_user_func_array([$this->data, $name], $arguments); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Magic isset |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public function __isset($name) |
54
|
|
|
{ |
55
|
|
|
return !is_null($this->__get($name)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get some data by pretty name |
60
|
|
|
* |
61
|
|
|
* @param string $name name for access |
62
|
|
|
* @return mixed |
63
|
|
|
* @throws \UnexpectedValueException |
64
|
|
|
*/ |
65
|
|
|
public function __get($name) |
66
|
|
|
{ |
67
|
|
|
return GetterHelper::get($this->data, $name, $this->safe); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get count of data fields, just sugar for data methods |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function count(): int |
76
|
|
|
{ |
77
|
|
|
return count($this->data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Json serialise data - here just data object/array |
82
|
|
|
* |
83
|
|
|
* @return object|array |
84
|
|
|
*/ |
85
|
|
|
public function jsonSerialize() |
86
|
|
|
{ |
87
|
|
|
return $this->data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get iterator |
92
|
|
|
* |
93
|
|
|
* @return \Traversable |
94
|
|
|
*/ |
95
|
|
|
public function getIterator(): \Traversable |
96
|
|
|
{ |
97
|
|
|
return GetterHelper::getIterator($this->data); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get keys list |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function keys(): array |
106
|
|
|
{ |
107
|
|
|
return GetterHelper::getKeys($this->data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Sugar for array access is_set |
112
|
|
|
* |
113
|
|
|
* @param type $offset |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function offsetExists($offset): bool |
117
|
|
|
{ |
118
|
|
|
return $this->__isset($offset); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sugar ArrayAccess getter |
123
|
|
|
* |
124
|
|
|
* @param type $offset |
125
|
|
|
* @return type |
126
|
|
|
*/ |
127
|
|
|
public function offsetGet($offset) |
128
|
|
|
{ |
129
|
|
|
return $this->__get($offset); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Human readable name for some field (by key) |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function fieldLabel(string $key): string |
139
|
|
|
{ |
140
|
|
|
if(is_object($this->data) and is_a($this->data, ModelInterface::class)) { |
141
|
|
|
return $this->data->fieldLabel($key); |
142
|
|
|
} else { |
143
|
|
|
return StrHelper::key2Label($key); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Dummy title if not exist |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function title(): string |
153
|
|
|
{ |
154
|
|
|
if(is_object($this->data) and is_a($this->data, ModelInterface::class)) { |
155
|
|
|
return $this->data->title(); |
156
|
|
|
} elseif (!empty($this->titleKey)) { |
157
|
|
|
return $this[$this->titleKey]; |
158
|
|
|
} elseif(is_array($this->data)) { |
159
|
|
|
return 'Some array...'; |
160
|
|
|
} else { |
161
|
|
|
return 'Object #'.spl_object_id($this->data); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Dummy fieldsInfo if not exist |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
public function fieldsInfo(): array |
171
|
|
|
{ |
172
|
|
|
if(is_object($this->data) and is_a($this->data, ModelInterface::class)) { |
173
|
|
|
return $this->data->fieldsInfo(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$info = []; |
177
|
|
|
foreach($this->keys() as $key) { |
178
|
|
|
$info[$key] = []; |
179
|
|
|
} |
180
|
|
|
return $info; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Magic setter for ArrayAccess |
185
|
|
|
* |
186
|
|
|
* @param mixed $offset |
187
|
|
|
* @param mixed $value |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function offsetSet($offset, $value): void |
191
|
|
|
{ |
192
|
|
|
if(is_array($this->data) or is_a($this->data, \ArrayAccess::class)) { |
193
|
|
|
$this->data[$offset] = $value; |
194
|
|
|
} else { |
195
|
|
|
$this->data->$offset = $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Magic unset for ArrayAccess |
201
|
|
|
* |
202
|
|
|
* @param mixed $offset |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function offsetUnset($offset): void |
206
|
|
|
{ |
207
|
|
|
unset($this->$offset); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Magic unset |
212
|
|
|
* |
213
|
|
|
* @param string $name |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
public function __unset($name) : void |
217
|
|
|
{ |
218
|
|
|
if(is_array($this->data) or is_a($this->data, \ArrayAccess::class)) { |
219
|
|
|
unset($this->data[$name]); |
220
|
|
|
} else { |
221
|
|
|
unset($this->data->$name); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |