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;
 }
 /**
  * Helper function
  * Matches a number that begins with one of the specified prefixes
  **/
 protected function matchNumberPrefix($prefixList = array(), $number = '', $inputShortPrefixes = false)
 {
     $matchPrefixList = $inputShortPrefixes ? array_keys($prefixList) : $prefixList;
     preg_match_all('/^(' . implode('|', $matchPrefixList) . ')(.*)/', $number, $matches);
     if (!empty($matches[1]) && !empty($matches[2])) {
         $response = new PhoneNumber();
         $response->setNationalDestinationCode(!$inputShortPrefixes ? $matches[1][0] : $prefixList[$matches[1][0]]);
         $response->setNationalDestinationCodeInternational(!$inputShortPrefixes ? ltrim($matches[1][0], '0') : $matches[1][0]);
         $response->setSubscriberNumber($matches[2][0]);
         return $response;
     }
 }
 public function extractNationalDestinationCode($number = '', $countryCode = null)
 {
     // all numbers have the same length
     if (strlen($number) != $this->numberLength) {
         return;
     }
     //landlines + voip
     if (strpos($number, $this->landlinePrefix) === 0 || strpos($number, $this->voipPrefix) === 0) {
         $phoneNumber = new PhoneNumber();
         $phoneNumber->setSubscriberNumber($number);
         $phoneNumber->setIsMobile(false);
         return $phoneNumber;
     }
     //mobile
     foreach ($this->mobileRules as $rule) {
         if (preg_match('/^' . $rule . '.*$/', $number)) {
             $phoneNumber = new PhoneNumber();
             $phoneNumber->setSubscriberNumber($number);
             $phoneNumber->setIsMobile(true);
             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);
 }
 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;
         }
     }
 }