diff --git a/src/Attributes/UUIDBin.php b/src/Attributes/UUIDBin.php index a9cdaf5..2eb33db 100644 --- a/src/Attributes/UUIDBin.php +++ b/src/Attributes/UUIDBin.php @@ -16,6 +16,8 @@ #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)] class UUIDBin extends CastForSave implements CastInterface { + public const NULLABLE = 1 << 0; + /** * CastForSave constructor. */ @@ -36,22 +38,31 @@ public function getUUIDCaster(): \Closure $method = $this->version; + if (!$value && ($this->options & static::NULLABLE)) { + return null; + } + return new UuidBinWrapper($value ?: \Ramsey\Uuid\Uuid::$method()); }; } public function hydrate(mixed $value): mixed { - return UuidBinWrapper::wrap($value); + return UuidBinWrapper::tryWrap($value); } public function extract(mixed $value): mixed { - return UuidBinWrapper::wrap($value); + return UuidBinWrapper::tryWrap($value); } public static function wrap(mixed $value): UuidInterface { return UuidBinWrapper::wrap($value); } + + public static function tryWrap(mixed $value): ?UuidInterface + { + return UuidBinWrapper::tryWrap($value); + } } diff --git a/src/Attributes/UUIDBinNullable.php b/src/Attributes/UUIDBinNullable.php new file mode 100644 index 0000000..b9cfd4f --- /dev/null +++ b/src/Attributes/UUIDBinNullable.php @@ -0,0 +1,23 @@ +