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