Skip to content

Commit 67ea943

Browse files
Cleanup PR
1 parent 77a46fc commit 67ea943

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

src/Support/EloquentCasts/DataCollectionEloquentCast.php

+27-31
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,17 @@ public function get($model, string $key, $value, array $attributes): ?DataCollec
3535

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

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

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

47-
return ($this->dataClass)::from($item);
48-
},
49-
$data
50-
);
44+
return $morphedClass::from($item['data']);
45+
}
46+
47+
return ($this->dataClass)::from($item);
48+
}, $data);
5149

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

73-
$isAbstract = $this->isAbstractClassCast();
74-
$data = array_map(
75-
function (array | BaseData $item) use ($isAbstract) {
76-
if ($isAbstract) {
77-
$class = get_class($item);
78-
79-
return [
80-
'type' => $this->dataConfig->morphMap->getDataClassAlias($class) ?? $class,
81-
'data' => json_decode(json: $item->toJson(), associative: true, flags: JSON_THROW_ON_ERROR),
82-
];
83-
}
84-
85-
return is_array($item)
86-
? ($this->dataClass)::from($item)
87-
: $item;
88-
},
89-
$value
90-
);
91-
92-
if ($isAbstract) {
71+
$dataClass = $this->dataConfig->getDataClass($this->dataClass);
72+
73+
$data = array_map(function (array|BaseData $item) use ($dataClass) {
74+
if ($dataClass->isAbstract && $item instanceof TransformableData) {
75+
$class = get_class($item);
76+
77+
return [
78+
'type' => $this->dataConfig->morphMap->getDataClassAlias($class) ?? $class,
79+
'data' => json_decode(json: $item->toJson(), associative: true, flags: JSON_THROW_ON_ERROR),
80+
];
81+
}
82+
83+
return is_array($item)
84+
? ($this->dataClass)::from($item)
85+
: $item;
86+
}, $value);
87+
88+
if ($dataClass->isAbstract) {
9389
return json_encode($data);
9490
}
9591

tests/Support/EloquentCasts/DataCollectionEloquentCastTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,7 @@
169169
expect($model->abstract_collection)
170170
->toBeInstanceOf(DataCollection::class)
171171
->each->toBeInstanceOf(AbstractData::class);
172+
173+
expect($model->abstract_collection[0])->toBeInstanceOf(AbstractDataA::class);
174+
expect($model->abstract_collection[1])->toBeInstanceOf(AbstractDataB::class);
172175
});

0 commit comments

Comments
 (0)