-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Twig extension should not depend on templating helper
- Loading branch information
Showing
11 changed files
with
296 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Oops, something went wrong.