From 0abd60841908343b2bcb9a43102b91422a47bb0b Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 17 Jun 2025 11:08:33 +0200 Subject: [PATCH] (feat): allow backed enums as meta --- src/PostData.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/PostData.php b/src/PostData.php index 8f6efc1..913fb97 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -4,6 +4,7 @@ namespace Yard\Data; +use BackedEnum; use Carbon\CarbonImmutable; use Corcel\Model\Post; use ReflectionClass; @@ -102,7 +103,7 @@ private static function dataClass(string $postType): string if (null === $classFQN) { return static::class; } - + if (! class_exists($classFQN)) { throw new RuntimeException(sprintf('The class "%s" does not exist or is not autoloaded.', $classFQN)); } @@ -137,7 +138,9 @@ private function loadMeta(int $id): void $meta = $metaAttribute->newInstance(); $metaValue = $meta->getValue($id, $property->name, $this->metaPrefix()); if (null !== $metaValue && null !== $propertyTypeName) { - if ('Spatie\LaravelData\Data' === $propertyTypeName || is_subclass_of($propertyTypeName, 'Spatie\LaravelData\Data')) { + if (is_a($propertyTypeName, Data::class, true)) { + $metaValue = $propertyTypeName::from($metaValue); + } elseif (is_a($propertyTypeName, BackedEnum::class, true) && (is_int($metaValue) || is_string($metaValue))) { $metaValue = $propertyTypeName::from($metaValue); } $property->setValue($this, $metaValue);