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
|
|
|
* Get some data by pretty name |
34
|
|
|
* |
35
|
|
|
* @param string $name name for access |
36
|
|
|
* @param mixed $default used for non safe request, if we dont find answer |
37
|
|
|
* @return mixed |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function get(string $name, $default = null) |
41
|
|
|
{ |
42
|
|
|
return GetterHelper::get($this->data, $name, $this->safe, $default); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Magic proxy call to data |
47
|
|
|
* @param string $name |
48
|
|
|
* @param array $arguments |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function __call($name, $arguments) |
52
|
|
|
{ |
53
|
|
|
// @2DO: check if method exist |
54
|
|
|
return call_user_func_array([$this->data, $name], $arguments); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Magic isset |
59
|
|
|
* @param string $name |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function __isset($name) |
63
|
|
|
{ |
64
|
|
|
return !is_null($this->get($name)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Magic getter, sugar for get() |
69
|
|
|
* @param string $name |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function __get($name) |
73
|
|
|
{ |
74
|
|
|
return $this->get($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get count of data fields, just sugar for data methods |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function count(): int |
83
|
|
|
{ |
84
|
|
|
return count($this->data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Json serialise data - here just data object/array |
89
|
|
|
* @return type |
90
|
|
|
*/ |
91
|
|
|
public function jsonSerialize() |
92
|
|
|
{ |
93
|
|
|
return $this->data; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get iterator |
98
|
|
|
* |
99
|
|
|
* @return \Traversable |
100
|
|
|
*/ |
101
|
|
|
public function getIterator(): \Traversable |
102
|
|
|
{ |
103
|
|
|
return GetterHelper::getIterator($this->data); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get keys list |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function keys(): array |
112
|
|
|
{ |
113
|
|
|
return GetterHelper::getKeys($this->data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sugar for array access is_set |
118
|
|
|
* |
119
|
|
|
* @param type $offset |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function offsetExists($offset): bool |
123
|
|
|
{ |
124
|
|
|
return $this->__isset($offset); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sugar ArrayAccess getter |
129
|
|
|
* |
130
|
|
|
* @param type $offset |
131
|
|
|
* @return type |
132
|
|
|
*/ |
133
|
|
|
public function offsetGet($offset) |
134
|
|
|
{ |
135
|
|
|
return $this->get($offset); |
136
|
|
|
} |
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
|
|
|
public function title(): string |
148
|
|
|
{ |
149
|
|
|
if(is_array($this->data)) { |
150
|
|
|
return 'Some array...'; |
151
|
|
|
} elseif(is_object($this->data) and is_a($this->data, ModelInterface::class)) { |
152
|
|
|
return $this->data->title(); |
153
|
|
|
} else { |
154
|
|
|
return 'Object #'.spl_object_id($this->data); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function fieldsInfo(): array |
159
|
|
|
{ |
160
|
|
|
if(is_object($this->data) and is_a($this->data, ModelInterface::class)) { |
161
|
|
|
return $this->data->fieldsInfo(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$info = []; |
165
|
|
|
foreach($this->keys() as $key) { |
166
|
|
|
$info[$key] = []; |
167
|
|
|
} |
168
|
|
|
return $info; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Dummy method for interface only |
173
|
|
|
* |
174
|
|
|
* @param mixed $offset |
175
|
|
|
* @param mixed $value |
176
|
|
|
* @return void |
177
|
|
|
* @throws \RuntimeException |
178
|
|
|
*/ |
179
|
|
|
public function offsetSet($offset, $value): void |
180
|
|
|
{ |
181
|
|
|
throw new \RuntimeException('DataWraper is just read-only wrapper'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Dummy method for interface only |
186
|
|
|
* |
187
|
|
|
* @param mixed $offset |
188
|
|
|
* @return void |
189
|
|
|
* @throws type |
190
|
|
|
*/ |
191
|
|
|
public function offsetUnset($offset): void |
192
|
|
|
{ |
193
|
|
|
throw new \RuntimeException('DataWraper is just read-only wrapper'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |