Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Symfony 6 #96

Merged
merged 5 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stability requirement can be removed once Symfony 6.0.1 was released

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

namespace Misd\PhoneNumberBundle\Serializer\Normalizer;

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* @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;
}

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);
}
}
114 changes: 41 additions & 73 deletions src/Serializer/Normalizer/PhoneNumberNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,64 @@

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')) {
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}

} else {
// Symfony < 6.0
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
* Phone number serialization for Symfony serializer.
*/
public function normalize($object, $format = null, array $context = [])
class PhoneNumberNormalizer implements NormalizerInterface, DenormalizerInterface
{
return $this->phoneNumberUtil->format($object, $this->format);
}
use CommonPhoneNumberNormalizerTrait;

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof PhoneNumber;
}
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function normalize($object, $format = null, array $context = [])
{
return $this->doNormalize($object, $format, $context);
}

/**
* {@inheritdoc}
*
* @throws UnexpectedValueException
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if (null === $data) {
return;
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $this->doSupportsNormalization($data, $format);
}

try {
return $this->phoneNumberUtil->parse($data, $this->region);
} catch (NumberParseException $e) {
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
/**
* {@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 'libphonenumber\PhoneNumber' === $type && \is_string($data);
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return $this->doSupportsDenormalization($data, $type, $format);
}
}
}
52 changes: 52 additions & 0 deletions src/Serializer/Normalizer/PhoneNumberNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Misd\PhoneNumberBundle\Serializer\Normalizer;

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
{
use CommonPhoneNumberNormalizerTrait;

/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
}
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