Skip to content

Commit

Permalink
Fxi for UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Mar 4, 2024
1 parent f8f000e commit 63a6c4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Attributes/UUIDBin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class UUIDBin extends CastForSave implements CastInterface
{
public const NULLABLE = 1 << 0;

/**
* CastForSave constructor.
*/
Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions src/Attributes/UUIDBinNullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Windwalker\ORM\Attributes;

use Ramsey\Uuid\UuidInterface;
use Windwalker\Cache\Exception\LogicException;
use Windwalker\ORM\Cast\CastInterface;
use Windwalker\Query\Wrapper\UuidBinWrapper;
use Windwalker\Query\Wrapper\UuidWrapper;

/**
* The UUID class.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class UUIDBinNullable extends UUIDBin
{
public function __construct(string $version = 'uuid7', mixed $caster = null, int $options = self::NULLABLE)
{
parent::__construct($version, $caster, $options);
}
}

0 comments on commit 63a6c4a

Please sign in to comment.