-
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
7 changed files
with
123 additions
and
49 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