2024-09-03 12:46:53 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Socialbox\Objects;
|
|
|
|
|
2024-09-13 13:52:38 -04:00
|
|
|
use Socialbox\Enums\StandardError;
|
2024-09-03 12:46:53 -04:00
|
|
|
use Socialbox\Interfaces\SerializableInterface;
|
|
|
|
|
|
|
|
class RpcError implements SerializableInterface
|
|
|
|
{
|
|
|
|
private string $id;
|
2024-09-13 13:52:38 -04:00
|
|
|
private string $message;
|
|
|
|
private StandardError $code;
|
2024-09-03 12:46:53 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the RPC error object.
|
|
|
|
*
|
|
|
|
* @param string $id The ID of the RPC request
|
2024-09-13 13:52:38 -04:00
|
|
|
* @param StandardError $error The error code
|
|
|
|
* @param string $message The error message
|
2024-09-03 12:46:53 -04:00
|
|
|
*/
|
2024-09-13 13:52:38 -04:00
|
|
|
public function __construct(string $id, StandardError $error, string $message)
|
2024-09-03 12:46:53 -04:00
|
|
|
{
|
|
|
|
$this->id = $id;
|
2024-09-13 13:52:38 -04:00
|
|
|
$this->code = $error;
|
|
|
|
$this->message = $message;
|
2024-09-03 12:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-09-13 13:52:38 -04:00
|
|
|
public function getMessage(): string
|
2024-09-03 12:46:53 -04:00
|
|
|
{
|
2024-09-13 13:52:38 -04:00
|
|
|
return $this->message;
|
2024-09-03 12:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the error code.
|
|
|
|
*
|
2024-09-13 13:52:38 -04:00
|
|
|
* @return StandardError The error code.
|
2024-09-03 12:46:53 -04:00
|
|
|
*/
|
2024-09-13 13:52:38 -04:00
|
|
|
public function getCode(): StandardError
|
2024-09-03 12:46:53 -04:00
|
|
|
{
|
|
|
|
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,
|
2024-09-13 13:52:38 -04:00
|
|
|
'error' => $this->message,
|
|
|
|
'code' => $this->code->value
|
2024-09-03 12:46:53 -04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2024-09-13 13:52:38 -04:00
|
|
|
$errorCode = StandardError::tryFrom($data['code']);
|
|
|
|
|
|
|
|
if($errorCode == null)
|
|
|
|
{
|
|
|
|
$errorCode = StandardError::UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RpcError($data['id'], $data['error'], $errorCode);
|
2024-09-03 12:46:53 -04:00
|
|
|
}
|
|
|
|
}
|