1 | <?php |
||
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 | $class = new ReflectionClass(static::class); |
||
31 | |||
32 | $validators = $this->getFieldValidators($class); |
||
33 | |||
34 | $valueCaster = new ValueCaster(); |
||
35 | |||
36 | foreach ($validators as $field => $validator) { |
||
37 | if ( |
||
38 | ! isset($parameters[$field]) |
||
39 | && ! $validator->hasDefaultValue |
||
40 | && ! $validator->isNullable |
||
41 | ) { |
||
42 | throw DataTransferObjectError::uninitialized( |
||
43 | static::class, |
||
44 | $field |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | $value = $parameters[$field] ?? $this->{$field} ?? null; |
||
49 | |||
50 | if (is_array($value)) { |
||
51 | $value = $valueCaster->cast($value, $validator); |
||
52 | } |
||
53 | |||
54 | if (! $validator->isValidType($value)) { |
||
55 | throw DataTransferObjectError::invalidType( |
||
56 | static::class, |
||
57 | $field, |
||
58 | $validator->allowedTypes, |
||
59 | $value |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | $this->{$field} = $value; |
||
64 | |||
65 | unset($parameters[$field]); |
||
66 | } |
||
67 | |||
68 | if (! $this->ignoreMissing && 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 | /** |
||
89 | * @param string ...$keys |
||
90 | * |
||
91 | * @return static |
||
92 | */ |
||
93 | public function only(string ...$keys): DataTransferObject |
||
94 | { |
||
95 | $dataTransferObject = clone $this; |
||
96 | |||
97 | $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys]; |
||
98 | |||
99 | return $dataTransferObject; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param string ...$keys |
||
104 | * |
||
105 | * @return static |
||
106 | */ |
||
107 | public function except(string ...$keys): DataTransferObject |
||
108 | { |
||
109 | $dataTransferObject = clone $this; |
||
110 | |||
111 | $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys]; |
||
112 | |||
113 | return $dataTransferObject; |
||
114 | } |
||
115 | |||
116 | public function toArray(): array |
||
117 | { |
||
118 | if (count($this->onlyKeys)) { |
||
119 | $array = Arr::only($this->all(), $this->onlyKeys); |
||
120 | } else { |
||
121 | $array = Arr::except($this->all(), $this->exceptKeys); |
||
122 | } |
||
123 | |||
124 | $array = $this->parseArray($array); |
||
125 | |||
126 | return $array; |
||
127 | } |
||
128 | |||
129 | protected function parseArray(array $array): array |
||
130 | { |
||
131 | foreach ($array as $key => $value) { |
||
132 | if ( |
||
133 | $value instanceof DataTransferObject |
||
134 | || $value instanceof DataTransferObjectCollection |
||
135 | ) { |
||
136 | $array[$key] = $value->toArray(); |
||
137 | |||
138 | continue; |
||
139 | } |
||
140 | |||
141 | if (! is_array($value)) { |
||
142 | continue; |
||
143 | } |
||
144 | |||
145 | $array[$key] = $this->parseArray($value); |
||
146 | } |
||
147 | |||
148 | return $array; |
||
149 | } |
||
171 |