Sets the raw input of this phone number.
public setRawInput ( string $value ) : |
||
$value | string | The raw input. |
return | This PhoneNumber instance, for chaining method calls. |
public function testEqualWithCountryCodeSourceSet() { $numberA = new PhoneNumber(); $numberA->setRawInput("+1 650 253 00 00")->setCountryCode(CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN); $numberB = new PhoneNumber(); $numberB->setRawInput("+1 650 253 00 00")->setCountryCode(CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN); $this->assertEquals($numberA, $numberB); }
public function testSerializingPhoneNumber2() { $phoneNumber = new PhoneNumber(); $phoneNumber->setCountryCode(1); $phoneNumber->setNationalNumber(1); $phoneNumber->setExtension(1); $phoneNumber->setItalianLeadingZero(1); $phoneNumber->setNumberOfLeadingZeros(1); $phoneNumber->setRawInput(1); $phoneNumber->setCountryCodeSource(1); $phoneNumber->setPreferredDomesticCarrierCode(1); $serializedString = serialize($phoneNumber); $phoneObject2 = unserialize($serializedString); $this->assertTrue($phoneObject2->equals($phoneNumber)); }
public function testParseAndKeepRaw() { $alphaNumericNumber = new PhoneNumber(); $alphaNumericNumber->mergeFrom(self::$alphaNumericNumber); $alphaNumericNumber->setRawInput("800 six-flags"); $alphaNumericNumber->setCountryCodeSource(CountryCodeSource::FROM_DEFAULT_COUNTRY); $alphaNumericNumber->setPreferredDomesticCarrierCode(""); $this->assertEquals($alphaNumericNumber, $this->phoneUtil->parseAndKeepRawInput("800 six-flags", RegionCode::US)); $shorterAlphaNumber = new PhoneNumber(); $shorterAlphaNumber->setCountryCode(1)->setNationalNumber(8007493524); $shorterAlphaNumber->setRawInput("1800 six-flag")->setCountryCodeSource(CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN)->setPreferredDomesticCarrierCode(""); $this->assertEquals($shorterAlphaNumber, $this->phoneUtil->parseAndKeepRawInput("1800 six-flag", RegionCode::US)); $shorterAlphaNumber->setRawInput("+1800 six-flag")->setCountryCodeSource(CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN); $this->assertEquals($shorterAlphaNumber, $this->phoneUtil->parseAndKeepRawInput("+1800 six-flag", RegionCode::NZ)); $shorterAlphaNumber->setRawInput("001800 six-flag")->setCountryCodeSource(CountryCodeSource::FROM_NUMBER_WITH_IDD); $this->assertEquals($shorterAlphaNumber, $this->phoneUtil->parseAndKeepRawInput("001800 six-flag", RegionCode::NZ)); // Invalid region code supplied. try { $this->phoneUtil->parseAndKeepRawInput("123 456 7890", RegionCode::CS); $this->fail("Deprecated region code not allowed: should fail."); } catch (NumberParseException $e) { // Expected this exception. $this->assertEquals(NumberParseException::INVALID_COUNTRY_CODE, $e->getErrorType(), "Wrong error type stored in exception."); } $koreanNumber = new PhoneNumber(); $koreanNumber->setCountryCode(82)->setNationalNumber(22123456)->setRawInput("08122123456")->setCountryCodeSource(CountryCodeSource::FROM_DEFAULT_COUNTRY)->setPreferredDomesticCarrierCode("81"); $this->assertEquals($koreanNumber, $this->phoneUtil->parseAndKeepRawInput("08122123456", RegionCode::KR)); }
/** * Parses a string and fills up the phoneNumber. This method is the same as the public * parse() method, with the exception that it allows the default region to be null, for use by * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region * to be null or unknown ("ZZ"). * @param $numberToParse * @param string $defaultRegion * @param $keepRawInput * @param $checkRegion * @param PhoneNumber $phoneNumber * @throws NumberParseException */ private function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $checkRegion, PhoneNumber $phoneNumber) { $numberToParse = trim($numberToParse); if ($numberToParse === null) { throw new NumberParseException(NumberParseException::NOT_A_NUMBER, "The phone number supplied was null."); } elseif (mb_strlen($numberToParse) > self::MAX_INPUT_STRING_LENGTH) { throw new NumberParseException(NumberParseException::TOO_LONG, "The string supplied was too long to parse."); } $nationalNumber = ''; $this->buildNationalNumberForParsing($numberToParse, $nationalNumber); if (!$this->isViablePhoneNumber($nationalNumber)) { throw new NumberParseException(NumberParseException::NOT_A_NUMBER, "The string supplied did not seem to be a phone number."); } // Check the region supplied is valid, or that the extracted number starts with some sort of + // sign so the number's region can be determined. if ($checkRegion && !$this->checkRegionForParsing($nationalNumber, $defaultRegion)) { throw new NumberParseException(NumberParseException::INVALID_COUNTRY_CODE, "Missing or invalid default region."); } if ($keepRawInput) { $phoneNumber->setRawInput($numberToParse); } // Attempt to parse extension first, since it doesn't require region-specific data and we want // to have the non-normalised number here. $extension = $this->maybeStripExtension($nationalNumber); if (mb_strlen($extension) > 0) { $phoneNumber->setExtension($extension); } $regionMetadata = $this->getMetadataForRegion($defaultRegion); // Check to see if the number is given in international format so we know whether this number is // from the default region or not. $normalizedNationalNumber = ""; $countryCode = 0; try { // TODO: This method should really just take in the string buffer that has already // been created, and just remove the prefix, rather than taking in a string and then // outputting a string buffer. $countryCode = $this->maybeExtractCountryCode($nationalNumber, $regionMetadata, $normalizedNationalNumber, $keepRawInput, $phoneNumber); } catch (NumberParseException $e) { $matcher = new Matcher(self::$PLUS_CHARS_PATTERN, $nationalNumber); if ($e->getErrorType() == NumberParseException::INVALID_COUNTRY_CODE && $matcher->lookingAt()) { // Strip the plus-char, and try again. $countryCode = $this->maybeExtractCountryCode(substr($nationalNumber, $matcher->end()), $regionMetadata, $normalizedNationalNumber, $keepRawInput, $phoneNumber); if ($countryCode == 0) { throw new NumberParseException(NumberParseException::INVALID_COUNTRY_CODE, "Could not interpret numbers after plus-sign."); } } else { throw new NumberParseException($e->getErrorType(), $e->getMessage(), $e); } } if ($countryCode !== 0) { $phoneNumberRegion = $this->getRegionCodeForCountryCode($countryCode); if ($phoneNumberRegion != $defaultRegion) { // Metadata cannot be null because the country calling code is valid. $regionMetadata = $this->getMetadataForRegionOrCallingCode($countryCode, $phoneNumberRegion); } } else { // If no extracted country calling code, use the region supplied instead. The national number // is just the normalized version of the number we were given to parse. $normalizedNationalNumber .= $this->normalize($nationalNumber); if ($defaultRegion !== null) { $countryCode = $regionMetadata->getCountryCode(); $phoneNumber->setCountryCode($countryCode); } else { if ($keepRawInput) { $phoneNumber->clearCountryCodeSource(); } } } if (mb_strlen($normalizedNationalNumber) < self::MIN_LENGTH_FOR_NSN) { throw new NumberParseException(NumberParseException::TOO_SHORT_NSN, "The string supplied is too short to be a phone number."); } if ($regionMetadata !== null) { $carrierCode = ""; $potentialNationalNumber = $normalizedNationalNumber; $this->maybeStripNationalPrefixAndCarrierCode($potentialNationalNumber, $regionMetadata, $carrierCode); // We require that the NSN remaining after stripping the national prefix and carrier code be // of a possible length for the region. Otherwise, we don't do the stripping, since the // original number could be a valid short number. if (!$this->isShorterThanPossibleNormalNumber($regionMetadata, $potentialNationalNumber)) { $normalizedNationalNumber = $potentialNationalNumber; if ($keepRawInput) { $phoneNumber->setPreferredDomesticCarrierCode($carrierCode); } } } $lengthOfNationalNumber = mb_strlen($normalizedNationalNumber); if ($lengthOfNationalNumber < self::MIN_LENGTH_FOR_NSN) { throw new NumberParseException(NumberParseException::TOO_SHORT_NSN, "The string supplied is too short to be a phone number."); } if ($lengthOfNationalNumber > self::MAX_LENGTH_FOR_NSN) { throw new NumberParseException(NumberParseException::TOO_LONG, "The string supplied is too long to be a phone number."); } $this->setItalianLeadingZerosForPhoneNumber($normalizedNationalNumber, $phoneNumber); $phoneNumber->setNationalNumber((double) $normalizedNationalNumber); }