1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ValueObject; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
|
8
|
|
|
abstract class ValueObject |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
protected $allValues = []; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
protected $exceptKeys = []; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $onlyKeys = []; |
18
|
|
|
|
19
|
|
|
public function __construct(array $parameters) |
20
|
|
|
{ |
21
|
|
|
$class = new ReflectionClass(static::class); |
22
|
|
|
|
23
|
|
|
$properties = $this->getPublicProperties($class); |
24
|
|
|
|
25
|
|
|
foreach ($properties as $property) { |
26
|
|
|
if ( |
27
|
|
|
! isset($parameters[$property->getName()]) |
28
|
|
|
&& ! $property->isNullable() |
29
|
|
|
) { |
30
|
|
|
throw ValueObjectError::uninitialized($property); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$value = $parameters[$property->getName()] ?? null; |
34
|
|
|
|
35
|
|
|
$property->set($value); |
36
|
|
|
|
37
|
|
|
unset($parameters[$property->getName()]); |
38
|
|
|
|
39
|
|
|
$this->allValues[$property->getName()] = $property->getValue($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (count($parameters)) { |
43
|
|
|
throw ValueObjectError::unknownProperties(array_keys($parameters), $class); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function all(): array |
48
|
|
|
{ |
49
|
|
|
return $this->allValues; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string ...$keys |
54
|
|
|
* |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
|
|
public function only(string ...$keys): ValueObject |
58
|
|
|
{ |
59
|
|
|
$valueObject = clone $this; |
60
|
|
|
|
61
|
|
|
$valueObject->onlyKeys = array_merge($this->onlyKeys, $keys); |
62
|
|
|
|
63
|
|
|
return $valueObject; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string ...$keys |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
|
|
public function except(string ...$keys): ValueObject |
72
|
|
|
{ |
73
|
|
|
$valueObject = clone $this; |
74
|
|
|
|
75
|
|
|
$valueObject->exceptKeys = array_merge($this->exceptKeys, $keys); |
76
|
|
|
|
77
|
|
|
return $valueObject; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function toArray(): array |
81
|
|
|
{ |
82
|
|
|
if (count($this->onlyKeys)) { |
83
|
|
|
return Arr::only($this->all(), $this->onlyKeys); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return Arr::except($this->all(), $this->exceptKeys); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \ReflectionClass $class |
91
|
|
|
* |
92
|
|
|
* @return array|\Spatie\ValueObject\Property[] |
93
|
|
|
*/ |
94
|
|
|
protected function getPublicProperties(ReflectionClass $class): array |
95
|
|
|
{ |
96
|
|
|
$properties = []; |
97
|
|
|
|
98
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
99
|
|
|
$properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $properties; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|