Skip to content

Commit

Permalink
add support for Symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Nov 28, 2021
1 parent 39bbeac commit 2c1510b
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 104 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions src/Serializer/Normalizer/CommonPhoneNumberNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Misd\PhoneNumberBundle\Serializer\Normalizer;

use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;

/**
* @internal
*
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 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;
}
}
60 changes: 60 additions & 0 deletions src/Serializer/Normalizer/LegacyPhoneNumberNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Misd\PhoneNumberBundle\Serializer\Normalizer;

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* @internal
*
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 6.
*/
trait LegacyPhoneNumberNormalizerTrait
{
/**
* {@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);
}
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return 'libphonenumber\PhoneNumber' === $type && \is_string($data);
}
}
94 changes: 13 additions & 81 deletions src/Serializer/Normalizer/PhoneNumberNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,28 @@

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 CommonPhoneNumberNormalizerTrait;
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 CommonPhoneNumberNormalizerTrait;
use LegacyPhoneNumberNormalizerTrait;
}
}
60 changes: 60 additions & 0 deletions src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Misd\PhoneNumberBundle\Serializer\Normalizer;

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* @internal
*
* Do not use directly. Just used for achieving compatibility with Symfony < 6 and >= 6.
*/
trait PhoneNumberNormalizerTrait
{
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
return $this->phoneNumberUtil->format($object, $this->format);
}

/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, string $format = null): bool
{
return $data instanceof PhoneNumber;
}

/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
{
if (null === $data) {
return null;
}

try {
return $this->phoneNumberUtil->parse($data, $this->region);
} catch (NumberParseException $e) {
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
}
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
{
return 'libphonenumber\PhoneNumber' === $type && \is_string($data);
}
}
3 changes: 3 additions & 0 deletions tests/Doctrine/DBAL/Types/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,6 +27,8 @@
*/
class PhoneNumberNormalizerTest extends TestCase
{
use ProphecyTrait;

protected function setUp(): void
{
if (!class_exists(Serializer::class)) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Templating/Helper/PhoneNumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 2c1510b

Please sign in to comment.