Skip to content

Commit

Permalink
[BUGFIX] Implement __serialize and __unserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschaufi committed Apr 24, 2024
1 parent a890161 commit f7e1e61
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Classes/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@

namespace SimonSchaufi\TYPO3Phone;

use Exception;
use JsonSerializable;
use libphonenumber\NumberParseException as libNumberParseException;
use libphonenumber\PhoneNumber as libPhoneNumber;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use Serializable;
use SimonSchaufi\TYPO3Phone\Exceptions\CountryCodeException;
use SimonSchaufi\TYPO3Phone\Exceptions\NumberFormatException;
use SimonSchaufi\TYPO3Phone\Exceptions\NumberParseException;
use SimonSchaufi\TYPO3Phone\Traits\ParsesCountries;
use SimonSchaufi\TYPO3Phone\Traits\ParsesFormats;
use SimonSchaufi\TYPO3Phone\Traits\ParsesTypes;

class PhoneNumber implements JsonSerializable, Serializable
/**
* @see https://github.com/Propaganistas/Laravel-Phone/blob/master/src/PhoneNumber.php
*/
class PhoneNumber implements \JsonSerializable, \Serializable
{
use ParsesCountries;
use ParsesFormats;
Expand Down Expand Up @@ -417,6 +417,16 @@ public function unserialize($serialized)
$this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance());
}

public function __serialize()
{
return ['number' => $this->formatE164()];
}

public function __unserialize(array $data)
{
$this->number = $data['number'];
}

/**
* Convert the phone instance to a formatted number.
*
Expand All @@ -428,7 +438,7 @@ public function __toString()
// Let's just return the original number in that case.
try {
return $this->formatE164();
} catch (Exception $exception) {
} catch (\Exception $exception) {
return (string)$this->number;
}
}
Expand Down

0 comments on commit f7e1e61

Please sign in to comment.