Skip to content

Commit 5b5ce47

Browse files
committed
(feat): allow backed enums as enum
1 parent 69a55d5 commit 5b5ce47

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/PostData.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static function dataClass(string $postType): string
102102
if (null === $classFQN) {
103103
return static::class;
104104
}
105-
105+
106106
if (! class_exists($classFQN)) {
107107
throw new RuntimeException(sprintf('The class "%s" does not exist or is not autoloaded.', $classFQN));
108108
}
@@ -137,7 +137,9 @@ private function loadMeta(int $id): void
137137
$meta = $metaAttribute->newInstance();
138138
$metaValue = $meta->getValue($id, $property->name, $this->metaPrefix());
139139
if (null !== $metaValue && null !== $propertyTypeName) {
140-
if ('Spatie\LaravelData\Data' === $propertyTypeName || is_subclass_of($propertyTypeName, 'Spatie\LaravelData\Data')) {
140+
if (is_a($propertyTypeName, Data::class, true)) {
141+
$metaValue = $propertyTypeName::from($metaValue);
142+
} elseif (is_a($propertyTypeName, \BackedEnum::class, true) && (is_int($metaValue) || is_string($metaValue))) {
141143
$metaValue = $propertyTypeName::from($metaValue);
142144
}
143145
$property->setValue($this, $metaValue);

0 commit comments

Comments
 (0)