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 bool */ |
13
|
|
|
protected $ignoreMissing = false; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $exceptKeys = []; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $onlyKeys = []; |
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
|
|
|
/** |
32
|
|
|
* @param array $arrayOfParameters |
33
|
|
|
* |
34
|
|
|
* @return \Spatie\DataTransferObject\ImmutableDataTransferObject[]|static[] |
35
|
|
|
*/ |
36
|
|
|
public static function arrayOf(array $arrayOfParameters): array |
37
|
|
|
{ |
38
|
|
|
return array_map( |
39
|
|
|
function ($parameters) { |
40
|
|
|
return new static($parameters); |
41
|
|
|
}, |
42
|
|
|
$arrayOfParameters |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function __construct(array $parameters = []) |
47
|
|
|
{ |
48
|
|
|
$validators = $this->getFieldValidators(); |
49
|
|
|
|
50
|
|
|
$valueCaster = $this->getValueCaster(); |
51
|
|
|
|
52
|
|
|
foreach ($validators as $field => $validator) { |
53
|
|
|
if ( |
54
|
|
|
! isset($parameters[$field]) |
55
|
|
|
&& ! $validator->hasDefaultValue |
56
|
|
|
&& ! $validator->isNullable |
57
|
|
|
) { |
58
|
|
|
throw DataTransferObjectError::uninitialized( |
59
|
|
|
static::class, |
60
|
|
|
$field |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$value = $parameters[$field] ?? $this->{$field} ?? null; |
65
|
|
|
|
66
|
|
|
$value = $this->castValue($valueCaster, $validator, $value); |
67
|
|
|
|
68
|
|
|
if (! $validator->isValidType($value)) { |
69
|
|
|
throw DataTransferObjectError::invalidType( |
70
|
|
|
static::class, |
71
|
|
|
$field, |
72
|
|
|
$validator->allowedTypes, |
73
|
|
|
$value |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->{$field} = $value; |
78
|
|
|
|
79
|
|
|
unset($parameters[$field]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (! $this->ignoreMissing && count($parameters)) { |
83
|
|
|
throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function all(): array |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$data = []; |
90
|
|
|
|
91
|
|
|
$class = new ReflectionClass(static::class); |
92
|
|
|
|
93
|
|
|
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
94
|
|
|
|
95
|
|
|
foreach ($properties as $reflectionProperty) { |
96
|
|
|
$data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $data; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string ...$keys |
104
|
|
|
* |
105
|
|
|
* @return static |
106
|
|
|
*/ |
107
|
|
|
public function only(string ...$keys): DataTransferObject |
108
|
|
|
{ |
109
|
|
|
$valueObject = clone $this; |
110
|
|
|
|
111
|
|
|
$valueObject->onlyKeys = array_merge($this->onlyKeys, $keys); |
112
|
|
|
|
113
|
|
|
return $valueObject; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string ...$keys |
118
|
|
|
* |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
|
|
public function except(string ...$keys): DataTransferObject |
122
|
|
|
{ |
123
|
|
|
$valueObject = clone $this; |
124
|
|
|
|
125
|
|
|
$valueObject->exceptKeys = array_merge($this->exceptKeys, $keys); |
126
|
|
|
|
127
|
|
|
return $valueObject; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function toArray(): array |
131
|
|
|
{ |
132
|
|
|
if (count($this->onlyKeys)) { |
133
|
|
|
$array = Arr::only($this->all(), $this->onlyKeys); |
134
|
|
|
} else { |
135
|
|
|
$array = Arr::except($this->all(), $this->exceptKeys); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$array = $this->parseArray($array); |
139
|
|
|
|
140
|
|
|
return $array; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function parseArray(array $array): array |
144
|
|
|
{ |
145
|
|
|
foreach ($array as $key => $value) { |
146
|
|
|
if ( |
147
|
|
|
$value instanceof DataTransferObject |
148
|
|
|
|| $value instanceof DataTransferObjectCollection |
149
|
|
|
) { |
150
|
|
|
$array[$key] = $value->toArray(); |
151
|
|
|
|
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (! is_array($value)) { |
156
|
|
|
continue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$array[$key] = $this->parseArray($value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $array; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param \ReflectionClass $class |
|
|
|
|
167
|
|
|
* |
168
|
|
|
* @return \Spatie\DataTransferObject\FieldValidator[] |
169
|
|
|
*/ |
170
|
|
|
protected function getFieldValidators(): array |
171
|
|
|
{ |
172
|
|
View Code Duplication |
return DTOCache::resolve(static::class, function () { |
|
|
|
|
173
|
|
|
$class = new ReflectionClass(static::class); |
174
|
|
|
|
175
|
|
|
$properties = []; |
176
|
|
|
|
177
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
178
|
|
|
// Skip static properties |
179
|
|
|
if ($reflectionProperty->isStatic()) { |
180
|
|
|
continue; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$field = $reflectionProperty->getName(); |
184
|
|
|
|
185
|
|
|
$properties[$field] = FieldValidator::fromReflection($reflectionProperty); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $properties; |
189
|
|
|
}); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param \Spatie\DataTransferObject\ValueCaster $valueCaster |
194
|
|
|
* @param \Spatie\DataTransferObject\FieldValidator $fieldValidator |
195
|
|
|
* @param mixed $value |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
|
protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value) |
199
|
|
|
{ |
200
|
|
|
if (is_array($value)) { |
201
|
|
|
return $valueCaster->cast($value, $fieldValidator); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
protected function getValueCaster(): ValueCaster |
208
|
|
|
{ |
209
|
|
|
return new ValueCaster(); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.