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 array[] */ |
19
|
|
|
protected static $cache = []; |
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
|
|
|
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
|
|
|
if ( |
39
|
|
|
! isset($parameters[$property->getName()]) |
40
|
|
|
&& ! $property->isDefault() |
41
|
|
|
&& ! $property->isNullable() |
42
|
|
|
) { |
43
|
|
|
throw DataTransferObjectError::uninitialized($property); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$value = $parameters[$property->getName()] ?? $property->getValue($this); |
47
|
|
|
|
48
|
|
|
$property->set($value); |
49
|
|
|
|
50
|
|
|
unset($parameters[$property->getName()]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (count($parameters)) { |
54
|
|
|
throw DataTransferObjectError::unknownProperties(array_keys($parameters), $class->getName()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function all(): array |
59
|
|
|
{ |
60
|
|
|
$data = []; |
61
|
|
|
|
62
|
|
|
$class = new ReflectionClass(static::class); |
63
|
|
|
|
64
|
|
|
$properties = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
65
|
|
|
|
66
|
|
|
foreach ($properties as $reflectionProperty) { |
67
|
|
|
$data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string ...$keys |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public function only(string ...$keys): DataTransferObject |
79
|
|
|
{ |
80
|
|
|
$valueObject = clone $this; |
81
|
|
|
|
82
|
|
|
$valueObject->onlyKeys = array_merge($this->onlyKeys, $keys); |
83
|
|
|
|
84
|
|
|
return $valueObject; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string ...$keys |
89
|
|
|
* |
90
|
|
|
* @return static |
91
|
|
|
*/ |
92
|
|
|
public function except(string ...$keys): DataTransferObject |
93
|
|
|
{ |
94
|
|
|
$valueObject = clone $this; |
95
|
|
|
|
96
|
|
|
$valueObject->exceptKeys = array_merge($this->exceptKeys, $keys); |
97
|
|
|
|
98
|
|
|
return $valueObject; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function toArray(): array |
102
|
|
|
{ |
103
|
|
|
if (count($this->onlyKeys)) { |
104
|
|
|
$array = Arr::only($this->all(), $this->onlyKeys); |
105
|
|
|
} else { |
106
|
|
|
$array = Arr::except($this->all(), $this->exceptKeys); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$array = $this->parseArray($array); |
110
|
|
|
|
111
|
|
|
return $array; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function parseArray(array $array): array |
115
|
|
|
{ |
116
|
|
|
foreach ($array as $key => $value) { |
117
|
|
|
if ( |
118
|
|
|
$value instanceof DataTransferObject |
119
|
|
|
|| $value instanceof DataTransferObjectCollection |
120
|
|
|
) { |
121
|
|
|
$array[$key] = $value->toArray(); |
122
|
|
|
|
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (! is_array($value)) { |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$array[$key] = $this->parseArray($value); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $array; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \ReflectionClass $class |
138
|
|
|
* |
139
|
|
|
* @return array|\Spatie\DataTransferObject\Property[] |
140
|
|
|
*/ |
141
|
|
|
protected function getPublicProperties(ReflectionClass $class): array |
142
|
|
|
{ |
143
|
|
|
$properties = []; |
144
|
|
|
|
145
|
|
|
foreach ($this->getPublicReflectionProperties($class) as $reflectionProperty) { |
146
|
|
|
$properties[$reflectionProperty->getName()] = Property::fromReflection($this, $reflectionProperty); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $properties; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param \ReflectionClass $class |
154
|
|
|
* |
155
|
|
|
* @return array|ReflectionProperty[] |
156
|
|
|
*/ |
157
|
|
|
protected function getPublicReflectionProperties(ReflectionClass $class): array |
158
|
|
|
{ |
159
|
|
|
if (isset(self::$cache[static::class])) { |
160
|
|
|
return self::$cache[static::class]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
self::$cache[static::class] = []; |
164
|
|
|
|
165
|
|
|
foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
166
|
|
|
self::$cache[static::class][$reflectionProperty->getName()] = $reflectionProperty; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return self::$cache[static::class]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|