libphonenumber\PhoneNumberUtil::testNumberLength PHP Method

testNumberLength() protected method

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.
protected testNumberLength ( string $number, PhoneNumberDesc $phoneNumberDesc ) : integer
$number string
$phoneNumberDesc PhoneNumberDesc
return integer 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;
    }
PhoneNumberUtil