Skip to content

Commit

Permalink
Add support for custom object serialization (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
TRowbotham authored Aug 14, 2021
1 parent 8da1625 commit 6dac159
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
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

0 comments on commit 6dac159

Please sign in to comment.