diff --git a/DependencyInjection/MisdPhoneNumberExtension.php b/DependencyInjection/MisdPhoneNumberExtension.php
index 904694c6..e51c86a3 100644
--- a/DependencyInjection/MisdPhoneNumberExtension.php
+++ b/DependencyInjection/MisdPhoneNumberExtension.php
@@ -32,9 +32,9 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
if (interface_exists('Symfony\Component\Templating\Helper\HelperInterface')) {
$loader->load('templating.xml');
- if (class_exists('Symfony\Bundle\TwigBundle\TwigBundle')) {
- $loader->load('twig.xml');
- }
+ }
+ if (class_exists('Symfony\Bundle\TwigBundle\TwigBundle')) {
+ $loader->load('twig.xml');
}
if (interface_exists('Symfony\Component\Form\FormTypeInterface')) {
$loader->load('form.xml');
diff --git a/Formatter/PhoneNumberFormatter.php b/Formatter/PhoneNumberFormatter.php
new file mode 100644
index 00000000..9274495b
--- /dev/null
+++ b/Formatter/PhoneNumberFormatter.php
@@ -0,0 +1,89 @@
+phoneNumberUtil = $phoneNumberUtil;
+ }
+
+ /**
+ * Format a phone number.
+ *
+ * @param PhoneNumber $phoneNumber Phone number.
+ * @param int|string $format Format, or format constant name.
+ *
+ * @return string Formatted phone number.
+ *
+ * @throws InvalidArgumentException If an argument is invalid.
+ */
+ public function format(PhoneNumber $phoneNumber, $format = PhoneNumberFormat::INTERNATIONAL)
+ {
+ if (true === is_string($format)) {
+ $constant = '\libphonenumber\PhoneNumberFormat::' . $format;
+
+ if (false === defined($constant)) {
+ throw new InvalidArgumentException('The format must be either a constant value or name in libphonenumber\PhoneNumberFormat');
+ }
+
+ $format = constant('\libphonenumber\PhoneNumberFormat::' . $format);
+ }
+
+ return $this->phoneNumberUtil->format($phoneNumber, $format);
+ }
+
+ /**
+ * @param PhoneNumber $phoneNumber Phone number.
+ * @param int|string $type PhoneNumberType, or PhoneNumberType constant name.
+ *
+ * @return bool
+ *
+ * @throws InvalidArgumentException If type argument is invalid.
+ */
+ public function isType(PhoneNumber $phoneNumber, $type = PhoneNumberType::UNKNOWN)
+ {
+ if (true === is_string($type)) {
+ $constant = '\libphonenumber\PhoneNumberType::' . $type;
+
+ if (false === defined($constant)) {
+ throw new InvalidArgumentException('The format must be either a constant value or name in libphonenumber\PhoneNumberType');
+ }
+
+ $type = constant('\libphonenumber\PhoneNumberType::' . $type);
+ }
+
+ return $this->phoneNumberUtil->getNumberType($phoneNumber) === $type ? true : false;
+ }
+}
diff --git a/Resources/config/services.xml b/Resources/config/services.xml
index f66c7ffb..bfecf1b0 100644
--- a/Resources/config/services.xml
+++ b/Resources/config/services.xml
@@ -30,6 +30,11 @@
+
+
+
+
+
diff --git a/Resources/config/templating.xml b/Resources/config/templating.xml
index 9c7cedae..3166bd9e 100644
--- a/Resources/config/templating.xml
+++ b/Resources/config/templating.xml
@@ -16,7 +16,7 @@
-
+
diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml
index 0a2ab6ac..f0619098 100644
--- a/Resources/config/twig.xml
+++ b/Resources/config/twig.xml
@@ -14,7 +14,7 @@
-
+
diff --git a/Templating/Helper/PhoneNumberHelper.php b/Templating/Helper/PhoneNumberHelper.php
index bba7d2fd..5e30b8b7 100644
--- a/Templating/Helper/PhoneNumberHelper.php
+++ b/Templating/Helper/PhoneNumberHelper.php
@@ -15,7 +15,7 @@
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
-use Misd\PhoneNumberBundle\Exception\InvalidArgumentException;
+use Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter;
use Symfony\Component\Templating\Helper\HelperInterface;
/**
@@ -24,11 +24,11 @@
class PhoneNumberHelper implements HelperInterface
{
/**
- * Phone number utility.
+ * Phone number formatter.
*
- * @var PhoneNumberUtil
+ * @var PhoneNumberFormatter
*/
- protected $phoneNumberUtil;
+ protected $formatter;
/**
* Charset.
@@ -40,11 +40,15 @@ class PhoneNumberHelper implements HelperInterface
/**
* Constructor.
*
- * @param PhoneNumberUtil $phoneNumberUtil Phone number utility.
+ * @param PhoneNumberFormatter $formatter Phone number formatter.
*/
- public function __construct(PhoneNumberUtil $phoneNumberUtil)
+ public function __construct($formatter)
{
- $this->phoneNumberUtil = $phoneNumberUtil;
+ if ($formatter instanceof PhoneNumberUtil) {
+ // throw deprecation message
+ $formatter = new PhoneNumberFormatter($formatter);
+ }
+ $this->formatter = $formatter;
}
/**
@@ -78,22 +82,10 @@ public function getName()
* @param int|string $format Format, or format constant name.
*
* @return string Formatted phone number.
- *
- * @throws InvalidArgumentException If an argument is invalid.
*/
public function format(PhoneNumber $phoneNumber, $format = PhoneNumberFormat::INTERNATIONAL)
{
- if (true === is_string($format)) {
- $constant = '\libphonenumber\PhoneNumberFormat::' . $format;
-
- if (false === defined($constant)) {
- throw new InvalidArgumentException('The format must be either a constant value or name in libphonenumber\PhoneNumberFormat');
- }
-
- $format = constant('\libphonenumber\PhoneNumberFormat::' . $format);
- }
-
- return $this->phoneNumberUtil->format($phoneNumber, $format);
+ return $this->formatter->format($phoneNumber, $format);
}
/**
@@ -101,21 +93,9 @@ public function format(PhoneNumber $phoneNumber, $format = PhoneNumberFormat::IN
* @param int|string $type PhoneNumberType, or PhoneNumberType constant name.
*
* @return bool
- *
- * @throws InvalidArgumentException If type argument is invalid.
*/
public function isType(PhoneNumber $phoneNumber, $type = PhoneNumberType::UNKNOWN)
{
- if (true === is_string($type)) {
- $constant = '\libphonenumber\PhoneNumberType::' . $type;
-
- if (false === defined($constant)) {
- throw new InvalidArgumentException('The format must be either a constant value or name in libphonenumber\PhoneNumberType');
- }
-
- $type = constant('\libphonenumber\PhoneNumberType::' . $type);
- }
-
- return $this->phoneNumberUtil->getNumberType($phoneNumber) === $type ? true : false;
+ return $this->formatter->getNumberType($phoneNumber) === $type ? true : false;
}
}
diff --git a/Tests/DependencyInjection/MisdPhoneNumberExtensionTest.php b/Tests/DependencyInjection/MisdPhoneNumberExtensionTest.php
index c0398c86..c5e2a4ba 100644
--- a/Tests/DependencyInjection/MisdPhoneNumberExtensionTest.php
+++ b/Tests/DependencyInjection/MisdPhoneNumberExtensionTest.php
@@ -142,6 +142,15 @@ public function testLoad()
'method' => 'serializePhoneNumber',
)
);
+
+ $this->assertHasService(
+ 'misd_phone_number.formatter',
+ 'Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter'
+ );
+ $this->assertHasService(
+ 'Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter',
+ 'Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter'
+ );
}
protected function assertHasService($id, $instanceOf)
diff --git a/Tests/Formatter/PhoneNumberFormatterTest.php b/Tests/Formatter/PhoneNumberFormatterTest.php
new file mode 100644
index 00000000..e8320aed
--- /dev/null
+++ b/Tests/Formatter/PhoneNumberFormatterTest.php
@@ -0,0 +1,110 @@
+phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
+ ->disableOriginalConstructor()->getMock();
+
+ $this->formatter = new PhoneNumberFormatter($this->phoneNumberUtil);
+ }
+
+ /**
+ * @dataProvider formatProvider
+ */
+ public function testFormat($format, $expectedFormat)
+ {
+ $phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
+
+ $this->phoneNumberUtil->expects($this->once())->method('format')->with($phoneNumber, $expectedFormat);
+
+ $this->formatter->format($phoneNumber, $format);
+ }
+
+ /**
+ * 0 => Format
+ * 1 => Expected format
+ */
+ public function formatProvider()
+ {
+ return array(
+ array(PhoneNumberFormat::NATIONAL, PhoneNumberFormat::NATIONAL),
+ array('NATIONAL', PhoneNumberFormat::NATIONAL),
+ );
+ }
+
+ /**
+ * @expectedException \Misd\PhoneNumberBundle\Exception\InvalidArgumentException
+ */
+ public function testFormatInvalidArgumentException()
+ {
+ $phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
+
+ $this->formatter->format($phoneNumber, 'foo');
+ }
+
+ /**
+ * @dataProvider isTypeProvider
+ */
+ public function testIsType($type, $isNationalFormat)
+ {
+ $phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
+
+ $this->phoneNumberUtil->expects($this->once())->method('getNumberType')->with($phoneNumber)->willReturn(PhoneNumberFormat::NATIONAL);
+
+ $this->assertSame($isNationalFormat, $this->formatter->isType($phoneNumber, $type));
+ }
+
+ /**
+ * 0 => Format
+ * 1 => Expected format
+ */
+ public function isTypeProvider()
+ {
+ return array(
+ array(PhoneNumberFormat::NATIONAL, true),
+ array(PhoneNumberFormat::INTERNATIONAL, false),
+ );
+ }
+
+ /**
+ * @expectedException \Misd\PhoneNumberBundle\Exception\InvalidArgumentException
+ */
+ public function testIsTypeInvalidArgumentException()
+ {
+ $phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
+
+ $this->formatter->isType($phoneNumber, 'foo');
+ }
+}
diff --git a/Tests/Templating/Helper/PhoneNumberHelperTest.php b/Tests/Templating/Helper/PhoneNumberHelperTest.php
index 3dab84b1..35383e79 100644
--- a/Tests/Templating/Helper/PhoneNumberHelperTest.php
+++ b/Tests/Templating/Helper/PhoneNumberHelperTest.php
@@ -21,7 +21,7 @@
*/
class PhoneNumberHelperTest extends TestCase
{
- public function testConstructor()
+ public function testDeprecatedConstructor()
{
$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
->disableOriginalConstructor()->getMock();
@@ -31,6 +31,16 @@ public function testConstructor()
$this->assertInstanceOf('Symfony\Component\Templating\Helper\HelperInterface', $helper);
}
+ public function testConstructor()
+ {
+ $phoneNumberFormatter = $this->getMockBuilder('Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter')
+ ->disableOriginalConstructor()->getMock();
+
+ $helper = new PhoneNumberHelper($phoneNumberFormatter);
+
+ $this->assertInstanceOf('Symfony\Component\Templating\Helper\HelperInterface', $helper);
+ }
+
public function testCharset()
{
$phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
@@ -53,47 +63,43 @@ public function testName()
$this->assertTrue(is_string($helper->getName()));
}
- /**
- * @dataProvider processProvider
- */
- public function testProcess($format, $expectedFormat)
+ public function testFormat()
{
$phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
- $phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
+ $phoneNumberFormatter = $this->getMockBuilder('Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter')
->disableOriginalConstructor()->getMock();
- $phoneNumberUtil->expects($this->once())->method('format')->with($phoneNumber, $expectedFormat);
-
- $helper = new PhoneNumberHelper($phoneNumberUtil);
+ $phoneNumberFormatter->expects($this->once())->method('format')->with($phoneNumber, PhoneNumberFormat::NATIONAL);
- $helper->format($phoneNumber, $format);
- }
+ $helper = new PhoneNumberHelper($phoneNumberFormatter);
- /**
- * 0 => Format
- * 1 => Expected format
- */
- public function processProvider()
- {
- return array(
- array(PhoneNumberFormat::NATIONAL, PhoneNumberFormat::NATIONAL),
- array('NATIONAL', PhoneNumberFormat::NATIONAL),
- );
+ $helper->format($phoneNumber, PhoneNumberFormat::NATIONAL);
}
/**
- * @expectedException \Misd\PhoneNumberBundle\Exception\InvalidArgumentException
+ * @dataProvider isTypeProvider
*/
- public function testProcessInvalidArgumentException()
+ public function testIsType($type, $isNationalType)
{
$phoneNumber = $this->getMock('libphonenumber\PhoneNumber');
- $phoneNumberUtil = $this->getMockBuilder('libphonenumber\PhoneNumberUtil')
+ $phoneNumberFormatter = $this->getMockBuilder('Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter')
->disableOriginalConstructor()->getMock();
+ $phoneNumberFormatter->expects($this->once())->method('getNumberType')->with($phoneNumber)->willReturn(PhoneNumberFormat::NATIONAL);
- $helper = new PhoneNumberHelper($phoneNumberUtil);
+ $helper = new PhoneNumberHelper($phoneNumberFormatter);
+
+ $result = $helper->isType($phoneNumber, $type);
+
+ $this->assertSame($isNationalType, $result);
+ }
- $helper->format($phoneNumber, 'foo');
+ public function isTypeProvider()
+ {
+ return array(
+ array(PhoneNumberFormat::INTERNATIONAL, false),
+ array(PhoneNumberFormat::NATIONAL, true),
+ );
}
public function testDeprecatedClassName() {
diff --git a/Tests/Twig/Extension/PhoneNumberHelperExtensionTest.php b/Tests/Twig/Extension/PhoneNumberHelperExtensionTest.php
index a6e0daf2..90857f71 100644
--- a/Tests/Twig/Extension/PhoneNumberHelperExtensionTest.php
+++ b/Tests/Twig/Extension/PhoneNumberHelperExtensionTest.php
@@ -21,9 +21,9 @@
class PhoneNumberHelperExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter
*/
- private $helper;
+ private $formatter;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Misd\PhoneNumberBundle\Twig\Extension\PhoneNumberHelperExtension
@@ -32,11 +32,22 @@ class PhoneNumberHelperExtensionTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
- $this->helper = $this->getMockBuilder('Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper')
+ $this->formatter = $this->getMockBuilder('Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter')
->disableOriginalConstructor()
->getMock();
- $this->extension = new PhoneNumberHelperExtension($this->helper);
+ $this->extension = new PhoneNumberHelperExtension($this->formatter);
+ }
+
+ public function testDeprecatedConstructor()
+ {
+ $helper = $this->getMockBuilder('Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $extension = new PhoneNumberHelperExtension($helper);
+
+ $this->assertInstanceOf('Twig_Extension', $extension);
}
public function testConstructor()
@@ -54,7 +65,7 @@ public function testGetFunctions()
$callable = $functions[0]->getCallable();
- $this->assertSame($this->helper, $callable[0]);
+ $this->assertSame($this->formatter, $callable[0]);
$this->assertSame('format', $callable[1]);
$this->assertInstanceOf('Twig_SimpleFunction', $functions[1]);
@@ -62,7 +73,7 @@ public function testGetFunctions()
$callable = $functions[1]->getCallable();
- $this->assertSame($this->helper, $callable[0]);
+ $this->assertSame($this->formatter, $callable[0]);
$this->assertSame('isType', $callable[1]);
}
@@ -76,7 +87,7 @@ public function testGetFilters()
$callable = $filters[0]->getCallable();
- $this->assertSame($this->helper, $callable[0]);
+ $this->assertSame($this->formatter, $callable[0]);
$this->assertSame('format', $callable[1]);
}
@@ -90,7 +101,7 @@ public function testGetTests()
$callable = $tests[0]->getCallable();
- $this->assertSame($this->helper, $callable[0]);
+ $this->assertSame($this->formatter, $callable[0]);
$this->assertSame('isType', $callable[1]);
}
diff --git a/Twig/Extension/PhoneNumberHelperExtension.php b/Twig/Extension/PhoneNumberHelperExtension.php
index fdccc101..a018e5f3 100644
--- a/Twig/Extension/PhoneNumberHelperExtension.php
+++ b/Twig/Extension/PhoneNumberHelperExtension.php
@@ -11,6 +11,7 @@
namespace Misd\PhoneNumberBundle\Twig\Extension;
+use Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter;
use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper;
/**
@@ -19,20 +20,23 @@
class PhoneNumberHelperExtension extends \Twig_Extension
{
/**
- * Phone number helper.
+ * Phone number formatter.
*
- * @var PhoneNumberHelper
+ * @var PhoneNumberFormatter
*/
- protected $helper;
+ protected $formatter;
/**
* Constructor.
*
- * @param PhoneNumberHelper $helper Phone number helper.
+ * @param PhoneNumberFormatter $formatter Phone number formatter.
*/
- public function __construct(PhoneNumberHelper $helper)
+ public function __construct($formatter)
{
- $this->helper = $helper;
+ if ($formatter instanceof PhoneNumberHelper) {
+ // throw deprecation message
+ }
+ $this->formatter = $formatter;
}
/**
@@ -41,8 +45,8 @@ public function __construct(PhoneNumberHelper $helper)
public function getFunctions()
{
return array(
- new \Twig_SimpleFunction('phone_number_format', array($this->helper, 'format'), array('deprecated' => '1.2')),
- new \Twig_SimpleFunction('phone_number_is_type', array($this->helper, 'isType'), array('deprecated' => '1.2')),
+ new \Twig_SimpleFunction('phone_number_format', array($this->formatter, 'format'), array('deprecated' => '1.2')),
+ new \Twig_SimpleFunction('phone_number_is_type', array($this->formatter, 'isType'), array('deprecated' => '1.2')),
);
}
@@ -52,7 +56,7 @@ public function getFunctions()
public function getFilters()
{
return array(
- new \Twig_SimpleFilter('phone_number_format', array($this->helper, 'format')),
+ new \Twig_SimpleFilter('phone_number_format', array($this->formatter, 'format')),
);
}
@@ -62,7 +66,7 @@ public function getFilters()
public function getTests()
{
return array(
- new \Twig_SimpleTest('phone_number_of_type', array($this->helper, 'isType')),
+ new \Twig_SimpleTest('phone_number_of_type', array($this->formatter, 'isType')),
);
}