Skip to content

Commit

Permalink
feat: allow doctrine/dbal v4 and remove older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
kochen committed Feb 7, 2024
1 parent fafebbe commit 8dade32
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 77 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"doctrine/dbal": "^2.8 || ^3.0",
"php": "^8.0",
"doctrine/dbal": "^4.0",
"ramsey/uuid": "^3.9.7 || ^4.0"
},
"require-dev": {
"captainhook/plugin-composer": "^5.3",
"doctrine/orm": "^2.5",
"doctrine/orm": "^3.0",
"ergebnis/composer-normalize": "^2.28.3",
"mockery/mockery": "^1.5",
"php-parallel-lint/php-console-highlighter": "^1.0",
Expand Down
25 changes: 9 additions & 16 deletions src/UuidBinaryOrderedTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidFormat;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Codec\OrderedTimeCodec;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
Expand Down Expand Up @@ -52,7 +55,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
{
return $platform->getBinaryTypeDeclarationSQL(
[
'length' => '16',
'length' => 16,
'fixed' => true,
],
);
Expand All @@ -76,7 +79,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
return $this->decode($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
throw ValueNotConvertible::new($value, self::NAME);
}
}

Expand Down Expand Up @@ -105,20 +108,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
// Ignore the exception and pass through.
}

throw ConversionException::conversionFailed($value, self::NAME);
throw InvalidType::new($value, self::NAME, ['null', 'string', UuidInterface::class]);
}

public function getName(): string
{
return self::NAME;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

public function getBindingType(): int
public function getBindingType(): ParameterType
{
return ParameterType::BINARY;
}
Expand Down Expand Up @@ -158,7 +151,7 @@ private function assertUuidV1(UuidInterface $value): void
{
/** @psalm-suppress DeprecatedMethod */
if ($value->getVersion() !== 1) {
throw ConversionException::conversionFailedFormat(
throw InvalidFormat::new(
$value->toString(),
self::NAME,
self::ASSERT_FORMAT,
Expand All @@ -184,7 +177,7 @@ private function decode(string $bytes): UuidInterface
try {
$decoded = $this->getCodec()->decodeBytes($bytes);
} catch (UnsupportedOperationException $e) {
throw ConversionException::conversionFailedFormat(
throw InvalidFormat::new(
bin2hex($bytes),
self::NAME,
self::ASSERT_FORMAT,
Expand Down
20 changes: 6 additions & 14 deletions src/UuidBinaryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
Expand All @@ -43,7 +45,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
{
return $platform->getBinaryTypeDeclarationSQL(
[
'length' => '16',
'length' => 16,
'fixed' => true,
],
);
Expand All @@ -67,7 +69,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
$uuid = Uuid::fromBytes($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
throw ValueNotConvertible::new($value, self::NAME);
}

return $uuid;
Expand Down Expand Up @@ -96,20 +98,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
// Ignore the exception and pass through.
}

throw ConversionException::conversionFailed($value, self::NAME);
throw InvalidType::new($value, self::NAME, ['null', 'string', UuidInterface::class]);
}

public function getName(): string
{
return self::NAME;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

public function getBindingType(): int
public function getBindingType(): ParameterType
{
return ParameterType::BINARY;
}
Expand Down
16 changes: 4 additions & 12 deletions src/UuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\GuidType;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
Expand Down Expand Up @@ -53,7 +55,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
try {
$uuid = Uuid::fromString($value);
} catch (Throwable $e) {
throw ConversionException::conversionFailed($value, self::NAME);
throw ValueNotConvertible::new($value, self::NAME);
}

return $uuid;
Expand Down Expand Up @@ -81,17 +83,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
return (string) $value;
}

throw ConversionException::conversionFailed($value, self::NAME);
}

public function getName(): string
{
return self::NAME;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
throw InvalidType::new($value, self::NAME, ['null', 'string', UuidInterface::class, Uuid::class]);
}

/**
Expand Down
13 changes: 2 additions & 11 deletions tests/UuidBinaryOrderedTimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -44,11 +45,6 @@ private function getType(): Type
return Type::getType('uuid_binary_ordered_time');
}

public function testGetName(): void
{
$this->assertSame('uuid_binary_ordered_time', $this->getType()->getName());
}

public function testUuidConvertsToDatabaseValue(): void
{
$uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66');
Expand Down Expand Up @@ -135,13 +131,8 @@ public function testGetGuidTypeDeclarationSQL(): void
);
}

public function testRequiresSQLCommentHint(): void
{
$this->assertTrue($this->getType()->requiresSQLCommentHint($this->getPlatform()));
}

public function testItReturnsAppropriateBindingType(): void
{
$this->assertEquals(16, $this->getType()->getBindingType());
$this->assertEquals(ParameterType::BINARY, $this->getType()->getBindingType());
}
}
13 changes: 2 additions & 11 deletions tests/UuidBinaryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ramsey\Uuid\Doctrine;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -120,11 +121,6 @@ public function testReturnValueIfUuid7ForPHPValue(): void
$this->assertSame($uuid, $this->getType()->convertToPHPValue($uuid, $this->getPlatform()));
}

public function testGetName(): void
{
$this->assertSame('uuid_binary', $this->getType()->getName());
}

public function testGetGuidTypeDeclarationSQL(): void
{
$this->assertSame(
Expand All @@ -133,13 +129,8 @@ public function testGetGuidTypeDeclarationSQL(): void
);
}

public function testRequiresSQLCommentHint(): void
{
$this->assertTrue($this->getType()->requiresSQLCommentHint($this->getPlatform()));
}

public function testItReturnsAppropriateBindingType(): void
{
$this->assertEquals(16, $this->getType()->getBindingType());
$this->assertEquals(ParameterType::BINARY, $this->getType()->getBindingType());
}
}
10 changes: 0 additions & 10 deletions tests/UuidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ public function testReturnValueIfUuidForPHPValue(): void
$this->assertSame($uuid, $this->getType()->convertToPHPValue($uuid, $this->getPlatform()));
}

public function testGetName(): void
{
$this->assertSame('uuid', $this->getType()->getName());
}

public function testGetGuidTypeDeclarationSQL(): void
{
$this->assertSame(
Expand All @@ -138,11 +133,6 @@ public function testGetGuidTypeDeclarationSQL(): void
);
}

public function testRequiresSQLCommentHint(): void
{
$this->assertTrue($this->getType()->requiresSQLCommentHint($this->getPlatform()));
}

public function testGetMappedDatabaseTypes(): void
{
$this->assertSame(['uuid'], $this->getType()->getMappedDatabaseTypes($this->getPlatform()));
Expand Down

0 comments on commit 8dade32

Please sign in to comment.