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/Twig/Extension/PhoneNumberHelperExtension.php b/Twig/Extension/PhoneNumberHelperExtension.php index fdccc101..bc933b35 100644 --- a/Twig/Extension/PhoneNumberHelperExtension.php +++ b/Twig/Extension/PhoneNumberHelperExtension.php @@ -11,7 +11,7 @@ namespace Misd\PhoneNumberBundle\Twig\Extension; -use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper; +use Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter; /** * Phone number helper Twig extension. @@ -19,20 +19,20 @@ 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; + $this->formatter = $formatter; } /** @@ -41,8 +41,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 +52,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 +62,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')), ); }