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 | $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $data; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string ...$keys |
||
| 101 | * |
||
| 102 | * @return static |
||
| 103 | */ |
||
| 104 | public function only(string ...$keys): DataTransferObject |
||
| 105 | { |
||
| 106 | $dataTransferObject = clone $this; |
||
| 107 | |||
| 108 | $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys]; |
||
| 109 | |||
| 110 | return $dataTransferObject; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string ...$keys |
||
| 115 | * |
||
| 116 | * @return static |
||
| 117 | */ |
||
| 118 | public function except(string ...$keys): DataTransferObject |
||
| 119 | { |
||
| 120 | $dataTransferObject = clone $this; |
||
| 121 | |||
| 122 | $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys]; |
||
| 123 | |||
| 124 | return $dataTransferObject; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function toArray(): array |
||
| 128 | { |
||
| 129 | if (count($this->onlyKeys)) { |
||
| 130 | $array = Arr::only($this->all(), $this->onlyKeys); |
||
| 131 | } else { |
||
| 132 | $array = Arr::except($this->all(), $this->exceptKeys); |
||
| 133 | } |
||
| 134 | |||
| 135 | $array = $this->parseArray($array); |
||
| 136 | |||
| 137 | return $array; |
||
| 138 | } |
||
| 139 | |||
| 140 | protected function parseArray(array $array): array |
||
| 141 | { |
||
| 142 | foreach ($array as $key => $value) { |
||
| 143 | if ( |
||
| 144 | $value instanceof DataTransferObject |
||
| 145 | || $value instanceof DataTransferObjectCollection |
||
| 146 | ) { |
||
| 147 | $array[$key] = $value->toArray(); |
||
| 148 | |||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (! is_array($value)) { |
||
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | $array[$key] = $this->parseArray($value); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $array; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param \ReflectionClass $class |
||
| 164 | * |
||
| 165 | * @return \Spatie\DataTransferObject\FieldValidator[] |
||
| 166 | */ |
||
| 167 | protected function getFieldValidators(): array |
||
| 168 | { |
||
| 169 | return DTOCache::resolve(static::class, function () { |
||
| 170 | $class = new ReflectionClass(static::class); |
||
| 171 | |||
| 172 | $properties = []; |
||
| 173 | |||
| 174 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
||
| 175 | // Skip static properties |
||
| 176 | if ($reflectionProperty->isStatic()) { |
||
| 177 | continue; |
||
| 178 | } |
||
| 179 | |||
| 180 | $field = $reflectionProperty->getName(); |
||
| 181 | |||
| 182 | $properties[$field] = FieldValidator::fromReflection($reflectionProperty); |
||
| 183 | } |
||
| 184 | |||
| 185 | return $properties; |
||
| 186 | }); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param \Spatie\DataTransferObject\ValueCaster $valueCaster |
||
| 191 | * @param \Spatie\DataTransferObject\FieldValidator $fieldValidator |
||
| 192 | * @param mixed $value |
||
| 193 | * |
||
| 194 | * @return mixed |
||
| 195 | */ |
||
| 196 | protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value) |
||
| 197 | { |
||
| 198 | if (is_array($value)) { |
||
| 199 | return $valueCaster->cast($value, $fieldValidator); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $value; |
||
| 203 | } |
||
| 204 | |||
| 205 | protected function getValueCaster(): ValueCaster |
||
| 206 | { |
||
| 207 | return new ValueCaster(); |
||
| 208 | } |
||
| 209 | } |
||
| 210 |