Skip to content

Commit

Permalink
Add Symfony serializer support (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
polc authored and thewilkybarkid committed Dec 8, 2016
1 parent 697718b commit eb29531
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<argument type="service" id="libphonenumber.phone_number_util"/>
</service>

<service id="misd_phone_number.serializer.normalizer" class="Misd\PhoneNumberBundle\Serializer\Normalizer\PhoneNumberNormalizer">
<tag name="serializer.normalizer" />

<argument type="service" id="libphonenumber.phone_number_util" />
</service>
</services>

</container>
102 changes: 102 additions & 0 deletions Serializer/Normalizer/PhoneNumberNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
* (c) University of Cambridge
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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\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;

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

/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function normalize($object, $format = null, array $context = array())
{
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 = array())
{
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 $type === 'libphonenumber\PhoneNumber';
}
}
101 changes: 101 additions & 0 deletions Tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* This file is part of the Symfony2 PhoneNumberBundle.
*
* (c) University of Cambridge
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Misd\PhoneNumberBundle\Tests\Serializer\Handler;

use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Misd\PhoneNumberBundle\Serializer\Normalizer\PhoneNumberNormalizer;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Phone number serialization test.
*/
class PhoneNumberNormalizerTest extends TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\Serializer\Serializer')) {
$this->markTestSkipped('The Symfony Serializer is not available.');
}
}

public function testSupportNormalization()
{
$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();

$normalizer = new PhoneNumberNormalizer($phoneNumberUtil);

$this->assertTrue($normalizer->supportsNormalization(new PhoneNumber()));
$this->assertFalse($normalizer->supportsNormalization(new \stdClass()));
}

public function testNormalize()
{
$phoneNumber = new PhoneNumber();
$phoneNumber->setRawInput('+33193166989');

$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();
$phoneNumberUtil->expects($this->once())->method('format')->with($phoneNumber, PhoneNumberFormat::E164)->will($this->returnValue('+33193166989'));

$normalizer = new PhoneNumberNormalizer($phoneNumberUtil);

$this->assertEquals('+33193166989', $normalizer->normalize($phoneNumber));
}

public function testSupportDenormalization()
{
$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();

$normalizer = new PhoneNumberNormalizer($phoneNumberUtil);

$this->assertTrue($normalizer->supportsDenormalization('+33193166989', 'libphonenumber\PhoneNumber'));
$this->assertFalse($normalizer->supportsDenormalization('+33193166989', 'stdClass'));
}

public function testDenormalize()
{
$phoneNumber = new PhoneNumber();
$phoneNumber->setRawInput('+33193166989');

$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();

$phoneNumberUtil->expects($this->once())->method('parse')
->with('+33193166989', PhoneNumberUtil::UNKNOWN_REGION)
->will($this->returnValue($phoneNumber));

$normalizer = new PhoneNumberNormalizer($phoneNumberUtil);

$this->assertSame($phoneNumber, $normalizer->denormalize('+33193166989', 'libphonenumber\PhoneNumber'));
}

/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testInvalidDateThrowException()
{
$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();

$phoneNumberUtil->expects($this->once())->method('parse')
->with('invalid phone number', PhoneNumberUtil::UNKNOWN_REGION)
->willThrowException(new NumberParseException(NumberParseException::INVALID_COUNTRY_CODE, ""));

$normalizer = new PhoneNumberNormalizer($phoneNumberUtil);
$normalizer->denormalize('invalid phone number', 'libphonenumber\PhoneNumber');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"suggest": {
"doctrine/doctrine-bundle": "Add a DBAL mapping type",
"jms/serializer-bundle": "Serialize/deserialize phone numbers",
"jms/serializer-bundle": "Serialize/deserialize phone numbers using JSM library",
"symfony/serializer": "Serialize/deserialize phone numbers using Symfony library",
"symfony/form": "Add a data transformer",
"symfony/templating": "Format phone numbers in templates",
"symfony/twig-bundle": "Format phone numbers in Twig templates",
Expand Down

0 comments on commit eb29531

Please sign in to comment.