Open anboo opened 3 years ago
$phoneNumberUtil = PhoneNumberUtil::getInstance();
$phoneNumberObject = $phoneNumberUtil->parse('+79636417683');
$serializedPhoneNumber = serialize($phoneNumberObject);
echo $serializedPhoneNumber.PHP_EOL;
$phoneNumberObjectFromUnserialized = unserialize($serializedPhoneNumber);
dump($phoneNumberObjectFromUnserialized);
$this->assertEquals(
$phoneNumberUtil->getNumberType($phoneNumberObject),
$phoneNumberUtil->getNumberType($phoneNumberObjectFromUnserialized)
);
$this->assertEquals(
$phoneNumberUtil->format($phoneNumberObject, PhoneNumberFormat::E164),
$phoneNumberUtil->format($phoneNumberObjectFromUnserialized, PhoneNumberFormat::E164)
);
$this->assertEquals(
$phoneNumberUtil->canBeInternationallyDialled($phoneNumberObject),
$phoneNumberUtil->canBeInternationallyDialled($phoneNumberObjectFromUnserialized)
);
$this->assertEquals(
$phoneNumberUtil->truncateTooLongNumber($phoneNumberObject),
$phoneNumberUtil->truncateTooLongNumber($phoneNumberObjectFromUnserialized)
);
$this->assertEquals(
$phoneNumberUtil->truncateTooLongNumber($phoneNumberObject),
$phoneNumberUtil->truncateTooLongNumber($phoneNumberObjectFromUnserialized)
);
It's working ok Serialized phone number object is
C:26:"libphonenumber\PhoneNumber":76:{a:8:{i:0;i:7;i:1;s:10:"9636417683";i:2;N;i:3;N;i:4;i:1;i:5;N;i:6;i:4;i:7;N;}}
Dump after unserialize:
^ libphonenumber\PhoneNumber^ {#384
#countryCode: 7
#nationalNumber: "9636417683"
#extension: null
#italianLeadingZero: null
#rawInput: null
#countryCodeSource: 4
#preferredDomesticCarrierCode: null
#hasNumberOfLeadingZeros: false
#numberOfLeadingZeros: 1
}
Can you tell me if there could be any problems when using the serialize phone number object function?
Serialization is unit tested, so it should be absolutely fine for your needs there.
As for speed improvements, MRs are welcome, providing it doesn't deviate too much from the Java version of Google's library. Just looking through the code, there are a bunch of calls if (mb_strlen($variable) === 0)
that can be replaced with if ($variable === '')
for a minor improvement.
i replaced mb_strlen === 0
and mb_strlen > 0
by string comparison.
Maybe need add mechanism for cache of parsing? How I can cache or serialize phonenumber phone for cache?