diff --git a/src/BigDecimal.php b/src/BigDecimal.php index b84f505..7824650 100644 --- a/src/BigDecimal.php +++ b/src/BigDecimal.php @@ -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. * diff --git a/src/BigInteger.php b/src/BigInteger.php index 0dcc8f3..f213fbe 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -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. * diff --git a/src/BigRational.php b/src/BigRational.php index 7fbabd7..8446ee2 100644 --- a/src/BigRational.php +++ b/src/BigRational.php @@ -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. *