-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Symfony serializer support (#98)
- Loading branch information
1 parent
697718b
commit eb29531
Showing
4 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
Tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters