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\Frontend\ValueObject; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ValueObject |
14
|
|
|
*/ |
15
|
|
|
class ValueObject extends BaseStruct |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* ValueObject constructor. |
19
|
|
|
* @param iterable $attributes |
20
|
|
|
*/ |
21
|
|
|
public function __construct(iterable $attributes = []) |
22
|
|
|
{ |
23
|
|
|
$this->setAttributes($attributes); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param iterable $attributes |
28
|
|
|
*/ |
29
|
|
|
public function setAttributes(iterable $attributes): void |
30
|
|
|
{ |
31
|
|
|
foreach ($attributes as $attribute => $value) { |
32
|
|
|
$this->set($attribute, $value); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string|int $attribute |
38
|
|
|
* @param mixed $value |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function set($attribute, $value): void |
42
|
|
|
{ |
43
|
|
|
$this->value[$attribute] = new ScalarValue($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function toArray(): array |
50
|
|
|
{ |
51
|
|
|
return \array_map(function (BaseStruct $value) { |
52
|
|
|
return $value instanceof ValueObject ? $value->toArray() : $value->getValue(); |
53
|
|
|
}, $this->value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getAttributes(): array |
60
|
|
|
{ |
61
|
|
|
return $this->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|int $attribute |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
|
|
public function __get($attribute) |
69
|
|
|
{ |
70
|
|
|
return $this->get($attribute); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|int $attribute |
75
|
|
|
* @param mixed $value |
76
|
|
|
*/ |
77
|
|
|
public function __set($attribute, $value) |
78
|
|
|
{ |
79
|
|
|
$this->set($attribute, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string|int $attribute |
84
|
|
|
* @return mixed|null |
85
|
|
|
*/ |
86
|
|
|
public function get($attribute) |
87
|
|
|
{ |
88
|
|
|
return $this->value[$attribute] ?? new ScalarValue(null); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function jsonSerialize(): array |
95
|
|
|
{ |
96
|
|
|
return $this->toArray(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.