/** * 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; }
/** * @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()); } $this->setPossibleLength($other->getPossibleLength()); $this->setPossibleLengthLocalOnly($other->getPossibleLengthLocalOnly()); return $this; }
/** * 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); }