1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Base |
7
|
|
|
* @package Payever\Core |
8
|
|
|
* @author payever GmbH <[email protected]> |
9
|
|
|
* @copyright 2017-2021 payever GmbH |
10
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
11
|
|
|
* @link https://docs.payever.org/shopsystems/api/getting-started |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Payever\ExternalIntegration\Core\Base; |
15
|
|
|
|
16
|
|
|
use Payever\ExternalIntegration\Core\Helper\StringHelper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class implements and describes functions of Message Entity |
20
|
|
|
* @SuppressWarnings(PHPMD.NumberOfChildren) |
21
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
22
|
|
|
*/ |
23
|
|
|
abstract class MessageEntity implements MessageEntityInterface, \ArrayAccess |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Whether entity fields must be underscored when converting to array/json |
27
|
|
|
*/ |
28
|
|
|
const UNDERSCORE_ON_SERIALIZATION = true; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function __construct($data = null) |
34
|
|
|
{ |
35
|
|
|
if ($data) { |
36
|
|
|
$this->load($data); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function load($data) |
44
|
|
|
{ |
45
|
|
|
foreach ($data as $key => $value) { |
46
|
|
|
$function = StringHelper::camelize('set_' . $key); |
47
|
|
|
|
48
|
|
|
$this->{$function}($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function toString() |
58
|
|
|
{ |
59
|
|
|
return json_encode($this->toArray()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
65
|
|
|
*/ |
66
|
|
|
public function toArray($object = null) |
67
|
|
|
{ |
68
|
|
|
$result = []; |
69
|
|
|
|
70
|
|
|
if ($object === null) { |
71
|
|
|
$object = get_object_vars($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($object as $property => $value) { |
75
|
|
|
if (is_null($value)) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (static::UNDERSCORE_ON_SERIALIZATION && !is_integer($property)) { |
80
|
|
|
$property = StringHelper::underscore($property); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (is_array($value)) { |
84
|
|
|
$result[$property] = $this->toArray($value); |
85
|
|
|
} elseif ($value instanceof MessageEntity) { |
86
|
|
|
$result[$property] = $value->toArray(); |
87
|
|
|
} elseif ($value instanceof \DateTime) { |
88
|
|
|
$result[$property] = $value->format(DATE_ATOM); |
89
|
|
|
} else { |
90
|
|
|
$result[$property] = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function getRequired() |
101
|
|
|
{ |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function isValid() |
109
|
|
|
{ |
110
|
|
|
foreach ($this->getRequired() as $property) { |
111
|
|
|
if (static::UNDERSCORE_ON_SERIALIZATION) { |
112
|
|
|
$property = StringHelper::camelize($property); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->{$property} === null) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Implements magic method __toString() |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function __toString() |
129
|
|
|
{ |
130
|
|
|
return $this->toString(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set/Get attribute wrapper |
135
|
|
|
* |
136
|
|
|
* @param string $method |
137
|
|
|
* @param array $args |
138
|
|
|
* |
139
|
|
|
* @return self|bool|mixed |
140
|
|
|
*/ |
141
|
|
|
public function __call($method, $args) |
142
|
|
|
{ |
143
|
|
|
$property = lcfirst(substr($method, 3)); |
144
|
|
|
|
145
|
|
|
switch (substr($method, 0, 3)) { |
146
|
|
|
case 'get': |
147
|
|
|
if (property_exists($this, $property)) { |
148
|
|
|
return $this->{$property}; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return null; |
152
|
|
|
case 'set': |
153
|
|
|
if (property_exists($this, $property)) { |
154
|
|
|
$this->{$property} = isset($args[0]) ? $args[0] : null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Implementation of \ArrayAccess::offsetSet() |
165
|
|
|
* |
166
|
|
|
* @param string $offset |
167
|
|
|
* @param mixed $value |
168
|
|
|
* @return void |
169
|
|
|
* @link http://www.php.net/manual/en/arrayaccess.offsetset.php |
170
|
|
|
*/ |
171
|
|
|
#[\ReturnTypeWillChange] |
172
|
|
|
public function offsetSet($offset, $value) |
173
|
|
|
{ |
174
|
|
|
$property = StringHelper::camelize($offset); |
175
|
|
|
|
176
|
|
|
$this->{$property} = $value; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Implementation of \ArrayAccess::offsetExists() |
181
|
|
|
* |
182
|
|
|
* @param string $offset |
183
|
|
|
* @return bool |
184
|
|
|
* @link http://www.php.net/manual/en/arrayaccess.offsetexists.php |
185
|
|
|
*/ |
186
|
|
|
#[\ReturnTypeWillChange] |
187
|
|
|
public function offsetExists($offset) |
188
|
|
|
{ |
189
|
|
|
$property = StringHelper::camelize($offset); |
190
|
|
|
|
191
|
|
|
return property_exists($this, $property); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Implementation of \ArrayAccess::offsetUnset() |
196
|
|
|
* |
197
|
|
|
* @param string $offset |
198
|
|
|
* @return void |
199
|
|
|
* @link http://www.php.net/manual/en/arrayaccess.offsetunset.php |
200
|
|
|
*/ |
201
|
|
|
#[\ReturnTypeWillChange] |
202
|
|
|
public function offsetUnset($offset) |
203
|
|
|
{ |
204
|
|
|
$property = StringHelper::camelize($offset); |
205
|
|
|
|
206
|
|
|
unset($this->{$property}); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Implementation of \ArrayAccess::offsetGet() |
211
|
|
|
* |
212
|
|
|
* @param string $offset |
213
|
|
|
* @return mixed |
214
|
|
|
* @link http://www.php.net/manual/en/arrayaccess.offsetget.php |
215
|
|
|
*/ |
216
|
|
|
#[\ReturnTypeWillChange] |
217
|
|
|
public function offsetGet($offset) |
218
|
|
|
{ |
219
|
|
|
$property = StringHelper::camelize($offset); |
220
|
|
|
|
221
|
|
|
if (property_exists($this, $property)) { |
222
|
|
|
return $this->{$property}; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|