Esempio n. 1
0
 /**
  * Performs the actual assertion logic.
  *
  * If <code>$matcher</code> doesn't match <code>$actual</code>,
  * throws a {@link Hamcrest\AssertionError} with a description
  * of the failure along with the optional <code>$identifier</code>.
  *
  * @param string $identifier added to the message upon failure
  * @param mixed $actual value to compare against <code>$matcher</code>
  * @param \Hamcrest\Matcher $matcher applied to <code>$actual</code>
  * @throws AssertionError
  */
 private static function doAssert($identifier, $actual, Matcher $matcher)
 {
     if (!$matcher->matches($actual)) {
         $description = new StringDescription();
         if (!empty($identifier)) {
             $description->appendText($identifier . PHP_EOL);
         }
         $description->appendText('Expected: ')->appendDescriptionOf($matcher)->appendText(PHP_EOL . '     but: ');
         $matcher->describeMismatch($actual, $description);
         throw new AssertionError((string) $description);
     }
 }
Esempio n. 2
0
 /**
  * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is
  * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the
  * same as that of the region where the number is from, then NATIONAL formatting will be applied.
  *
  * <p>If the number itself has a country calling code of zero or an otherwise invalid country
  * calling code, then we return the number with no formatting applied.
  *
  * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and
  * Kazakhstan (who share the same country calling code). In those cases, no international prefix
  * is used. For regions which have multiple international prefixes, the number in its
  * INTERNATIONAL format will be returned instead.
  *
  * @param PhoneNumber $number the phone number to be formatted
  * @param string $regionCallingFrom the region where the call is being placed
  * @return string  the formatted phone number
  */
 public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom)
 {
     if (!$this->isValidRegionCode($regionCallingFrom)) {
         return $this->format($number, PhoneNumberFormat::INTERNATIONAL);
     }
     $countryCallingCode = $number->getCountryCode();
     $nationalSignificantNumber = $this->getNationalSignificantNumber($number);
     if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
         return $nationalSignificantNumber;
     }
     if ($countryCallingCode == self::NANPA_COUNTRY_CODE) {
         if ($this->isNANPACountry($regionCallingFrom)) {
             // For NANPA regions, return the national format for these regions but prefix it with the
             // country calling code.
             return $countryCallingCode . " " . $this->format($number, PhoneNumberFormat::NATIONAL);
         }
     } else {
         if ($countryCallingCode == $this->getCountryCodeForValidRegion($regionCallingFrom)) {
             // If regions share a country calling code, the country calling code need not be dialled.
             // This also applies when dialling within a region, so this if clause covers both these cases.
             // Technically this is the case for dialling from La Reunion to other overseas departments of
             // France (French Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover this
             // edge case for now and for those cases return the version including country calling code.
             // Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion
             return $this->format($number, PhoneNumberFormat::NATIONAL);
         }
     }
     // Metadata cannot be null because we checked 'isValidRegionCode()' above.
     $metadataForRegionCallingFrom = $this->getMetadataForRegion($regionCallingFrom);
     $internationalPrefix = $metadataForRegionCallingFrom->getInternationalPrefix();
     // For regions that have multiple international prefixes, the international format of the
     // number is returned, unless there is a preferred international prefix.
     $internationalPrefixForFormatting = "";
     $uniqueInternationalPrefixMatcher = new Matcher(self::UNIQUE_INTERNATIONAL_PREFIX, $internationalPrefix);
     if ($uniqueInternationalPrefixMatcher->matches()) {
         $internationalPrefixForFormatting = $internationalPrefix;
     } else {
         if ($metadataForRegionCallingFrom->hasPreferredInternationalPrefix()) {
             $internationalPrefixForFormatting = $metadataForRegionCallingFrom->getPreferredInternationalPrefix();
         }
     }
     $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode);
     // Metadata cannot be null because the country calling code is valid.
     $metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode);
     $formattedNationalNumber = $this->formatNsn($nationalSignificantNumber, $metadataForRegion, PhoneNumberFormat::INTERNATIONAL);
     $formattedNumber = $formattedNationalNumber;
     $this->maybeAppendFormattedExtension($number, $metadataForRegion, PhoneNumberFormat::INTERNATIONAL, $formattedNumber);
     if (mb_strlen($internationalPrefixForFormatting) > 0) {
         $formattedNumber = $internationalPrefixForFormatting . " " . $countryCallingCode . " " . $formattedNumber;
     } else {
         $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::INTERNATIONAL, $formattedNumber);
     }
     return $formattedNumber;
 }
 /**
  * Returns whether the given national number (a string containing only decimal digits) matches
  * the national number pattern defined in the given {@code PhoneNumberDesc} message.
  *
  * @param string $nationalNumber
  * @param PhoneNumberDesc $numberDesc
  * @param boolean $allowPrefixMatch
  * @return boolean
  */
 public function matchesNationalNumber($nationalNumber, PhoneNumberDesc $numberDesc, $allowPrefixMatch)
 {
     $nationalNumberPatternMatcher = new Matcher($numberDesc->getNationalNumberPattern(), $nationalNumber);
     return $nationalNumberPatternMatcher->matches() || $allowPrefixMatch && $nationalNumberPatternMatcher->lookingAt();
 }
 private function matchesEmergencyNumberHelper($number, $regionCode, $allowPrefixMatch)
 {
     $number = PhoneNumberUtil::extractPossibleNumber($number);
     $matcher = new Matcher(PhoneNumberUtil::$PLUS_CHARS_PATTERN, $number);
     if ($matcher->lookingAt()) {
         // Returns false if the number starts with a plus sign. WE don't believe dialling the country
         // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can
         // add additional logic here to handle it.
         return false;
     }
     $metadata = $this->getMetadataForRegion($regionCode);
     if ($metadata === null || !$metadata->hasEmergency()) {
         return false;
     }
     $emergencyNumberPattern = $metadata->getEmergency()->getNationalNumberPattern();
     $normalizedNumber = PhoneNumberUtil::normalizeDigitsOnly($number);
     $emergencyMatcher = new Matcher($emergencyNumberPattern, $normalizedNumber);
     return !$allowPrefixMatch || in_array($regionCode, self::$regionsWhereEmergencyNumbersMustBeExact) ? $emergencyMatcher->matches() : $emergencyMatcher->lookingAt();
 }
 public function chooseFormattingPatternForNumber(array $availableFormats, $nationalNumber)
 {
     foreach ($availableFormats as $numFormat) {
         /** @var NumberFormat $numFormat  */
         $size = $numFormat->leadingDigitsPatternSize();
         // We always use the last leading_digits_pattern, as it is the most detailed.
         if ($size > 0) {
             $leadingDigitsPatternMatcher = new Matcher($numFormat->getLeadingDigitsPattern($size - 1), $nationalNumber);
         }
         if ($size == 0 || $leadingDigitsPatternMatcher->lookingAt()) {
             $m = new Matcher($numFormat->getPattern(), $nationalNumber);
             if ($m->matches() > 0) {
                 return $numFormat;
             }
         }
     }
     return null;
 }
 /**
  * Returns whether the given national number (a string containing only decimal digits) matches
  * the possible number pattern defined in the given {@code PhoneNumberDesc} message.
  *
  * @param string $nationalNumber
  * @param PhoneNumberDesc $numberDesc
  * @return boolean
  */
 public function matchesPossibleNumber($nationalNumber, PhoneNumberDesc $numberDesc)
 {
     $possibleNumberPatternMatcher = new Matcher($numberDesc->getPossibleNumberPattern(), $nationalNumber);
     return $possibleNumberPatternMatcher->matches();
 }