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