| Total Complexity | 74 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DataTransferObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataTransferObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class DataTransferObject implements DtoContract |
||
| 21 | { |
||
| 22 | /** @var array */ |
||
| 23 | protected $onlyKeys = []; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | protected $with = []; |
||
| 27 | |||
| 28 | /** @var Property[] */ |
||
| 29 | protected $properties = []; |
||
| 30 | |||
| 31 | /** @var bool */ |
||
| 32 | protected $immutable = false; |
||
| 33 | |||
| 34 | /** @var bool */ |
||
| 35 | protected $validation = true; |
||
| 36 | |||
| 37 | public function __construct(array $parameters) |
||
| 38 | { |
||
| 39 | $this->boot($parameters); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Boot the dto and process all parameters. |
||
| 44 | * @param array $parameters |
||
| 45 | * @throws \ReflectionException |
||
| 46 | */ |
||
| 47 | protected function boot(array $parameters): void |
||
| 48 | { |
||
| 49 | $this->properties = (new PropertyFactory($this))->build($parameters); |
||
| 50 | $this->determineImmutability(); |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function determineImmutability() |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | public function setImmutable(bool $immutable): void |
||
| 64 | { |
||
| 65 | if ($immutable) { |
||
| 66 | if (!$this->isImmutable()) { |
||
| 67 | $this->immutable = true; |
||
| 68 | foreach ($this->properties as $property) { |
||
| 69 | $this->chainPropertyImmutable($property, true); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } else |
||
| 73 | $this->immutable = true; |
||
| 74 | |||
| 75 | } |
||
| 76 | |||
| 77 | protected function chainPropertyImmutable(Property $property, bool $immutable) |
||
| 78 | { |
||
| 79 | $dto = $property->getValue(); |
||
| 80 | if ($dto instanceof DataTransferObject) { |
||
| 81 | $dto->setImmutable($immutable); |
||
| 82 | } elseif (is_iterable($dto)) { |
||
| 83 | foreach ($dto as $aPotentialDto) { |
||
| 84 | if ($aPotentialDto instanceof DataTransferObject) { |
||
| 85 | $aPotentialDto->setImmutable($immutable); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Immutable behavior |
||
| 93 | * Throw error if a user tries to set a property. |
||
| 94 | * @param $name |
||
| 95 | * @param $value |
||
| 96 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
| 97 | */ |
||
| 98 | public function __set($name, $value) |
||
| 99 | { |
||
| 100 | if ($this->immutable) { |
||
| 101 | throw new ImmutableDtoException($name); |
||
| 102 | } |
||
| 103 | if (!isset($this->properties[$name])) { |
||
| 104 | throw new PropertyNotFoundDtoException($name, get_class($this)); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($this->properties[$name]->isImmutable()) { |
||
| 108 | throw new ImmutablePropertyDtoException($name); |
||
| 109 | } |
||
| 110 | $this->$name = $value; |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function propertyExists(string $propertyName) |
||
| 114 | { |
||
| 115 | return array_key_exists($propertyName, $this->properties); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function &__get($name) |
||
| 119 | { |
||
| 120 | if (!$this->propertyExists($name)) { |
||
| 121 | if ($this instanceof WithAdditionalProperties) { |
||
| 122 | if (array_key_exists($name, $this->with)) { |
||
| 123 | if ($this->isImmutable()) { |
||
| 124 | $value = $this->with[$name]; |
||
| 125 | return $value; |
||
| 126 | } |
||
| 127 | return $this->with[$name]; |
||
| 128 | } |
||
| 129 | throw new PropertyNotFoundDtoException($name, static::class); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | $property = $this->properties[$name]; |
||
| 133 | $violations = $property->getViolations(); |
||
| 134 | if ($violations->count() > 0) { |
||
| 135 | throw new ValidatorException([$name => $violations]); |
||
| 136 | } |
||
| 137 | if ($this->isImmutable()) { |
||
| 138 | $value = $this->properties[$name]->getValue(); |
||
| 139 | return $value; |
||
| 140 | } |
||
| 141 | return $this->properties[$name]->value; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function isImmutable(): bool |
||
| 145 | { |
||
| 146 | return $this->immutable; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function isValid(){ |
||
| 150 | return empty($this->getValidationViolations()); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function validationEnabled() |
||
| 154 | { |
||
| 155 | return $this->validation; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function setValidation(bool $validate) |
||
| 159 | { |
||
| 160 | $this->validation = $validate; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function all(): array |
||
| 176 | } |
||
| 177 | |||
| 178 | public function only(string ...$keys): DtoContract |
||
| 179 | { |
||
| 180 | $this->onlyKeys = array_merge($this->onlyKeys, $keys); |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function except(string ...$keys): DtoContract |
||
| 186 | { |
||
| 187 | foreach ($keys as $key) { |
||
| 188 | if (array_key_exists($key, $this->with)) { |
||
| 189 | unset($this->with[$key]); |
||
| 190 | } |
||
| 191 | $property = $this->properties[$key] ?? null; |
||
| 192 | if (isset($property)) { |
||
| 193 | $property->setVisible(false); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function with(string $key, $value): DtoContract |
||
| 201 | { |
||
| 202 | if (array_key_exists($key, $this->properties)) { |
||
| 203 | throw new PropertyAlreadyExistsException($key); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $this->override($key, $value); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function override(string $key, $value): DtoContract |
||
| 210 | { |
||
| 211 | if ($this->isImmutable()) { |
||
| 212 | throw new ImmutableDtoException($key); |
||
| 213 | } |
||
| 214 | if (($propertyExists = array_key_exists($key, $this->properties) && $this->properties[$key]->isImmutable())) { |
||
| 215 | throw new ImmutablePropertyDtoException($key); |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($propertyExists) { |
||
|
|
|||
| 219 | $property = $this->properties[$key]; |
||
| 220 | $property->set($value); |
||
| 221 | if ($this->validation) { |
||
| 222 | $this->validate(); |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | $this->with[$key] = $value; |
||
| 226 | } |
||
| 227 | |||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | public function toArray(): array |
||
| 232 | { |
||
| 233 | $data = $this->all(); |
||
| 234 | $array = []; |
||
| 235 | |||
| 236 | if (count($this->onlyKeys)) { |
||
| 237 | $array = array_intersect_key($data, array_flip((array)$this->onlyKeys)); |
||
| 238 | } else { |
||
| 239 | foreach ($data as $key => $propertyValue) { |
||
| 240 | if (array_key_exists($key, $this->with) || (array_key_exists($key, $this->properties) && $this->properties[$key]->isVisible() && $this->properties[$key]->isInitialized())) { |
||
| 241 | $array[$key] = $propertyValue; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->parseArray($array); |
||
| 247 | } |
||
| 248 | |||
| 249 | protected function parseArray(array $array): array |
||
| 250 | { |
||
| 251 | foreach ($array as $key => $value) { |
||
| 252 | if ( |
||
| 253 | $value instanceof DataTransferObject |
||
| 254 | || $value instanceof DataTransferObjectCollection |
||
| 255 | ) { |
||
| 256 | $array[$key] = $value->toArray(); |
||
| 257 | |||
| 258 | continue; |
||
| 259 | } |
||
| 260 | |||
| 261 | if (!is_array($value)) { |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | |||
| 265 | $array[$key] = $this->parseArray($value); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $array; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function getValidationViolations() |
||
| 295 | } |
||
| 296 | |||
| 297 | public function validate() |
||
| 298 | { |
||
| 299 | $violations = $this->getValidationViolations(); |
||
| 300 | if (!empty($violations)) { |
||
| 301 | throw new ValidatorException($violations); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | protected function recursivelySortKeys(array $array, $str = '') |
||
| 325 | } |
||
| 326 | } |
||
| 327 |