/**
* Extracts the pattern for international format. If there is no intlFormat, default to using the
* national format. If the intlFormat is set to "NA" the intlFormat should be ignored.
*
* @param PhoneMetadata $metadata
* @param \DOMElement $numberFormatElement
* @param NumberFormat $nationalFormat
* @throws \RuntimeException if multiple intlFormats have been encountered.
* @return bool whether an international number format is defined.
*/
private static function loadInternationalFormat(PhoneMetadata $metadata, \DOMElement $numberFormatElement, NumberFormat $nationalFormat)
{
$intlFormat = new NumberFormat();
$intlFormatPattern = $numberFormatElement->getElementsByTagName(self::INTL_FORMAT);
$hasExplicitIntlFormatDefined = false;
if ($intlFormatPattern->length > 1) {
$countryId = strlen($metadata->getId()) > 0 ? $metadata->getId() : $metadata->getCountryCode();
throw new \RuntimeException("Invalid number of intlFormat patterns for country: " . $countryId);
} elseif ($intlFormatPattern->length == 0) {
// Default to use the same as the national pattern if none is defined.
$intlFormat->mergeFrom($nationalFormat);
} else {
$intlFormat->setPattern($numberFormatElement->getAttribute(self::PATTERN));
self::setLeadingDigitsPatterns($numberFormatElement, $intlFormat);
$intlFormatPatternValue = $intlFormatPattern->item(0)->firstChild->nodeValue;
if ($intlFormatPatternValue !== "NA") {
$intlFormat->setFormat($intlFormatPatternValue);
}
$hasExplicitIntlFormatDefined = true;
}
if ($intlFormat->hasFormat()) {
$metadata->addIntlNumberFormat($intlFormat);
}
return $hasExplicitIntlFormatDefined;
}