| Total Complexity | 18 |
| Total Lines | 82 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class TypeCaster |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var PropertyType $type |
||
| 14 | */ |
||
| 15 | protected $type; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * TypeCaster constructor. |
||
| 19 | * @param PropertyType $type |
||
| 20 | */ |
||
| 21 | public function __construct(PropertyType $type) |
||
| 22 | { |
||
| 23 | $this->type = $type; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function cast($value) |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | protected function castDto($value) |
||
| 38 | { |
||
| 39 | foreach ($this->type->getTypes() as $type) { |
||
| 40 | if (is_subclass_of($type, DtoContract::class)) { |
||
| 41 | if (is_array($value)) |
||
| 42 | return new $type($value); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | return $value; |
||
| 46 | } |
||
| 47 | |||
| 48 | protected function castCollection(array $values) |
||
| 49 | { |
||
| 50 | $castTo = null; |
||
| 51 | |||
| 52 | foreach ($this->type->getArrayTypes() as $type) { |
||
| 53 | if (!is_subclass_of($type, DtoContract::class)) { |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | $castTo = $type; |
||
| 58 | |||
| 59 | break; |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!$castTo) { |
||
| 63 | return $values; |
||
| 64 | } |
||
| 65 | |||
| 66 | $casts = []; |
||
| 67 | |||
| 68 | foreach ($values as $value) { |
||
| 69 | $casts[] = new $castTo($value); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $casts; |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function shouldBeCastToCollection(array $values): bool |
||
| 92 | } |
||
| 93 | } |