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
|
|
|
DefinitionValueObject |
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 self($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
|
|
|
$result = []; |
126
|
|
|
|
127
|
|
|
foreach ($this->attributes as $key => $value) { |
128
|
|
|
switch (true) { |
129
|
|
|
case \is_scalar($value): |
130
|
|
|
case \is_array($value): |
131
|
|
|
case $value instanceof \JsonSerializable: |
|
|
|
|
132
|
|
|
break; |
133
|
|
|
case \is_object($value) && \method_exists($value, '__toString'): |
134
|
|
|
$value = (string)$value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$result[$key] = $value; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function serialize(): string |
147
|
|
|
{ |
148
|
|
|
return \serialize($this->attributes); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $serialized |
153
|
|
|
*/ |
154
|
|
|
public function unserialize($serialized): void |
155
|
|
|
{ |
156
|
|
|
$this->attributes = \unserialize($serialized, [ |
157
|
|
|
'allowed_classes' => true, |
158
|
|
|
]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string|int $offset |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function offsetExists($offset): bool |
166
|
|
|
{ |
167
|
|
|
return $this->has((string)$offset); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string|int $offset |
172
|
|
|
* @return mixed|null |
173
|
|
|
*/ |
174
|
|
|
public function offsetGet($offset) |
175
|
|
|
{ |
176
|
|
|
return $this->get((string)$offset); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string|int $offset |
181
|
|
|
* @param mixed $value |
182
|
|
|
*/ |
183
|
|
|
public function offsetSet($offset, $value): void |
184
|
|
|
{ |
185
|
|
|
if ($offset === null) { |
186
|
|
|
$offset = \count($this->attributes); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->set((string)$offset, $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string|int $offset |
194
|
|
|
*/ |
195
|
|
|
public function offsetUnset($offset): void |
196
|
|
|
{ |
197
|
|
|
$this->delete((string)$offset); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.