Skip to content

Commit

Permalink
Doctrine DBAL 4.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Apr 6, 2024
1 parent 03fde4f commit c4a74db
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=8.1.0 <8.4",
"doctrine/dbal": "^3.4@dev",
"doctrine/dbal": "^3.4@dev || ^4.0@dev",
"brick/phonenumber": "^0.2@dev || ^0.3@dev || ^0.4@dev || ^0.5@dev || ^0.6@dev"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ includes:
- vendor/spaze/phpstan-disallowed-calls/disallowed-loose-calls.neon
- tests/PHPStan/disallowedCalls.neon
- tests/PHPStan/shipmonk.neon
- tests/PHPStan/conditional.config.php

parameters:
level: max
Expand Down
22 changes: 19 additions & 3 deletions src/PhoneNumberDoctrine/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
use Brick\PhoneNumber\PhoneNumberFormat;
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\StringType;
use libphonenumber\PhoneNumberUtil;
use function class_exists;

class PhoneNumberType extends StringType
{

/**
* @deprecated Kept for DBAL 3.x compatibility
*/
public function getName(): string
{
return PhoneNumber::class;
Expand All @@ -34,7 +40,9 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Ph
try {
return PhoneNumber::parse($value);
} catch (\Throwable $exception) {
throw ConversionException::conversionFailed($value, $this->getName(), $exception);
throw class_exists(ValueNotConvertible::class)
? ValueNotConvertible::new($value, $this->getName(), null, $exception)
: throw ConversionException::conversionFailed($value, $this->getName(), $exception);
}
}

Expand All @@ -51,18 +59,26 @@ public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform)
try {
$value = PhoneNumber::parse($value);
} catch (\Throwable $exception) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', PhoneNumber::class, 'phone number string'], $exception);
throw class_exists(InvalidType::class)
? InvalidType::new($value, $this->getName(), ['null', PhoneNumber::class, 'phone number string'], $exception)
: ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', PhoneNumber::class, 'phone number string'], $exception);
}
}

return $value->format(PhoneNumberFormat::E164);
}

/**
* @deprecated Kept for DBAL 3.x compatibility
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* @deprecated Kept for DBAL 3.x compatibility
*/
public function getDefaultLength(AbstractPlatform $platform): int
{
return 1 + PhoneNumberUtil::MAX_LENGTH_COUNTRY_CODE + PhoneNumberUtil::MAX_LENGTH_FOR_NSN;
Expand All @@ -74,7 +90,7 @@ public function getDefaultLength(AbstractPlatform $platform): int
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
$fieldDeclaration['length'] = $this->getDefaultLength($platform);
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
return parent::getSQLDeclaration($fieldDeclaration, $platform);
}

}
6 changes: 3 additions & 3 deletions tests/PhoneNumberDoctrine/PhoneNumberTypeTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PhoneNumberTypeTest extends TestCase
},
ConversionException::class,
sprintf(
'Could not convert PHP value \'foo\'%%a?%% to type %%S?%%%s%%S?%%. Expected one of the following types: null, Brick\PhoneNumber\PhoneNumber, phone number string',
'Could not convert PHP value \'foo\'%%a?%% to type %%S?%%%s%%S?%%. Expected one of the following types: null, Brick\PhoneNumber\PhoneNumber, phone number string%%S?%%',
PhoneNumber::class,
),
);
Expand Down Expand Up @@ -106,7 +106,7 @@ class PhoneNumberTypeTest extends TestCase
$this->type->convertToPHPValue('foo', $this->platform);
},
ConversionException::class,
sprintf('Could not convert database value "foo" to Doctrine Type %s', PhoneNumber::class),
sprintf('Could not convert database value "foo" to Doctrine Type %%S?%%%s%%S?%%', PhoneNumber::class),
);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ class PhoneNumberTypeTest extends TestCase

public function testGetSQLDeclaration(): void
{
$this->platform->shouldReceive('getVarcharTypeDeclarationSQL')->with(['length' => 21])->andReturn('MOCKVARCHAR');
$this->platform->shouldReceive('getStringTypeDeclarationSQL')->with(['length' => 21])->andReturn('MOCKVARCHAR');
$declaration = $this->type->getSQLDeclaration(['length' => 255], $this->platform);
Assert::same('MOCKVARCHAR', $declaration);
}
Expand Down

0 comments on commit c4a74db

Please sign in to comment.