From f4bd90a178aa0e4c45ab90e510e85de5111cb124 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Sun, 28 Nov 2021 22:32:58 +0100 Subject: [PATCH 1/5] add support for Symfony 6 --- .github/workflows/tests.yml | 25 ++++- composer.json | 17 ++-- .../CommonPhoneNumberNormalizerTrait.php | 80 ++++++++++++++++ .../LegacyPhoneNumberNormalizerTrait.php | 52 +++++++++++ .../Normalizer/PhoneNumberNormalizer.php | 92 +++---------------- .../Normalizer/PhoneNumberNormalizerTrait.php | 52 +++++++++++ .../DBAL/Types/PhoneNumberTypeTest.php | 3 + .../Normalizer/PhoneNumberNormalizerTest.php | 3 + .../Helper/PhoneNumberHelperTest.php | 3 + .../Constraints/PhoneNumberValidatorTest.php | 38 +++++--- 10 files changed, 261 insertions(+), 104 deletions(-) create mode 100644 src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php create mode 100644 src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php create mode 100644 src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 988beb6b..437e9d23 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,18 +14,37 @@ jobs: matrix: include: - php: '7.4' + symfony-require: 4.4.* - php: '8.0' + symfony-require: 5.3.* + - php: '8.0' + symfony-require: 6.0.* + stability: dev - php: '8.1' mode: experimental + fail-fast: false steps: - name: Checkout uses: actions/checkout@v2 - - name: "Install dependencies with Composer" - uses: "ramsey/composer-install@v1" + - name: Setup PHP + uses: shivammathur/setup-php@v2 with: - composer-options: "--prefer-dist" + php-version: "${{ matrix.php }}" + coverage: none + + - name: Configure Composer minimum stability + if: matrix.stability + run: composer config minimum-stability ${{ matrix.stability }} + + - name: Install symfony/flex + run: composer global require symfony/flex + + - name: Install dependencies + env: + SYMFONY_REQUIRE: "${{ matrix.symfony-require }}" + run: composer update --prefer-dist - name: "Run PHPUnit" run: "vendor/bin/phpunit" diff --git a/composer.json b/composer.json index 53ddbada..db39ce55 100644 --- a/composer.json +++ b/composer.json @@ -14,17 +14,18 @@ "require": { "php": ">=7.4", "giggsey/libphonenumber-for-php": "^8.0", - "symfony/framework-bundle": "^4.4|^5.3", - "symfony/intl": "^4.4|^5.3" + "symfony/framework-bundle": "^4.4|^5.3|^6.0", + "symfony/intl": "^4.4|^5.3|^6.0" }, "require-dev": { "doctrine/doctrine-bundle": "^1.12|^2.0", - "phpunit/phpunit": "^8.4", - "symfony/form": "^4.4|^5.3", - "symfony/property-access": "^4.4|^5.3", - "symfony/serializer": "^4.4|^5.3", - "symfony/twig-bundle": "^4.4|^5.3", - "symfony/validator": "^4.4|^5.3" + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^9.5", + "symfony/form": "^4.4|^5.3|^6.0", + "symfony/property-access": "^4.4|^5.3|^6.0", + "symfony/serializer": "^4.4|^5.3|^6.0", + "symfony/twig-bundle": "^4.4|^5.3|^6.0", + "symfony/validator": "^4.4|^5.3|^6.0" }, "suggest": { "doctrine/doctrine-bundle": "Add a DBAL mapping type", diff --git a/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php new file mode 100644 index 00000000..28c5796e --- /dev/null +++ b/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php @@ -0,0 +1,80 @@ += 6. + */ +trait CommonPhoneNumberNormalizerTrait +{ + /** + * Region code. + * + * @var string + */ + private $region; + + /** + * Display format. + * + * @var int + */ + private $format; + + /** + * Display format. + * + * @var PhoneNumberUtil + */ + private $phoneNumberUtil; + + /** + * Constructor. + * + * @param PhoneNumberUtil $phoneNumberUtil phone number utility + * @param string $region region code + * @param int $format display format + */ + public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) + { + $this->phoneNumberUtil = $phoneNumberUtil; + $this->region = $region; + $this->format = $format; + } + + private function doNormalize($object, $format = null, array $context = []) + { + return $this->phoneNumberUtil->format($object, $this->format); + } + + private function doSupportsNormalization($data, $format = null) + { + return $data instanceof PhoneNumber; + } + + private function doDenormalize($data, $class, $format = null, array $context = []) + { + if (null === $data) { + return null; + } + + try { + return $this->phoneNumberUtil->parse($data, $this->region); + } catch (NumberParseException $e) { + throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); + } + } + + private function doSupportsDenormalization($data, $type, $format = null) + { + return 'libphonenumber\PhoneNumber' === $type && \is_string($data); + } +} diff --git a/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php new file mode 100644 index 00000000..b43a2f3b --- /dev/null +++ b/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php @@ -0,0 +1,52 @@ += 6. + */ +trait LegacyPhoneNumberNormalizerTrait +{ + use CommonPhoneNumberNormalizerTrait; + + /** + * {@inheritdoc} + * + * @throws InvalidArgumentException + */ + public function normalize($object, $format = null, array $context = []) + { + return $this->doNormalize($object, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsNormalization($data, $format = null) + { + return $this->doSupportsNormalization($data, $format); + } + + /** + * {@inheritdoc} + * + * @throws UnexpectedValueException + */ + public function denormalize($data, $class, $format = null, array $context = []) + { + return $this->doDenormalize($data, $class, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsDenormalization($data, $type, $format = null) + { + return $this->doSupportsDenormalization($data, $type, $format); + } +} diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizer.php b/src/Serializer/Normalizer/PhoneNumberNormalizer.php index 7a2af51d..3d5a71bc 100644 --- a/src/Serializer/Normalizer/PhoneNumberNormalizer.php +++ b/src/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -11,96 +11,26 @@ namespace Misd\PhoneNumberBundle\Serializer\Normalizer; -use libphonenumber\NumberParseException; -use libphonenumber\PhoneNumber; -use libphonenumber\PhoneNumberFormat; -use libphonenumber\PhoneNumberUtil; -use Symfony\Component\Serializer\Exception\InvalidArgumentException; -use Symfony\Component\Serializer\Exception\UnexpectedValueException; +use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Phone number serialization for Symfony serializer. - */ -class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface -{ - /** - * Region code. - * - * @var string - */ - private $region; - - /** - * Display format. - * - * @var int - */ - private $format; - +if (class_exists(ArrayDenormalizer::class) && !method_exists(ArrayDenormalizer::class, 'setSerializer')) { + // Symfony >= 6.0 /** - * Display format. - * - * @var PhoneNumberUtil + * Phone number serialization for Symfony serializer. */ - private $phoneNumberUtil; - - /** - * Constructor. - * - * @param PhoneNumberUtil $phoneNumberUtil phone number utility - * @param string $region region code - * @param int $format display format - */ - public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) + class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface { - $this->phoneNumberUtil = $phoneNumberUtil; - $this->region = $region; - $this->format = $format; + use PhoneNumberNormalizerTrait; } - - /** - * {@inheritdoc} - * - * @throws InvalidArgumentException - */ - public function normalize($object, $format = null, array $context = []) - { - return $this->phoneNumberUtil->format($object, $this->format); - } - - /** - * {@inheritdoc} - */ - public function supportsNormalization($data, $format = null) - { - return $data instanceof PhoneNumber; - } - - /** - * {@inheritdoc} - * - * @throws UnexpectedValueException - */ - public function denormalize($data, $class, $format = null, array $context = []) - { - if (null === $data) { - return; - } - - try { - return $this->phoneNumberUtil->parse($data, $this->region); - } catch (NumberParseException $e) { - throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); - } - } - +} else { + // Symfony < 6.0 /** - * {@inheritdoc} + * Phone number serialization for Symfony serializer. */ - public function supportsDenormalization($data, $type, $format = null) + class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface { - return 'libphonenumber\PhoneNumber' === $type && \is_string($data); + use LegacyPhoneNumberNormalizerTrait; } } diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php new file mode 100644 index 00000000..3ce08852 --- /dev/null +++ b/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php @@ -0,0 +1,52 @@ += 6. + */ +trait PhoneNumberNormalizerTrait +{ + use CommonPhoneNumberNormalizerTrait; + + /** + * {@inheritdoc} + * + * @throws InvalidArgumentException + */ + public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null + { + return $this->doNormalize($object, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsNormalization(mixed $data, string $format = null): bool + { + return $this->doSupportsNormalization($data, $format); + } + + /** + * {@inheritdoc} + * + * @throws UnexpectedValueException + */ + public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed + { + return $this->doDenormalize($data, $type, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsDenormalization(mixed $data, string $type, string $format = null): bool + { + return $this->doSupportsDenormalization($data, $type, $format); + } +} diff --git a/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php b/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php index d6c5fbe2..ba5e64ed 100644 --- a/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php +++ b/tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php @@ -18,12 +18,15 @@ use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; /** * Phone number type test. */ class PhoneNumberTypeTest extends TestCase { + use ProphecyTrait; + /** * @var AbstractPlatform */ diff --git a/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php b/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php index 36626ace..20abdebe 100644 --- a/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php +++ b/tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php @@ -18,6 +18,7 @@ use Misd\PhoneNumberBundle\Serializer\Normalizer\PhoneNumberNormalizer; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Serializer; @@ -26,6 +27,8 @@ */ class PhoneNumberNormalizerTest extends TestCase { + use ProphecyTrait; + protected function setUp(): void { if (!class_exists(Serializer::class)) { diff --git a/tests/Templating/Helper/PhoneNumberHelperTest.php b/tests/Templating/Helper/PhoneNumberHelperTest.php index 7a923cad..4a97b986 100644 --- a/tests/Templating/Helper/PhoneNumberHelperTest.php +++ b/tests/Templating/Helper/PhoneNumberHelperTest.php @@ -17,12 +17,15 @@ use Misd\PhoneNumberBundle\Exception\InvalidArgumentException; use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper; use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; /** * Phone number templating helper test. */ class PhoneNumberHelperTest extends TestCase { + use ProphecyTrait; + protected $phoneNumberUtil; protected $helper; diff --git a/tests/Validator/Constraints/PhoneNumberValidatorTest.php b/tests/Validator/Constraints/PhoneNumberValidatorTest.php index 654555f2..d417455d 100644 --- a/tests/Validator/Constraints/PhoneNumberValidatorTest.php +++ b/tests/Validator/Constraints/PhoneNumberValidatorTest.php @@ -14,8 +14,8 @@ use libphonenumber\PhoneNumberUtil; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber; use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumberValidator; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Mapping\ClassMetadata; @@ -28,7 +28,7 @@ class PhoneNumberValidatorTest extends TestCase { /** - * @var \Symfony\Component\Validator\Context\ExecutionContextInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Symfony\Component\Validator\Context\ExecutionContextInterface|MockObject */ protected $context; @@ -39,12 +39,12 @@ class PhoneNumberValidatorTest extends TestCase protected function setUp(): void { - $this->context = $this->prophesize(ExecutionContextInterface::class); + $this->context = $this->createMock(ExecutionContextInterface::class); $this->validator = new PhoneNumberValidator(PhoneNumberUtil::getInstance()); - $this->validator->initialize($this->context->reveal()); + $this->validator->initialize($this->context); - $this->context->getObject()->willReturn(new Foo()); + $this->context->method('getObject')->willReturn(new Foo()); } /** @@ -67,14 +67,28 @@ public function testValidate($value, $violates, $type = null, $defaultRegion = n } if (true === $violates) { - $constraintViolationBuilder = $this->prophesize(ConstraintViolationBuilderInterface::class); - $constraintViolationBuilder->setParameter(Argument::type('string'), Argument::type('string'))->willReturn($constraintViolationBuilder->reveal()); - $constraintViolationBuilder->setCode(Argument::type('string'))->willReturn($constraintViolationBuilder->reveal()); - $constraintViolationBuilder->addViolation()->willReturn($constraintViolationBuilder->reveal()); - - $this->context->buildViolation($constraint->getMessage())->shouldBeCalledTimes(1)->willReturn($constraintViolationBuilder->reveal()); + $constraintViolationBuilder = $this->createMock(ConstraintViolationBuilderInterface::class); + $constraintViolationBuilder + ->expects($this->exactly(2)) + ->method('setParameter') + ->with($this->isType('string'), $this->isType('string')) + ->willReturn($constraintViolationBuilder); + $constraintViolationBuilder + ->expects($this->once()) + ->method('setCode') + ->with($this->isType('string')) + ->willReturn($constraintViolationBuilder); + $constraintViolationBuilder + ->method('addViolation') + ->willReturn($constraintViolationBuilder); + + $this->context + ->expects($this->once()) + ->method('buildViolation') + ->with($constraint->getMessage()) + ->willReturn($constraintViolationBuilder); } else { - $this->context->buildViolation()->shouldNotBeCalled(); + $this->context->expects($this->never())->method('buildViolation'); } $this->validator->validate($value, $constraint); From cc786e32182d62e6fb99829c71a1f79179b72264 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Mon, 29 Nov 2021 12:14:44 +0100 Subject: [PATCH 2/5] remove LegacyPhoneNumberNormalizerTrait --- .../LegacyPhoneNumberNormalizerTrait.php | 52 ------------------- .../Normalizer/PhoneNumberNormalizer.php | 40 +++++++++++++- 2 files changed, 39 insertions(+), 53 deletions(-) delete mode 100644 src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php diff --git a/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php deleted file mode 100644 index b43a2f3b..00000000 --- a/src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php +++ /dev/null @@ -1,52 +0,0 @@ -= 6. - */ -trait LegacyPhoneNumberNormalizerTrait -{ - use CommonPhoneNumberNormalizerTrait; - - /** - * {@inheritdoc} - * - * @throws InvalidArgumentException - */ - public function normalize($object, $format = null, array $context = []) - { - return $this->doNormalize($object, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function supportsNormalization($data, $format = null) - { - return $this->doSupportsNormalization($data, $format); - } - - /** - * {@inheritdoc} - * - * @throws UnexpectedValueException - */ - public function denormalize($data, $class, $format = null, array $context = []) - { - return $this->doDenormalize($data, $class, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function supportsDenormalization($data, $type, $format = null) - { - return $this->doSupportsDenormalization($data, $type, $format); - } -} diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizer.php b/src/Serializer/Normalizer/PhoneNumberNormalizer.php index 3d5a71bc..34744016 100644 --- a/src/Serializer/Normalizer/PhoneNumberNormalizer.php +++ b/src/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -11,6 +11,8 @@ namespace Misd\PhoneNumberBundle\Serializer\Normalizer; +use Symfony\Component\Serializer\Exception\InvalidArgumentException; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -31,6 +33,42 @@ class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterfac */ class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface { - use LegacyPhoneNumberNormalizerTrait; + use CommonPhoneNumberNormalizerTrait; + + /** + * {@inheritdoc} + * + * @throws InvalidArgumentException + */ + public function normalize($object, $format = null, array $context = []) + { + return $this->doNormalize($object, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsNormalization($data, $format = null) + { + return $this->doSupportsNormalization($data, $format); + } + + /** + * {@inheritdoc} + * + * @throws UnexpectedValueException + */ + public function denormalize($data, $class, $format = null, array $context = []) + { + return $this->doDenormalize($data, $class, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsDenormalization($data, $type, $format = null) + { + return $this->doSupportsDenormalization($data, $type, $format); + } } } From a46a61bddb428b9451a07cc6db8860a341d79626 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Wed, 1 Dec 2021 17:46:44 +0100 Subject: [PATCH 3/5] revert PhoneNumberNormalizer changes --- .github/workflows/tests.yml | 2 +- .../CommonPhoneNumberNormalizerTrait.php | 80 ------------ .../Normalizer/PhoneNumberNormalizer.php | 114 +++++++++++------- .../Normalizer/PhoneNumberNormalizerTrait.php | 52 -------- 4 files changed, 74 insertions(+), 174 deletions(-) delete mode 100644 src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php delete mode 100644 src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 437e9d23..1e4a70e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,7 @@ jobs: symfony-require: 6.0.* stability: dev - php: '8.1' - mode: experimental + stability: dev fail-fast: false steps: diff --git a/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php deleted file mode 100644 index 28c5796e..00000000 --- a/src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php +++ /dev/null @@ -1,80 +0,0 @@ -= 6. - */ -trait CommonPhoneNumberNormalizerTrait -{ - /** - * Region code. - * - * @var string - */ - private $region; - - /** - * Display format. - * - * @var int - */ - private $format; - - /** - * Display format. - * - * @var PhoneNumberUtil - */ - private $phoneNumberUtil; - - /** - * Constructor. - * - * @param PhoneNumberUtil $phoneNumberUtil phone number utility - * @param string $region region code - * @param int $format display format - */ - public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) - { - $this->phoneNumberUtil = $phoneNumberUtil; - $this->region = $region; - $this->format = $format; - } - - private function doNormalize($object, $format = null, array $context = []) - { - return $this->phoneNumberUtil->format($object, $this->format); - } - - private function doSupportsNormalization($data, $format = null) - { - return $data instanceof PhoneNumber; - } - - private function doDenormalize($data, $class, $format = null, array $context = []) - { - if (null === $data) { - return null; - } - - try { - return $this->phoneNumberUtil->parse($data, $this->region); - } catch (NumberParseException $e) { - throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); - } - } - - private function doSupportsDenormalization($data, $type, $format = null) - { - return 'libphonenumber\PhoneNumber' === $type && \is_string($data); - } -} diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizer.php b/src/Serializer/Normalizer/PhoneNumberNormalizer.php index 34744016..7a2af51d 100644 --- a/src/Serializer/Normalizer/PhoneNumberNormalizer.php +++ b/src/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -11,64 +11,96 @@ namespace Misd\PhoneNumberBundle\Serializer\Normalizer; +use libphonenumber\NumberParseException; +use libphonenumber\PhoneNumber; +use libphonenumber\PhoneNumberFormat; +use libphonenumber\PhoneNumberUtil; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\UnexpectedValueException; -use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -if (class_exists(ArrayDenormalizer::class) && !method_exists(ArrayDenormalizer::class, 'setSerializer')) { - // Symfony >= 6.0 +/** + * Phone number serialization for Symfony serializer. + */ +class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface +{ + /** + * Region code. + * + * @var string + */ + private $region; + + /** + * Display format. + * + * @var int + */ + private $format; + /** - * Phone number serialization for Symfony serializer. + * Display format. + * + * @var PhoneNumberUtil */ - class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface + private $phoneNumberUtil; + + /** + * Constructor. + * + * @param PhoneNumberUtil $phoneNumberUtil phone number utility + * @param string $region region code + * @param int $format display format + */ + public function __construct(PhoneNumberUtil $phoneNumberUtil, $region = PhoneNumberUtil::UNKNOWN_REGION, $format = PhoneNumberFormat::E164) { - use PhoneNumberNormalizerTrait; + $this->phoneNumberUtil = $phoneNumberUtil; + $this->region = $region; + $this->format = $format; } -} else { - // Symfony < 6.0 + /** - * Phone number serialization for Symfony serializer. + * {@inheritdoc} + * + * @throws InvalidArgumentException */ - class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface + public function normalize($object, $format = null, array $context = []) { - use CommonPhoneNumberNormalizerTrait; + return $this->phoneNumberUtil->format($object, $this->format); + } - /** - * {@inheritdoc} - * - * @throws InvalidArgumentException - */ - public function normalize($object, $format = null, array $context = []) - { - return $this->doNormalize($object, $format, $context); - } + /** + * {@inheritdoc} + */ + public function supportsNormalization($data, $format = null) + { + return $data instanceof PhoneNumber; + } - /** - * {@inheritdoc} - */ - public function supportsNormalization($data, $format = null) - { - return $this->doSupportsNormalization($data, $format); + /** + * {@inheritdoc} + * + * @throws UnexpectedValueException + */ + public function denormalize($data, $class, $format = null, array $context = []) + { + if (null === $data) { + return; } - /** - * {@inheritdoc} - * - * @throws UnexpectedValueException - */ - public function denormalize($data, $class, $format = null, array $context = []) - { - return $this->doDenormalize($data, $class, $format, $context); + try { + return $this->phoneNumberUtil->parse($data, $this->region); + } catch (NumberParseException $e) { + throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); } + } - /** - * {@inheritdoc} - */ - public function supportsDenormalization($data, $type, $format = null) - { - return $this->doSupportsDenormalization($data, $type, $format); - } + /** + * {@inheritdoc} + */ + public function supportsDenormalization($data, $type, $format = null) + { + return 'libphonenumber\PhoneNumber' === $type && \is_string($data); } } diff --git a/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php b/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php deleted file mode 100644 index 3ce08852..00000000 --- a/src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php +++ /dev/null @@ -1,52 +0,0 @@ -= 6. - */ -trait PhoneNumberNormalizerTrait -{ - use CommonPhoneNumberNormalizerTrait; - - /** - * {@inheritdoc} - * - * @throws InvalidArgumentException - */ - public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null - { - return $this->doNormalize($object, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function supportsNormalization(mixed $data, string $format = null): bool - { - return $this->doSupportsNormalization($data, $format); - } - - /** - * {@inheritdoc} - * - * @throws UnexpectedValueException - */ - public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed - { - return $this->doDenormalize($data, $type, $format, $context); - } - - /** - * {@inheritdoc} - */ - public function supportsDenormalization(mixed $data, string $type, string $format = null): bool - { - return $this->doSupportsDenormalization($data, $type, $format); - } -} From aa38f516bc1c9cb080faacef620e810a07ac6578 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Wed, 1 Dec 2021 17:51:07 +0100 Subject: [PATCH 4/5] require symfony/serializer ^6.0.1 --- .github/workflows/tests.yml | 1 - composer.json | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1e4a70e4..dbe953f8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,6 @@ jobs: symfony-require: 6.0.* stability: dev - php: '8.1' - stability: dev fail-fast: false steps: diff --git a/composer.json b/composer.json index db39ce55..00f12867 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "phpunit/phpunit": "^9.5", "symfony/form": "^4.4|^5.3|^6.0", "symfony/property-access": "^4.4|^5.3|^6.0", - "symfony/serializer": "^4.4|^5.3|^6.0", + "symfony/serializer": "^4.4|^5.3|^6.0.1", "symfony/twig-bundle": "^4.4|^5.3|^6.0", "symfony/validator": "^4.4|^5.3|^6.0" }, @@ -52,5 +52,8 @@ "branch-alias": { "dev-master": "3.5.x-dev" } + }, + "conflict": { + "symfony/serializer": "6.0.0" } } From 43a613763b7789f16e1f5ce0d870690b70c9588c Mon Sep 17 00:00:00 2001 From: David Maicher Date: Wed, 1 Dec 2021 17:59:37 +0100 Subject: [PATCH 5/5] add return type hints to silence deprecation warnings --- src/Twig/Extension/PhoneNumberHelperExtension.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Twig/Extension/PhoneNumberHelperExtension.php b/src/Twig/Extension/PhoneNumberHelperExtension.php index 71bbbf44..bb7f6e3a 100644 --- a/src/Twig/Extension/PhoneNumberHelperExtension.php +++ b/src/Twig/Extension/PhoneNumberHelperExtension.php @@ -41,6 +41,8 @@ public function __construct(PhoneNumberHelper $helper) /** * {@inheritdoc} + * + * @return array */ public function getFunctions() { @@ -53,6 +55,8 @@ public function getFunctions() /** * {@inheritdoc} + * + * @return array */ public function getFilters() { @@ -64,6 +68,8 @@ public function getFilters() /** * {@inheritdoc} + * + * @return array */ public function getTests() {