1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yandex PHP Library |
4
|
|
|
* |
5
|
|
|
* @copyright NIX Solutions Ltd. |
6
|
|
|
* @link https://github.com/nixsolutions/yandex-php-library |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @namespace |
11
|
|
|
*/ |
12
|
|
|
namespace Yandex\Common; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Model |
16
|
|
|
* |
17
|
|
|
* @package Yandex\Common |
18
|
|
|
*/ |
19
|
|
|
abstract class Model |
20
|
|
|
{ |
21
|
|
|
protected $mappingClasses = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Contains property name mappings. |
25
|
|
|
* |
26
|
|
|
* [ |
27
|
|
|
* 'data_array_property1' => 'objectProperty1', |
28
|
|
|
* 'data_array_property2' => 'objectProperty2', |
29
|
|
|
* ] |
30
|
|
|
* |
31
|
|
|
* Data array property uses as keys |
32
|
|
|
* because there is can be more then one rule per object property |
33
|
|
|
* |
34
|
|
|
* f.g. $data['nmodels'] and ['modelsnum'] should map in modelsCount property. |
35
|
|
|
* Otherwise not unique array keys cause remapping of properties. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $propNameMap = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param array $data |
45
|
|
|
*/ |
46
|
152 |
|
public function __construct($data = []) |
47
|
|
|
{ |
48
|
152 |
|
$this->fromArray($data); |
49
|
152 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set from XML |
53
|
|
|
* |
54
|
|
|
* @param \SimpleXMLIterator $data |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function fromXml(\SimpleXMLIterator $data) |
58
|
|
|
{ |
59
|
|
|
//todo: refactor fromXml() |
60
|
|
|
if (method_exists($this, 'add')) { |
61
|
|
|
for ($data->rewind(); $data->valid(); $data->next()) { |
62
|
|
|
$this->add($data->current()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//collect attributes |
69
|
|
|
if ($data->attributes()->count() > 0) { |
70
|
|
|
foreach ($data->attributes() as $key => $attribute) { |
71
|
|
|
$propertyName = $key; |
72
|
|
|
$ourPropertyName = array_search($propertyName, $this->propNameMap, true); |
73
|
|
|
|
74
|
|
|
if (false !== $ourPropertyName) { |
75
|
|
|
$propertyName = $ourPropertyName; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (property_exists($this, $propertyName)) { |
79
|
|
|
$this->{$propertyName} = (string)$attribute; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//collect node data |
85
|
|
|
for ($data->rewind(); $data->valid(); $data->next()) { |
86
|
|
|
$propertyName = $data->key(); |
87
|
|
|
$ourPropertyName = array_search($propertyName, $this->propNameMap, true); |
88
|
|
|
|
89
|
|
|
if (false !== $ourPropertyName) { |
90
|
|
|
$propertyName = $ourPropertyName; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
if (property_exists($this, $propertyName)) { |
|
|
|
|
94
|
|
|
if (array_key_exists($propertyName, $this->mappingClasses)) { |
95
|
|
|
$this->{$propertyName} = new $this->mappingClasses[$propertyName]($data->current()); |
96
|
|
|
} else { |
97
|
|
|
$this->{$propertyName} = (string)$data->current(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set from array |
107
|
|
|
* |
108
|
|
|
* @param array $data |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
150 |
|
public function fromArray($data) |
112
|
|
|
{ |
113
|
150 |
|
foreach ($data as $key => $val) { |
114
|
114 |
|
if (is_int($key)) { |
115
|
67 |
|
if (method_exists($this, "add")) { |
116
|
67 |
|
$this->add($val); |
|
|
|
|
117
|
67 |
|
} |
118
|
67 |
|
} |
119
|
|
|
|
120
|
114 |
|
$propertyName = $key; |
121
|
114 |
|
$ourPropertyName = array_search($propertyName, $this->propNameMap); |
122
|
|
|
|
123
|
114 |
|
if ($ourPropertyName && isset($data[$ourPropertyName])) { |
124
|
|
|
$propertyName = $ourPropertyName; |
125
|
|
|
} |
126
|
|
|
|
127
|
114 |
|
if (!empty($this->propNameMap)) { |
128
|
79 |
|
if (array_key_exists($key, $this->propNameMap)) { |
129
|
73 |
|
$propertyName = $this->propNameMap[$key]; |
130
|
73 |
|
} |
131
|
79 |
|
} |
132
|
|
|
|
133
|
114 |
|
if (property_exists($this, $propertyName)) { |
134
|
106 |
View Code Duplication |
if (isset($this->mappingClasses[$propertyName])) { |
|
|
|
|
135
|
78 |
|
$this->{$propertyName} = new $this->mappingClasses[$propertyName]($val); |
136
|
78 |
|
} else { |
137
|
106 |
|
$this->{$propertyName} = $val; |
138
|
|
|
} |
139
|
106 |
|
} |
140
|
150 |
|
} |
141
|
|
|
|
142
|
150 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set from json |
147
|
|
|
* |
148
|
|
|
* @param string $json |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
1 |
|
public function fromJson($json) |
152
|
|
|
{ |
153
|
1 |
|
$this->fromArray(json_decode($json, true)); |
154
|
|
|
|
155
|
1 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get array from object |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
42 |
|
public function toArray() |
164
|
|
|
{ |
165
|
42 |
|
return $this->toArrayRecursive($this); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get array from object |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
1 |
|
public function toJson() |
174
|
|
|
{ |
175
|
1 |
|
return json_encode($this->toArrayRecursive($this)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get array from object |
180
|
|
|
* |
181
|
|
|
* @param array|object $data |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
40 |
|
protected function toArrayRecursive($data) |
185
|
|
|
{ |
186
|
40 |
|
if (is_array($data) || is_object($data)) { |
187
|
40 |
|
$result = []; |
188
|
40 |
|
foreach ($data as $key => $value) { |
189
|
38 |
|
if ($key === "mappingClasses" || $key === "propNameMap") { |
190
|
38 |
|
continue; |
191
|
|
|
} |
192
|
38 |
|
$propNameMap = $key; |
193
|
38 |
|
$obj = $this; |
194
|
38 |
|
if (is_object($data)) { |
195
|
38 |
|
$obj = $data; |
196
|
38 |
|
} |
197
|
|
|
|
198
|
38 |
|
if (property_exists($obj, $propNameMap)) { |
199
|
38 |
|
$ourPropertyName = array_search($propNameMap, $obj->propNameMap); |
200
|
|
|
|
201
|
38 |
|
if ($ourPropertyName) { |
202
|
28 |
|
$propNameMap = $ourPropertyName; |
203
|
28 |
|
} |
204
|
38 |
|
} |
205
|
|
|
|
206
|
38 |
|
if (is_object($value) && method_exists($value, "getAll")) { |
207
|
9 |
|
if (method_exists($obj, 'toArrayRecursive')) { |
208
|
9 |
|
$result[$propNameMap] = $obj->toArrayRecursive($value->getAll()); |
209
|
9 |
|
} |
210
|
38 |
|
} elseif ($value !== null) { |
211
|
18 |
|
if (method_exists($obj, 'toArrayRecursive')) { |
212
|
18 |
|
$result[$propNameMap] = $obj->toArrayRecursive($value); |
213
|
18 |
|
} |
214
|
18 |
|
} |
215
|
40 |
|
} |
216
|
|
|
|
217
|
40 |
|
return $result; |
218
|
|
|
} |
219
|
|
|
|
220
|
18 |
|
return $data; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: