diff --git a/Resources/config/services.xml b/Resources/config/services.xml index a6f6bddc..f872ef0e 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -60,6 +60,11 @@ + + + + + diff --git a/Serializer/Normalizer/PhoneNumberNormalizer.php b/Serializer/Normalizer/PhoneNumberNormalizer.php new file mode 100644 index 00000000..f73b38b4 --- /dev/null +++ b/Serializer/Normalizer/PhoneNumberNormalizer.php @@ -0,0 +1,102 @@ +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'; + } +} \ No newline at end of file diff --git a/Tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php b/Tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php new file mode 100644 index 00000000..c93dd796 --- /dev/null +++ b/Tests/Serializer/Normalizer/PhoneNumberNormalizerTest.php @@ -0,0 +1,101 @@ +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'); + } +} diff --git a/composer.json b/composer.json index 22d5e952..acd073fc 100644 --- a/composer.json +++ b/composer.json @@ -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",