propertyguru / e164-phone-number-formatter-bundle
Propertygurus's phone number formatter
Requires
- php: >=5.4
Requires (Dev)
- mockery/mockery: dev-master@dev
- phpunit/phpunit: 4.6.*
- squizlabs/php_codesniffer: >=2.3.0
- symfony/symfony: ~2.6,>=2.6.6
This package is not auto-updated.
Last update: 2024-06-22 17:13:35 UTC
README
E164-phone-number-formatter-bundle
Symfony2 bundle to format phone numbers in E164 format.
Installation
Add the repository to your composer.json file
// composer.json { "require": { // ... "propertyguru/E164-phone-number-formatter-bundle" : "dev-master" } }
Register the bundle in your AppKernel:
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Guru\PhoneNumberFormatterBundle\GuruPhoneNumberFormatterBundle(), // ... ); }
Usage
Setup
$formatter = $this->container->get('guru_phone_number_formatter.formatter');
// optional: set current country - if known
$formatter->setDefaultRegionCode('my');
If you know the country code already
$e164 = $formatter->numberToE164('0101234567', '60');
If the country code if embedded in the number
$e164 = $formatter->numberToE164('+60101234567');
If you are not sure if the country code is embedded or not
$e164 = $formatter->numberToE164('+60101234567', '60');
All of the above will output
array(
'countryCode' => '60',
'nationalDestinationCode' => '010',
'nationalDestinationCodeInternational' => '10',
'subscriberNumber' => '1234567',
'isMobile' => true,
)
Embedded country code has precedence over specified country code
$e164 = $formatter->numberToE164('+65101234567', '60');
and will output:
array(
'countryCode' => '65',
'nationalDestinationCode' => NULL,
'nationalDestinationCodeInternational' => NULL,
'subscriberNumber' => '101234567',
'isMobile' => false,
)
Weighted results
Weights can be defined that some country codes are preffered over others. If a number can match more than one country, then only the one with the highest weight is considered
$formatter->setRegionWeights(array(
'my' => 1,
'sg' => 2
));
Example : 98123123 can be either a malaysia or singaporean phone number.