Skip to content

Commit

Permalink
Cleanup PR
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 10, 2024
1 parent 77a46fc commit 67ea943
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
58 changes: 27 additions & 31 deletions src/Support/EloquentCasts/DataCollectionEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@ public function get($model, string $key, $value, array $attributes): ?DataCollec

$data = json_decode($value, true, flags: JSON_THROW_ON_ERROR);

$isAbstract = $this->isAbstractClassCast();
$data = array_map(
function (array $item) use ($isAbstract) {
if ($isAbstract) {
$dataClass = $this->dataConfig->morphMap->getMorphedDataClass($item['type']) ?? $item['type'];
$dataClass = $this->dataConfig->getDataClass($this->dataClass);

return $dataClass::from($item['data']);
}
$data = array_map(function (array $item) use ($dataClass) {
if ($dataClass->isAbstract && $dataClass->transformable) {
$morphedClass = $this->dataConfig->morphMap->getMorphedDataClass($item['type']) ?? $item['type'];

return ($this->dataClass)::from($item);
},
$data
);
return $morphedClass::from($item['data']);
}

return ($this->dataClass)::from($item);
}, $data);

return new ($this->dataCollectionClass)($this->dataClass, $data);
}
Expand All @@ -70,26 +68,24 @@ public function set($model, string $key, $value, array $attributes): ?string
throw CannotCastData::shouldBeArray($model::class, $key);
}

$isAbstract = $this->isAbstractClassCast();
$data = array_map(
function (array | BaseData $item) use ($isAbstract) {
if ($isAbstract) {
$class = get_class($item);

return [
'type' => $this->dataConfig->morphMap->getDataClassAlias($class) ?? $class,
'data' => json_decode(json: $item->toJson(), associative: true, flags: JSON_THROW_ON_ERROR),
];
}

return is_array($item)
? ($this->dataClass)::from($item)
: $item;
},
$value
);

if ($isAbstract) {
$dataClass = $this->dataConfig->getDataClass($this->dataClass);

$data = array_map(function (array|BaseData $item) use ($dataClass) {
if ($dataClass->isAbstract && $item instanceof TransformableData) {
$class = get_class($item);

return [
'type' => $this->dataConfig->morphMap->getDataClassAlias($class) ?? $class,
'data' => json_decode(json: $item->toJson(), associative: true, flags: JSON_THROW_ON_ERROR),
];
}

return is_array($item)
? ($this->dataClass)::from($item)
: $item;
}, $value);

if ($dataClass->isAbstract) {
return json_encode($data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,7 @@
expect($model->abstract_collection)
->toBeInstanceOf(DataCollection::class)
->each->toBeInstanceOf(AbstractData::class);

expect($model->abstract_collection[0])->toBeInstanceOf(AbstractDataA::class);
expect($model->abstract_collection[1])->toBeInstanceOf(AbstractDataB::class);
});

0 comments on commit 67ea943

Please sign in to comment.