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