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