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 $exceptKeys = []; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $onlyKeys = []; |
17
|
|
|
|
18
|
|
|
/** @var Property[] | array */ |
19
|
|
|
protected $properties = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $parameters |
23
|
|
|
* |
24
|
|
|
* @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static |
25
|
|
|
*/ |
26
|
|
|
public static function immutable(array $parameters): ImmutableDataTransferObject |
27
|
|
|
{ |
28
|
|
|
return new ImmutableDataTransferObject(new static($parameters)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
final public function __construct(array $parameters) |
32
|
|
|
{ |
33
|
|
|
$class = new ReflectionClass(static::class); |
34
|
|
|
|
35
|
|
|
$properties = $this->getPublicProperties($class); |
36
|
|
|
|
37
|
|
|
foreach ($properties as $property) { |
38
|
|
|
$property->setDefault($property->getValue($this)); |
39
|
|
|
$method = $property->getName(); |
40
|
|
|
|
41
|
|
|
if (method_exists($this, $method)) { |
42
|
|
|
$factory = new Attribute($property); |
43
|
|
|
$property = $this->$method($factory)->getProperty(); |
44
|
|
|
} |
45
|
|
|
$default = $property->getDefault(); |
46
|
|
|
if ( |
47
|
|
|
!isset($parameters[$property->getName()]) |
48
|
|
|
&& !isset($default) |
49
|
|
|
&& !$property->isNullable() |
50
|
|
|
&& $property->isRequired() |
51
|
|
|
) { |
52
|
|
|
throw DataTransferObjectError::uninitialized($property); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (array_key_exists($property->getName(), $parameters)) { |
56
|
|
|
$property->set($parameters[$property->getName()]); |
57
|
|
|
} else { |
58
|
|
|
$property->setUninitialized(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->properties[$property->getName()] = $property; |
62
|
|
|
|
63
|
|
|
unset($parameters[$property->getName()]); |
64
|
|
|
unset($this->{$property->getName()}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
if (count($parameters)) { |
69
|
|
|
throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function all(): array |
74
|
|
|
{ |
75
|
|
|
$data = []; |
76
|
|
|
|
77
|
|
|
$class = new ReflectionClass(static::class); |
78
|
|
|
|
79
|
|
|
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
80
|
|
|
|
81
|
|
|
foreach ($properties as $reflectionProperty) { |
82
|
|
|
$data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __set($name, $value) |
89
|
|
|
{ |
90
|
|
|
throw DataTransferObjectError::immutable($name); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function __get($name) |
94
|
|
|
{ |
95
|
|
|
return $this->properties[$name]->getActualValue(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string ...$keys |
100
|
|
|
* |
101
|
|
|
* @return static |
102
|
|
|
*/ |
103
|
|
|
public function only(string ...$keys): DataTransferObject |
104
|
|
|
{ |
105
|
|
|
$valueObject = clone $this; |
106
|
|
|
|
107
|
|
|
$valueObject->onlyKeys = array_merge($this->onlyKeys, $keys); |
108
|
|
|
|
109
|
|
|
return $valueObject; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string ...$keys |
114
|
|
|
* |
115
|
|
|
* @return static |
116
|
|
|
*/ |
117
|
|
|
public function except(string ...$keys): DataTransferObject |
118
|
|
|
{ |
119
|
|
|
$valueObject = clone $this; |
120
|
|
|
|
121
|
|
|
$valueObject->exceptKeys = array_merge($this->exceptKeys, $keys); |
122
|
|
|
|
123
|
|
|
return $valueObject; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function toArray(): array |
127
|
|
|
{ |
128
|
|
|
if (count($this->onlyKeys)) { |
129
|
|
|
$array = Arr::only($this->all(), $this->onlyKeys); |
130
|
|
|
} else { |
131
|
|
|
$array = Arr::except($this->all(), $this->exceptKeys); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$array = $this->parseArray($array); |
135
|
|
|
|
136
|
|
|
return $array; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function parseArray(array $array): array |
140
|
|
|
{ |
141
|
|
|
foreach ($array as $key => $value) { |
142
|
|
|
if ( |
143
|
|
|
$value instanceof DataTransferObject |
144
|
|
|
|| $value instanceof DataTransferObjectCollection |
145
|
|
|
) { |
146
|
|
|
$array[$key] = $value->toArray(); |
147
|
|
|
|
148
|
|
|
continue; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (!is_array($value)) { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$array[$key] = $this->parseArray($value); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $array; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param \ReflectionClass $class |
163
|
|
|
* |
164
|
|
|
* @return array|\Spatie\DataTransferObject\Property[] |
165
|
|
|
*/ |
166
|
|
|
protected function getPublicProperties(ReflectionClass $class): array |
167
|
|
|
{ |
168
|
|
|
$properties = []; |
169
|
|
|
|
170
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
171
|
|
|
$properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $properties; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function propertyIsOptional($property) |
178
|
|
|
{ |
179
|
|
|
$isOptionalMethod = $property->getName() . "IsOptional"; |
180
|
|
|
return method_exists($this, $isOptionalMethod) && is_bool($this->$isOptionalMethod()) ? $this->$isOptionalMethod() : false; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|