From 22c33f9b8463cf0ce8940280c8fdff74f583192c Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 3 Sep 2024 12:46:53 -0400 Subject: [PATCH] Add RpcError class to handle RPC error representations --- src/Socialbox/Objects/RpcError.php | 81 ++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/Socialbox/Objects/RpcError.php diff --git a/src/Socialbox/Objects/RpcError.php b/src/Socialbox/Objects/RpcError.php new file mode 100644 index 0000000..6867acd --- /dev/null +++ b/src/Socialbox/Objects/RpcError.php @@ -0,0 +1,81 @@ +id = $id; + $this->error = $error; + $this->code = $code; + } + + /** + * Returns the ID of the RPC request. + * + * @return string The ID of the RPC request. + */ + public function getId(): string + { + return $this->id; + } + + /** + * Returns the error message. + * + * @return string The error message. + */ + public function getError(): string + { + return $this->error; + } + + /** + * Returns the error code. + * + * @return int The error code. + */ + public function getCode(): int + { + return $this->code; + } + + /** + * Returns an array representation of the object. + * + * @return array The array representation of the object. + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'error' => $this->error, + 'code' => $this->code + ]; + } + + /** + * Returns the RPC error object from an array of data. + * + * @param array $data The data to construct the RPC error from. + * @return RpcError The RPC error object. + */ + public static function fromArray(array $data): RpcError + { + return new RpcError($data['id'], $data['error'], $data['code']); + } +} \ No newline at end of file