Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom object serialization #60

Merged
merged 2 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/BigDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,40 @@ public function __toString() : string
return \substr($value, 0, -$this->scale) . '.' . \substr($value, -$this->scale);
}

/**
* This method is required for serializing the object and SHOULD NOT be accessed directly.
*
* @internal
*
* @return array{value: string, scale: int}
*/
public function __serialize(): array
{
return ['value' => $this->value, 'scale' => $this->scale];
}

/**
* This method is only here to allow unserializing the object and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param array{value: string, scale: int} $data
*
* @return void
*
* @throws \LogicException
*/
public function __unserialize(array $data): void
{
if (isset($this->value)) {
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
}

$this->value = $data['value'];
$this->scale = $data['scale'];
}

/**
* This method is required by interface Serializable and SHOULD NOT be accessed directly.
*
Expand Down
33 changes: 33 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,39 @@ public function __toString() : string
return $this->value;
}

/**
* This method is required for serializing the object and SHOULD NOT be accessed directly.
*
* @internal
*
* @return array{value: string}
*/
public function __serialize(): array
{
return ['value' => $this->value];
}

/**
* This method is only here to allow unserializing the object and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param array{value: string} $data
*
* @return void
*
* @throws \LogicException
*/
public function __unserialize(array $data): void
{
if (isset($this->value)) {
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
}

$this->value = $data['value'];
}

/**
* This method is required by interface Serializable and SHOULD NOT be accessed directly.
*
Expand Down
34 changes: 34 additions & 0 deletions src/BigRational.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,40 @@ public function __toString() : string
return $this->numerator . '/' . $this->denominator;
}

/**
* This method is required for serializing the object and SHOULD NOT be accessed directly.
*
* @internal
*
* @return array{numerator: \Brick\Math\BigInteger, denominator: \Brick\Math\BigInteger}
*/
public function __serialize(): array
{
return ['numerator' => $this->numerator, 'denominator' => $this->denominator];
}

/**
* This method is only here to allow unserializing the object and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param array{numerator: \Brick\Math\BigInteger, denominator: \Brick\Math\BigInteger} $data
*
* @return void
*
* @throws \LogicException
*/
public function __unserialize(array $data): void
{
if (isset($this->numerator)) {
throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
}

$this->numerator = $data['numerator'];
$this->denominator = $data['denominator'];
}

/**
* This method is required by interface Serializable and SHOULD NOT be accessed directly.
*
Expand Down