1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\IR; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ValueObject |
14
|
|
|
*/ |
15
|
|
|
class ValueObject implements |
16
|
|
|
\ArrayAccess, |
17
|
|
|
\Serializable, |
18
|
|
|
\JsonSerializable, |
19
|
|
|
DefinitionInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array|mixed[] |
23
|
|
|
*/ |
24
|
|
|
protected $attributes = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ValueObject constructor. |
28
|
|
|
* @param array $attributes |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $attributes = []) |
31
|
|
|
{ |
32
|
|
|
foreach ($this->attributes as $key => $value) { |
33
|
|
|
$this->set($key, $value); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
foreach ($attributes as $key => $value) { |
37
|
|
|
$this->set($key, $value); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name |
43
|
|
|
* @param mixed $value |
44
|
|
|
*/ |
45
|
|
|
public function set(string $name, $value): void |
46
|
|
|
{ |
47
|
|
|
$this->attributes[$name] = \is_array($value) ? new ValueObject($value) : $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* @return mixed|null |
53
|
|
|
*/ |
54
|
|
|
public function __get(string $name) |
55
|
|
|
{ |
56
|
|
|
return $this->get($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* @param mixed $value |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function __set(string $name, $value): void |
65
|
|
|
{ |
66
|
|
|
$this->set($name, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @return mixed|null |
72
|
|
|
*/ |
73
|
|
|
public function get(string $name) |
74
|
|
|
{ |
75
|
|
|
return $this->attributes[$name] ?? null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function has(string $name): bool |
83
|
|
|
{ |
84
|
|
|
return isset($this->attributes[$name]) || \array_key_exists($name, $this->attributes); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected function delete(string $name): bool |
92
|
|
|
{ |
93
|
|
|
if (isset($this->$name)) { |
94
|
|
|
unset($this->attributes[$name]); |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function __isset(string $name): bool |
107
|
|
|
{ |
108
|
|
|
return $this->has($name); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $name |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function __unset(string $name): void |
116
|
|
|
{ |
117
|
|
|
$this->delete($name); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function jsonSerialize(): array |
124
|
|
|
{ |
125
|
|
|
return $this->attributes; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function serialize(): string |
132
|
|
|
{ |
133
|
|
|
return \serialize($this->attributes); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $serialized |
138
|
|
|
*/ |
139
|
|
|
public function unserialize($serialized): void |
140
|
|
|
{ |
141
|
|
|
$this->attributes = \unserialize($serialized, [ |
142
|
|
|
'allowed_classes' => true, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string|int $offset |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function offsetExists($offset): bool |
151
|
|
|
{ |
152
|
|
|
return $this->has((string)$offset); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string|int $offset |
157
|
|
|
* @return mixed|null |
158
|
|
|
*/ |
159
|
|
|
public function offsetGet($offset) |
160
|
|
|
{ |
161
|
|
|
return $this->get((string)$offset); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string|int $offset |
166
|
|
|
* @param mixed $value |
167
|
|
|
*/ |
168
|
|
|
public function offsetSet($offset, $value): void |
169
|
|
|
{ |
170
|
|
|
$this->set((string)$offset, $value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string|int $offset |
175
|
|
|
*/ |
176
|
|
|
public function offsetUnset($offset): void |
177
|
|
|
{ |
178
|
|
|
$this->delete((string)$offset); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|