parent
13bfbbe528
commit
eb40f1df49
10 changed files with 388 additions and 44 deletions
|
@ -6,17 +6,21 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
|
|||
use Doctrine\DBAL\Types\Exception\InvalidType;
|
||||
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use InvalidArgumentException;
|
||||
use Override;
|
||||
use Symfony\Component\Uid\Ulid;
|
||||
|
||||
use function is_string;
|
||||
|
||||
final class UlidType extends Type
|
||||
{
|
||||
public const NAME = 'ulid';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
|
||||
{
|
||||
#[Override]
|
||||
public function getSQLDeclaration(
|
||||
array $column,
|
||||
AbstractPlatform $platform,
|
||||
): string {
|
||||
if ($this->hasNativeGuidType($platform)) {
|
||||
return $platform->getGuidTypeDeclarationSQL($column);
|
||||
}
|
||||
|
@ -27,26 +31,35 @@ final class UlidType extends Type
|
|||
]);
|
||||
}
|
||||
|
||||
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Ulid
|
||||
{
|
||||
#[Override]
|
||||
public function convertToPHPValue(
|
||||
mixed $value,
|
||||
AbstractPlatform $platform,
|
||||
): Ulid|null {
|
||||
if ($value instanceof Ulid || $value === null) {
|
||||
return $value;
|
||||
}
|
||||
if (!is_string($value)) {
|
||||
|
||||
if (! is_string($value)) {
|
||||
throw InvalidType::new($value, self::NAME, ['null', 'string', self::class]);
|
||||
}
|
||||
|
||||
try {
|
||||
return Ulid::fromString($value);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw ValueNotConvertible::new($value, self::NAME, null, $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
|
||||
{
|
||||
$toStringFunc = fn (Ulid $v) => $v->toRfc4122();
|
||||
if (!$this->hasNativeGuidType($platform)) {
|
||||
$toStringFunc = fn (Ulid $v) => $v->toBinary();
|
||||
#[Override]
|
||||
public function convertToDatabaseValue(
|
||||
mixed $value,
|
||||
AbstractPlatform $platform,
|
||||
): string|null {
|
||||
$toStringFunc = static fn(Ulid $v): string => $v->toRfc4122();
|
||||
|
||||
if (! $this->hasNativeGuidType($platform)) {
|
||||
$toStringFunc = static fn(Ulid $v): string => $v->toBinary();
|
||||
}
|
||||
|
||||
if ($value instanceof Ulid) {
|
||||
|
@ -57,18 +70,32 @@ final class UlidType extends Type
|
|||
return null;
|
||||
}
|
||||
|
||||
if (!\is_string($value)) {
|
||||
if (! is_string($value)) {
|
||||
throw InvalidType::new($value, self::NAME, ['null', 'string', self::class]);
|
||||
}
|
||||
|
||||
try {
|
||||
return $toStringFunc(Ulid::fromString($value));
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw ValueNotConvertible::new($value, self::NAME, null, $e);
|
||||
}
|
||||
}
|
||||
|
||||
private function hasNativeGuidType(AbstractPlatform $platform): bool
|
||||
{
|
||||
return $platform->getGuidTypeDeclarationSQL([]) !== $platform->getStringTypeDeclarationSQL(['fixed' => true, 'length' => 36]);
|
||||
return $platform->getGuidTypeDeclarationSQL([]) !== $platform->getStringTypeDeclarationSQL([
|
||||
'fixed' => true,
|
||||
'length' => 36,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function register(): void
|
||||
{
|
||||
if (Type::hasType(self::NAME)) {
|
||||
Type::overrideType(self::NAME, self::class);
|
||||
return;
|
||||
}
|
||||
|
||||
Type::addType(self::NAME, self::class);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue