/** * Returns whether the given national number (a string containing only decimal digits) matches * the national number pattern defined in the given {@code PhoneNumberDesc} message. * * @param string $nationalNumber * @param PhoneNumberDesc $numberDesc * @param boolean $allowPrefixMatch * @return boolean */ public function matchesNationalNumber($nationalNumber, PhoneNumberDesc $numberDesc, $allowPrefixMatch) { $nationalNumberPatternMatcher = new Matcher($numberDesc->getNationalNumberPattern(), $nationalNumber); return $nationalNumberPatternMatcher->matches() || $allowPrefixMatch && $nationalNumberPatternMatcher->lookingAt(); }
/** * @param string $nationalNumber * @param PhoneNumberDesc $numberDesc * @return bool */ public function isNumberPossibleForDesc($nationalNumber, PhoneNumberDesc $numberDesc) { $possibleNumberPatternMatcher = new Matcher($numberDesc->getPossibleNumberPattern(), $nationalNumber); return $possibleNumberPatternMatcher->matches(); }
public function fromArray(array $input) { if (isset($input['generalDesc'])) { $desc = new PhoneNumberDesc(); $this->setGeneralDesc($desc->fromArray($input['generalDesc'])); } if (isset($input['fixedLine'])) { $desc = new PhoneNumberDesc(); $this->setFixedLine($desc->fromArray($input['fixedLine'])); } if (isset($input['mobile'])) { $desc = new PhoneNumberDesc(); $this->setMobile($desc->fromArray($input['mobile'])); } if (isset($input['tollFree'])) { $desc = new PhoneNumberDesc(); $this->setTollFree($desc->fromArray($input['tollFree'])); } if (isset($input['premiumRate'])) { $desc = new PhoneNumberDesc(); $this->setPremiumRate($desc->fromArray($input['premiumRate'])); } if (isset($input['sharedCost'])) { $desc = new PhoneNumberDesc(); $this->setSharedCost($desc->fromArray($input['sharedCost'])); } /* hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setPersonalNumber(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setVoip(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setPager(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setUan(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setVoicemail(desc); } hasDesc = objectInput.readBoolean(); if (hasDesc) { PhoneNumberDesc desc = new PhoneNumberDesc(); desc.readExternal(objectInput); setEmergency(desc); } */ if (isset($input['noInternationalDialling'])) { $desc = new PhoneNumberDesc(); $this->setNoInternationalDialling($desc->fromArray($input['noInternationalDialling'])); } $this->setId($input['id']); $this->setCountryCode($input['countryCode']); $this->setInternationalPrefix($input['internationalPrefix']); if (isset($input['preferredInternationalPrefix'])) { $this->setPreferredInternationalPrefix($input['preferredInternationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['preferredExtnPrefix'])) { $this->setPreferredExtnPrefix($input['preferredExtnPrefix']); } if (isset($input['nationalPrefixForParsing'])) { $this->setNationalPrefixForParsing($input['nationalPrefixForParsing']); } if (isset($input['nationalPrefixTransformRule'])) { $this->setNationalPrefixTransformRule($input['nationalPrefixTransformRule']); } /* setSameMobileAndFixedLinePattern(objectInput.readBoolean()); */ foreach ($input['numberFormat'] as $numberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($numberFormatElt); $this->addNumberFormat($numberFormat); } foreach ($input['intlNumberFormat'] as $intlNumberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($intlNumberFormatElt); $this->addIntlNumberFormat($numberFormat); } /* setMainCountryForCode(objectInput.readBoolean()); * */ $this->setMainCountryForCode($input['mainCountryForCode']); if (isset($input['leadingDigits'])) { $this->setLeadingDigits($input['leadingDigits']); } $this->setLeadingZeroPossible($input['leadingZeroPossible']); return $this; }
/** * @param PhoneNumberDesc $other * @return PhoneNumberDesc */ public function mergeFrom(PhoneNumberDesc $other) { if ($other->hasNationalNumberPattern()) { $this->setNationalNumberPattern($other->getNationalNumberPattern()); } if ($other->hasPossibleNumberPattern()) { $this->setPossibleNumberPattern($other->getPossibleNumberPattern()); } if ($other->hasExampleNumber()) { $this->setExampleNumber($other->getExampleNumber()); } return $this; }
private function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) { $possibleNumberPatternMatcher = preg_match('/^(' . str_replace(array(PHP_EOL, ' '), '', $numberDesc->getPossibleNumberPattern()) . ')$/', $nationalNumber); $nationalNumberPatternMatcher = preg_match('/^(' . str_replace(array(PHP_EOL, ' '), '', $numberDesc->getNationalNumberPattern()) . ')$/', $nationalNumber); return $possibleNumberPatternMatcher && $nationalNumberPatternMatcher; }
private function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) { $possibleNumberPatternMatcher = preg_match('/^(' . $numberDesc->getPossibleNumberPattern() . ')$/x', $nationalNumber); $nationalNumberPatternMatcher = preg_match('/^' . $numberDesc->getNationalNumberPattern() . '$/x', $nationalNumber); return $possibleNumberPatternMatcher && $nationalNumberPatternMatcher; }
/** * @param array $input * @return PhoneMetadata */ public function fromArray(array $input) { if (isset($input['generalDesc'])) { $desc = new PhoneNumberDesc(); $this->setGeneralDesc($desc->fromArray($input['generalDesc'])); } if (isset($input['fixedLine'])) { $desc = new PhoneNumberDesc(); $this->setFixedLine($desc->fromArray($input['fixedLine'])); } if (isset($input['mobile'])) { $desc = new PhoneNumberDesc(); $this->setMobile($desc->fromArray($input['mobile'])); } if (isset($input['tollFree'])) { $desc = new PhoneNumberDesc(); $this->setTollFree($desc->fromArray($input['tollFree'])); } if (isset($input['premiumRate'])) { $desc = new PhoneNumberDesc(); $this->setPremiumRate($desc->fromArray($input['premiumRate'])); } if (isset($input['sharedCost'])) { $desc = new PhoneNumberDesc(); $this->setSharedCost($desc->fromArray($input['sharedCost'])); } if (isset($input['personalNumber'])) { $desc = new PhoneNumberDesc(); $this->setPersonalNumber($desc->fromArray($input['personalNumber'])); } if (isset($input['voip'])) { $desc = new PhoneNumberDesc(); $this->setVoip($desc->fromArray($input['voip'])); } if (isset($input['pager'])) { $desc = new PhoneNumberDesc(); $this->setPager($desc->fromArray($input['pager'])); } if (isset($input['uan'])) { $desc = new PhoneNumberDesc(); $this->setUan($desc->fromArray($input['uan'])); } if (isset($input['emergency'])) { $desc = new PhoneNumberDesc(); $this->setEmergency($desc->fromArray($input['emergency'])); } if (isset($input['voicemail'])) { $desc = new PhoneNumberDesc(); $this->setVoicemail($desc->fromArray($input['voicemail'])); } if (isset($input['shortCode'])) { $desc = new PhoneNumberDesc(); $this->setShortCode($desc->fromArray($input['shortCode'])); } if (isset($input['standardRate'])) { $desc = new PhoneNumberDesc(); $this->setStandardRate($desc->fromArray($input['standardRate'])); } if (isset($input['carrierSpecific'])) { $desc = new PhoneNumberDesc(); $this->setCarrierSpecific($desc->fromArray($input['carrierSpecific'])); } if (isset($input['noInternationalDialling'])) { $desc = new PhoneNumberDesc(); $this->setNoInternationalDialling($desc->fromArray($input['noInternationalDialling'])); } $this->setId($input['id']); $this->setCountryCode($input['countryCode']); $this->setInternationalPrefix($input['internationalPrefix']); if (isset($input['preferredInternationalPrefix'])) { $this->setPreferredInternationalPrefix($input['preferredInternationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['nationalPrefix'])) { $this->setNationalPrefix($input['nationalPrefix']); } if (isset($input['preferredExtnPrefix'])) { $this->setPreferredExtnPrefix($input['preferredExtnPrefix']); } if (isset($input['nationalPrefixForParsing'])) { $this->setNationalPrefixForParsing($input['nationalPrefixForParsing']); } if (isset($input['nationalPrefixTransformRule'])) { $this->setNationalPrefixTransformRule($input['nationalPrefixTransformRule']); } foreach ($input['numberFormat'] as $numberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($numberFormatElt); $this->addNumberFormat($numberFormat); } foreach ($input['intlNumberFormat'] as $intlNumberFormatElt) { $numberFormat = new NumberFormat(); $numberFormat->fromArray($intlNumberFormatElt); $this->addIntlNumberFormat($numberFormat); } $this->setMainCountryForCode($input['mainCountryForCode']); if (isset($input['leadingDigits'])) { $this->setLeadingDigits($input['leadingDigits']); } if (isset($input['leadingZeroPossible'])) { $this->setLeadingZeroPossible($input['leadingZeroPossible']); } if (isset($input['mobileNumberPortableRegion'])) { $this->setMobileNumberPortableRegion($input['mobileNumberPortableRegion']); } return $this; }
/** * Helper method to check a number against possible lengths for this number, and determine whether * it matches, or is too short or too long. Currently, if a number pattern suggests that numbers * of length 7 and 10 are possible, and a number in between these possible lengths is entered, * such as of length 8, this will return TOO_LONG. * @param string $number * @param PhoneNumberDesc $phoneNumberDesc * @return int ValidationResult */ protected function testNumberLength($number, PhoneNumberDesc $phoneNumberDesc) { $possibleLengths = $phoneNumberDesc->getPossibleLength(); $localLengths = $phoneNumberDesc->getPossibleLengthLocalOnly(); $actualLength = mb_strlen($number); if (in_array($actualLength, $localLengths)) { return ValidationResult::IS_POSSIBLE; } // There should always be "possibleLengths" set for every element. This will be a build-time // check once ShortNumberMetadata.xml is migrated to contain this information as well. $minimumLength = reset($possibleLengths); if ($minimumLength == $actualLength) { return ValidationResult::IS_POSSIBLE; } elseif ($minimumLength > $actualLength) { return ValidationResult::TOO_SHORT; } elseif (isset($possibleLengths[count($possibleLengths) - 1]) && $possibleLengths[count($possibleLengths) - 1] < $actualLength) { return ValidationResult::TOO_LONG; } // Note that actually the number is not too long if possibleLengths does not contain the length: // we know it is less than the highest possible number length, and higher than the lowest // possible number length. However, we don't currently have an enum to express this, so we // return TOO_LONG in the short-term. // We skip the first element; we've already checked it. array_shift($possibleLengths); return in_array($actualLength, $possibleLengths) ? ValidationResult::IS_POSSIBLE : ValidationResult::TOO_LONG; }
/** * Processes a phone number description element from the XML file and returns it as a * PhoneNumberDesc. If the description element is a fixed line or mobile number, the general * description will be used to fill in the whole element if necessary, or any components that are * missing. For all other types, the general description will only be used to fill in missing * components if the type has a partial definition. For example, if no "tollFree" element exists, * we assume there are no toll free numbers for that locale, and return a phone number description * with "NA" for both the national and possible number patterns. * * @param generalDesc a generic phone number description that will be used to fill in missing * parts of the description * @param countryElement the XML element representing all the country information * @param numberType the name of the number type, corresponding to the appropriate tag in the XML * file with information about that type * @return complete description of that phone number type */ private static function processPhoneNumberDescElement(PhoneNumberDesc $generalDesc, \DOMElement $countryElement, $numberType) { $phoneNumberDescList = $countryElement->getElementsByTagName($numberType); $numberDesc = new PhoneNumberDesc(); if ($phoneNumberDescList->length == 0 && !self::isValidNumberType($numberType)) { $numberDesc->setNationalNumberPattern("NA"); $numberDesc->setPossibleNumberPattern("NA"); return $numberDesc; } $numberDesc->mergeFrom($generalDesc); if ($phoneNumberDescList->length > 0) { $element = $phoneNumberDescList->item(0); $possiblePattern = $element->getElementsByTagName(self::POSSIBLE_NUMBER_PATTERN); if ($possiblePattern->length > 0) { $numberDesc->setPossibleNumberPattern($possiblePattern->item(0)->firstChild->nodeValue); } $validPattern = $element->getElementsByTagName(self::NATIONAL_NUMBER_PATTERN); if ($validPattern->length > 0) { $numberDesc->setNationalNumberPattern($validPattern->item(0)->firstChild->nodeValue); } if (!self::$liteBuild) { $exampleNumber = $element->getElementsByTagName(self::EXAMPLE_NUMBER); if ($exampleNumber->length > 0) { $numberDesc->setExampleNumber($exampleNumber->item(0)->firstChild->nodeValue); } } } return $numberDesc; }
private function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) { if (DIRECTORY_SEPARATOR == '\\') { // For Windows $possibleNumberPatternMatcher = preg_match('/^(' . str_replace(array("\n", "\r", ' '), '', $numberDesc->getPossibleNumberPattern()) . ')$/', $nationalNumber); $nationalNumberPatternMatcher = preg_match('/^(' . str_replace(array("\n", "\r", ' '), '', $numberDesc->getNationalNumberPattern()) . ')$/', $nationalNumber); } else { // For Linux $possibleNumberPatternMatcher = preg_match('/^(' . str_replace(array(PHP_EOL, ' '), '', $numberDesc->getPossibleNumberPattern()) . ')$/', $nationalNumber); $nationalNumberPatternMatcher = preg_match('/^(' . str_replace(array(PHP_EOL, ' '), '', $numberDesc->getNationalNumberPattern()) . ')$/', $nationalNumber); } return $possibleNumberPatternMatcher && $nationalNumberPatternMatcher; }
/** * TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping * this performance optimization. * @param string $number * @param PhoneNumberDesc $numberDesc * @return bool */ protected function matchesPossibleNumberAndNationalNumber($number, PhoneNumberDesc $numberDesc) { if (count($numberDesc->getPossibleLength()) > 0 && !in_array(strlen($number), $numberDesc->getPossibleLength())) { return false; } return $this->matcherAPI->matchesNationalNumber($number, $numberDesc, false); }