Skip to content

Commit

Permalink
Twig extension should not depend on templating helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ossinkine committed Oct 4, 2018
1 parent 6503084 commit fe98b84
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 49 deletions.
6 changes: 3 additions & 3 deletions DependencyInjection/MisdPhoneNumberExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
89 changes: 89 additions & 0 deletions Formatter/PhoneNumberFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\Formatter;

use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use Misd\PhoneNumberBundle\Exception\InvalidArgumentException;

/**
* Phone number formatter.
*/
class PhoneNumberFormatter
{
/**
* Phone number utility.
*
* @var PhoneNumberUtil
*/
protected $phoneNumberUtil;

/**
* Constructor.
*
* @param PhoneNumberUtil $phoneNumberUtil Phone number utility.
*/
public function __construct(PhoneNumberUtil $phoneNumberUtil)
{
$this->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;
}
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<service id="libphonenumber.phone_number_to_time_zones_mapper" class="libphonenumber\PhoneNumberToTimeZonesMapper"/>
<service id="libphonenumber\PhoneNumberToTimeZonesMapper" alias="libphonenumber.phone_number_to_time_zones_mapper"/>

<service id="misd_phone_number.formatter" class="Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter">
<argument type="service" id="libphonenumber.phone_number_util"/>
</service>
<service id="Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter" alias="misd_phone_number.formatter"/>

</services>

</container>
2 changes: 1 addition & 1 deletion Resources/config/templating.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<tag name="templating.helper" alias="phone_number_helper"/>
<!-- phone_number_format is deprecated and will be removed in 2.0 (use phone_number_helper instead) -->
<tag name="templating.helper" alias="phone_number_format"/>
<argument type="service" id="libphonenumber.phone_number_util"/>
<argument type="service" id="misd_phone_number.formatter"/>
</service>
<service id="Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper" alias="misd_phone_number.templating.helper"/>
</services>
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<service id="misd_phone_number.twig.extension.format" class="Misd\PhoneNumberBundle\Twig\Extension\PhoneNumberHelperExtension"
public="false">
<tag name="twig.extension"/>
<argument type="service" id="misd_phone_number.templating.helper"/>
<argument type="service" id="misd_phone_number.formatter"/>
</service>
<service id="Misd\PhoneNumberBundle\Twig\Extension\PhoneNumberHelperExtension" alias="misd_phone_number.twig.extension.format"/>
</services>
Expand Down
46 changes: 13 additions & 33 deletions Templating/Helper/PhoneNumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -24,11 +24,11 @@
class PhoneNumberHelper implements HelperInterface
{
/**
* Phone number utility.
* Phone number formatter.
*
* @var PhoneNumberUtil
* @var PhoneNumberFormatter
*/
protected $phoneNumberUtil;
protected $formatter;

/**
* Charset.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -78,44 +82,20 @@ 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);
}

/**
* @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;
return $this->formatter->getNumberType($phoneNumber) === $type ? true : false;
}
}
22 changes: 11 additions & 11 deletions Twig/Extension/PhoneNumberHelperExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@

namespace Misd\PhoneNumberBundle\Twig\Extension;

use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper;
use Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter;

/**
* Phone number helper Twig extension.
*/
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;
}

/**
Expand All @@ -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')),
);
}

Expand All @@ -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')),
);
}

Expand All @@ -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')),
);
}

Expand Down

0 comments on commit fe98b84

Please sign in to comment.