From 63a6c4aef369830d85a8d1f5311743b0b9e3c125 Mon Sep 17 00:00:00 2001 From: Simon Asika Date: Mon, 4 Mar 2024 20:01:24 +0800 Subject: [PATCH] Fxi for UUID --- src/Attributes/UUIDBin.php | 15 +++++++++++++-- src/Attributes/UUIDBinNullable.php | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/Attributes/UUIDBinNullable.php 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 @@ +