Skip to content

Commit

Permalink
Update data collection eloquent cast
Browse files Browse the repository at this point in the history
  • Loading branch information
bentleyo committed Jan 2, 2025
1 parent 9e6e93d commit 2764e83
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/Support/EloquentCasts/DataCollectionEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public function get($model, string $key, $value, array $attributes): ?DataCollec

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

$dataClass = $this->dataConfig->getDataClass($this->dataClass);
$isAbstractClassCast = $this->isAbstractClassCast();

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

return $morphedClass::from($item['data']);
Expand Down Expand Up @@ -73,10 +73,10 @@ public function set($model, string $key, $value, array $attributes): ?string
throw CannotCastData::shouldBeArray($model::class, $key);
}

$dataClass = $this->dataConfig->getDataClass($this->dataClass);
$isAbstractClassCast = $this->isAbstractClassCast();

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

return [
Expand All @@ -90,7 +90,7 @@ public function set($model, string $key, $value, array $attributes): ?string
: $item;
}, $value);

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

Expand All @@ -107,6 +107,8 @@ public function set($model, string $key, $value, array $attributes): ?string

protected function isAbstractClassCast(): bool
{
return $this->dataConfig->getDataClass($this->dataClass)->isAbstract;
$dataClass = $this->dataConfig->getDataClass($this->dataClass);

return $dataClass->isAbstract && $dataClass->transformable && ! $dataClass->propertyMorphable;
}
}

0 comments on commit 2764e83

Please sign in to comment.