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 c61be06
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 82 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;
}
}
9 changes: 9 additions & 0 deletions Tests/DependencyInjection/MisdPhoneNumberExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
110 changes: 110 additions & 0 deletions Tests/Formatter/PhoneNumberFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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\Formatter;

use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter;
use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberFormatHelper;
use Misd\PhoneNumberBundle\Templating\Helper\PhoneNumberHelper;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Phone number formatter test.
*/
class PhoneNumberFormatterTest extends TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\libphonenumber\PhoneNumberUtil
*/
private $phoneNumberUtil;

/**
* @var \Misd\PhoneNumberBundle\Formatter\PhoneNumberFormatter
*/
private $formatter;

protected function setUp()
{
$this->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');
}
}
Loading

0 comments on commit c61be06

Please sign in to comment.