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