コード例 #1
0
 public function numberToE164($number = '', $countryCode = null)
 {
     if ($number == '') {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setCountryCode($countryCode);
         return $phoneNumber;
     }
     // extract the country code from the number if possible
     list($countryCode, $number) = $this->detectCountryCode($countryCode, $number);
     //cleanup the non-numeric chars
     $number = preg_replace('/[^0-9]+/', '', $number);
     // easier to read than 65 / 60 / etc
     $regionCode = $this->getRegionCodeFromCountryCode($countryCode);
     $regionFormatter = $this->getRegionFormatter($regionCode);
     if (!$regionFormatter) {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setCountryCode($countryCode);
         $phoneNumber->setSubscriberNumber($number);
         return $phoneNumber;
     }
     //extract area code
     $phoneNumber = $regionFormatter->extractNationalDestinationCode($number, $countryCode);
     if (!$phoneNumber) {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setSubscriberNumber($number);
     }
     $phoneNumber->setCountryCode($countryCode);
     return $phoneNumber;
 }
 /**
  * @dataProvider provideFormatByDigitCount
  **/
 public function testFormatByDigitCount($expected, $E164)
 {
     $this->initRegionFormatters($this->formatter);
     $phoneNumber = new PhoneNumber();
     $phoneNumber->setCountryCode($E164['countryCode']);
     $phoneNumber->setSubscriberNumber($E164['subscriberNumber']);
     $phoneNumber->setNationalDestinationCode($E164['nationalDestinationCode']);
     $phoneNumber->setNationalDestinationCodeInternational($E164['nationalDestinationCodeInternational']);
     $actual = $this->formatter->formatByDigitCount($phoneNumber);
     $this->assertEquals($expected, $actual);
 }
コード例 #3
0
 public function numberToE164($number = '', $countryCode = null)
 {
     if ($number == '') {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setCountryCode($countryCode);
         return $phoneNumber;
     }
     // extract the country code from the number if possible
     // get all possibilities
     $possibleCountryCodes = $this->getAllPossibleCountryCodes($countryCode, $number);
     //the original match order matters also
     //if the weights are the same
     foreach ($possibleCountryCodes as $k => $numberCode) {
         $possibleCountryCodes[$k]['order'] = $k;
     }
     $foundValidNumbers = array();
     $foundInvalidNumbers = array();
     //re-order the country codes based on the suplied weights
     uasort($possibleCountryCodes, array($this, 'comparePossibleNumbers'));
     $possibleCountryCodes = array_values($possibleCountryCodes);
     if (empty($possibleCountryCodes)) {
         //return default number
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setCountryCode($countryCode);
         $phoneNumber->setSubscriberNumber($number);
         return $phoneNumber;
     }
     foreach ($possibleCountryCodes as $k => $numberCode) {
         $countryCode = $numberCode['countryCode'];
         $subscriberNumber = $numberCode['subscriberNumber'];
         // cleanup the non-numeric chars
         $subscriberNumber = preg_replace('/[^0-9]+/', '', $subscriberNumber);
         // easier to read than 65 / 60 / etc
         $regionCode = $this->getRegionCodeFromCountryCode($countryCode);
         $regionFormatter = $this->getRegionFormatter($regionCode);
         if (!$regionFormatter) {
             // since we have no specific algorithm for this country
             // assume it's correct
             $phoneNumber = new PhoneNumber();
             $phoneNumber->setCountryCode($countryCode);
             $phoneNumber->setIsValid(true);
             $phoneNumber->setSubscriberNumber($subscriberNumber);
             $this->addListNumber($foundValidNumbers, $phoneNumber);
             continue;
         }
         //extract area code
         $phoneNumber = $regionFormatter->extractNationalDestinationCode($subscriberNumber, $countryCode);
         if (!$phoneNumber) {
             $phoneNumber = new PhoneNumber();
             $phoneNumber->setCountryCode($countryCode);
             $phoneNumber->setSubscriberNumber($subscriberNumber);
             $this->addListNumber($foundInvalidNumbers, $phoneNumber);
         } else {
             $phoneNumber->setCountryCode($countryCode);
             $phoneNumber->setIsValid(true);
             $this->addListNumber($foundValidNumbers, $phoneNumber);
         }
     }
     foreach ($foundValidNumbers as $countryCode => $numbers) {
         foreach ($numbers as $phoneNumber) {
             return $phoneNumber;
         }
     }
     foreach ($foundInvalidNumbers as $countryCode => $numbers) {
         foreach ($numbers as $phoneNumber) {
             return $phoneNumber;
         }
     }
 }