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