public function testFormatByPattern()
 {
     $newNumFormat = new NumberFormat();
     $newNumFormat->setPattern("(\\d{3})(\\d{3})(\\d{4})");
     $newNumFormat->setFormat("(\$1) \$2-\$3");
     $newNumberFormats = array();
     $newNumberFormats[] = $newNumFormat;
     $this->assertEquals("(650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+1 (650) 253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $this->assertEquals("tel:+1-650-253-0000", $this->phoneUtil->formatByPattern(self::$usNumber, PhoneNumberFormat::RFC3966, $newNumberFormats));
     // $NP is set to '1' for the US. Here we check that for other NANPA countries the US rules are
     // followed.
     $newNumFormat->setNationalPrefixFormattingRule('$NP ($FG)');
     $newNumFormat->setFormat("\$1 \$2-\$3");
     $this->assertEquals("1 (242) 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+1 242 365-1234", $this->phoneUtil->formatByPattern(self::$bsNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $newNumFormat->setPattern("(\\d{2})(\\d{5})(\\d{3})");
     $newNumFormat->setFormat("\$1-\$2 \$3");
     $newNumberFormats[0] = $newNumFormat;
     $this->assertEquals("02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+39 02-36618 300", $this->phoneUtil->formatByPattern(self::$itNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule('$NP$FG');
     $newNumFormat->setPattern("(\\d{2})(\\d{4})(\\d{4})");
     $newNumFormat->setFormat("\$1 \$2 \$3");
     $newNumberFormats[0] = $newNumFormat;
     $this->assertEquals("020 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule('($NP$FG)');
     $this->assertEquals("(020) 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $newNumFormat->setNationalPrefixFormattingRule("");
     $this->assertEquals("20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::NATIONAL, $newNumberFormats));
     $this->assertEquals("+44 20 7031 3000", $this->phoneUtil->formatByPattern(self::$gbNumber, PhoneNumberFormat::INTERNATIONAL, $newNumberFormats));
 }
 public static function setLeadingDigitsPatterns(\DOMElement $numberFormatElement, NumberFormat $format)
 {
     $leadingDigitsPatternNodes = $numberFormatElement->getElementsByTagName(self::LEADING_DIGITS);
     $numOfLeadingDigitsPatterns = $leadingDigitsPatternNodes->length;
     if ($numOfLeadingDigitsPatterns > 0) {
         for ($i = 0; $i < $numOfLeadingDigitsPatterns; $i++) {
             $elt = $leadingDigitsPatternNodes->item($i);
             $format->addLeadingDigitsPattern($elt->firstChild->nodeValue, true);
         }
     }
 }
 /**
  * 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;
 }
 /**
  * Formats a phone number in the specified format using client-defined formatting rules. Note that
  * if the phone number has a country calling code of zero or an otherwise invalid country calling
  * code, we cannot work out things like whether there should be a national prefix applied, or how
  * to format extensions, so we return the national significant number with no formatting applied.
  *
  * @param PhoneNumber $number                        the phone number to be formatted
  * @param int $numberFormat                  the format the phone number should be formatted into
  * @param array $userDefinedFormats            formatting rules specified by clients
  * @return String the formatted phone number
  */
 public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats)
 {
     $countryCallingCode = $number->getCountryCode();
     $nationalSignificantNumber = $this->getNationalSignificantNumber($number);
     if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
         return $nationalSignificantNumber;
     }
     // Note getRegionCodeForCountryCode() is used because formatting information for regions which
     // share a country calling code is contained by only one region for performance reasons. For
     // example, for NANPA regions it will be contained in the metadata for US.
     $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode);
     // Metadata cannot be null because the country calling code is valid
     $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode);
     $formattedNumber = "";
     $formattingPattern = $this->chooseFormattingPatternForNumber($userDefinedFormats, $nationalSignificantNumber);
     if ($formattingPattern === null) {
         // If no pattern above is matched, we format the number as a whole.
         $formattedNumber .= $nationalSignificantNumber;
     } else {
         $numFormatCopy = new NumberFormat();
         // Before we do a replacement of the national prefix pattern $NP with the national prefix, we
         // need to copy the rule so that subsequent replacements for different numbers have the
         // appropriate national prefix.
         $numFormatCopy->mergeFrom($formattingPattern);
         $nationalPrefixFormattingRule = $formattingPattern->getNationalPrefixFormattingRule();
         if (mb_strlen($nationalPrefixFormattingRule) > 0) {
             $nationalPrefix = $metadata->getNationalPrefix();
             if (mb_strlen($nationalPrefix) > 0) {
                 // Replace $NP with national prefix and $FG with the first group ($1).
                 $npPatternMatcher = new Matcher(self::NP_PATTERN, $nationalPrefixFormattingRule);
                 $nationalPrefixFormattingRule = $npPatternMatcher->replaceFirst($nationalPrefix);
                 $fgPatternMatcher = new Matcher(self::FG_PATTERN, $nationalPrefixFormattingRule);
                 $nationalPrefixFormattingRule = $fgPatternMatcher->replaceFirst("\\\$1");
                 $numFormatCopy->setNationalPrefixFormattingRule($nationalPrefixFormattingRule);
             } else {
                 // We don't want to have a rule for how to format the national prefix if there isn't one.
                 $numFormatCopy->clearNationalPrefixFormattingRule();
             }
         }
         $formattedNumber .= $this->formatNsnUsingPattern($nationalSignificantNumber, $numFormatCopy, $numberFormat);
     }
     $this->maybeAppendFormattedExtension($number, $metadata, $numberFormat, $formattedNumber);
     $this->prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, $formattedNumber);
     return $formattedNumber;
 }