libphonenumber\PhoneNumberUtil::getExampleNumberForType PHP Method

getExampleNumberForType() public method

Gets a valid number for the specified region and number type.
public getExampleNumberForType ( string | integer $regionCodeOrType, integer $type = null ) : PhoneNumber
$regionCodeOrType string | integer the region for which an example number is needed
$type integer the PhoneNumberType of number that is needed
return PhoneNumber a valid number for the specified region and type. Returns null when the metadata does not contain such information or if an invalid region or region 001 was entered. For 001 (representing non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead. If $regionCodeOrType is the only parameter supplied, then a valid number for the specified number type will be returned that may belong to any country.
    public function getExampleNumberForType($regionCodeOrType, $type = null)
    {
        if ($regionCodeOrType !== null && $type === null) {
            /*
             * Gets a valid number for the specified number type (it may belong to any country).
             */
            foreach ($this->getSupportedRegions() as $regionCode) {
                $exampleNumber = $this->getExampleNumberForType($regionCode, $regionCodeOrType);
                if ($exampleNumber !== null) {
                    return $exampleNumber;
                }
            }
            // If there wasn't an example number for a region, try the non-geographical entities
            foreach ($this->getSupportedGlobalNetworkCallingCodes() as $countryCallingCode) {
                $desc = $this->getNumberDescByType($this->getMetadataForNonGeographicalRegion($countryCallingCode), $regionCodeOrType);
                try {
                    if ($desc->getExampleNumber() != '') {
                        return $this->parse("+" . $countryCallingCode . $desc->getExampleNumber(), static::UNKNOWN_REGION);
                    }
                } catch (NumberParseException $e) {
                }
            }
            // There are no example numbers of this type for any country in the library.
            return null;
        }
        // Check the region code is valid.
        if (!$this->isValidRegionCode($regionCodeOrType)) {
            return null;
        }
        $desc = $this->getNumberDescByType($this->getMetadataForRegion($regionCodeOrType), $type);
        try {
            if ($desc->hasExampleNumber()) {
                return $this->parse($desc->getExampleNumber(), $regionCodeOrType);
            }
        } catch (NumberParseException $e) {
        }
        return null;
    }

Usage Example

コード例 #1
0
 private function checkNumbersValidAndCorrectType($exampleNumberRequestedType, $possibleExpectedTypes, $regionCode)
 {
     $exampleNumber = $this->phoneNumberUtil->getExampleNumberForType($regionCode, $exampleNumberRequestedType);
     if ($exampleNumber !== null) {
         $this->assertTrue($this->phoneNumberUtil->isValidNumber($exampleNumber), "Failed validation for {$exampleNumber}");
         // We know the number is valid, now we check the type.
         $exampleNumberType = $this->phoneNumberUtil->getNumberType($exampleNumber);
         $this->assertContains($exampleNumberType, $possibleExpectedTypes, "Wrong type for {$exampleNumber}");
     }
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::getExampleNumberForType
PhoneNumberUtil