diff --git a/src/Ulid/UlidType.php b/src/Ulid/UlidType.php index bc0a0a8..d05c21b 100644 --- a/src/Ulid/UlidType.php +++ b/src/Ulid/UlidType.php @@ -8,7 +8,6 @@ use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\GuidType; use Symfony\Component\Uid\Ulid; -use Throwable; /** * @author Guillaume Loulier @@ -42,6 +41,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform) return null; } + if ($value instanceof Ulid) { + return $value; + } + if (!Ulid::isValid($value)) { throw ConversionException::conversionFailed($value, self::NAME); } diff --git a/src/Uuid/UuidType.php b/src/Uuid/UuidType.php index 8284acf..2eb00d3 100644 --- a/src/Uuid/UuidType.php +++ b/src/Uuid/UuidType.php @@ -42,6 +42,10 @@ public function convertToPHPValue($value, AbstractPlatform $platform) return null; } + if ($value instanceof Uuid) { + return $value; + } + if (!Uuid::isValid($value)) { throw ConversionException::conversionFailed($value, self::NAME); } diff --git a/tests/Ulid/UlidTypeTest.php b/tests/Ulid/UlidTypeTest.php index f538cb7..b432734 100644 --- a/tests/Ulid/UlidTypeTest.php +++ b/tests/Ulid/UlidTypeTest.php @@ -102,4 +102,14 @@ public function testTypeCanBeConvertedToPHPValueWhenUsingValidString(): void $value = Type::getType('ulid')->convertToPHPValue(strtr($entryValue, ['-' => '0']), $platform); static::assertInstanceOf(Ulid::class, $value); } + + public function testTypeCanBeConvertedToPHPValueWhenUsingAnUlidObject(): void + { + $platform = $this->createMock(AbstractPlatform::class); + + $ulid = new Ulid(); + + $value = Type::getType('ulid')->convertToPHPValue($ulid, $platform); + static::assertInstanceOf(Ulid::class, $value); + } } diff --git a/tests/Uuid/UuidTypeTest.php b/tests/Uuid/UuidTypeTest.php index 82ba76c..66619e1 100644 --- a/tests/Uuid/UuidTypeTest.php +++ b/tests/Uuid/UuidTypeTest.php @@ -97,4 +97,14 @@ public function testTypeCanBeConvertedToPHPValueWhenUsingUuid(): void $value = Type::getType('uuid')->convertToPHPValue($uuid->toRfc4122(), $platform); static::assertInstanceOf(Uuid::class, $value); } + + public function testTypeCanBeConvertedToPHPValueWhenUsingAnUuidObject(): void + { + $platform = $this->createMock(AbstractPlatform::class); + + $uuid = Uuid::v4(); + + $value = Type::getType('uuid')->convertToPHPValue($uuid, $platform); + static::assertInstanceOf(Uuid::class, $value); + } }