2024-09-03 12:46:53 -04:00
|
|
|
<?php
|
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
namespace Socialbox\Objects;
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
use Socialbox\Enums\StandardError;
|
|
|
|
use Socialbox\Exceptions\RpcException;
|
|
|
|
use Socialbox\Interfaces\SerializableInterface;
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
class RpcError implements SerializableInterface
|
2024-09-03 12:46:53 -04:00
|
|
|
{
|
2024-12-19 15:09:22 -05:00
|
|
|
private string $id;
|
|
|
|
private StandardError $code;
|
|
|
|
private string $error;
|
2024-09-27 14:20:56 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
/**
|
|
|
|
* Constructs the RPC error object.
|
|
|
|
*
|
|
|
|
* @param string $id The ID of the RPC request
|
|
|
|
* @param StandardError|int $code The error code
|
|
|
|
* @param string|null $error The error message
|
|
|
|
*/
|
|
|
|
public function __construct(string $id, StandardError|int $code, ?string $error)
|
2024-09-27 14:20:56 -04:00
|
|
|
{
|
2024-12-19 15:09:22 -05:00
|
|
|
$this->id = $id;
|
2024-09-27 14:20:56 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
if(is_int($code))
|
|
|
|
{
|
|
|
|
$code = StandardError::tryFrom($code);
|
|
|
|
if($code === null)
|
|
|
|
{
|
|
|
|
$code = StandardError::UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
$this->code = $code;
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
if($error === null)
|
|
|
|
{
|
|
|
|
$this->error = $code->getMessage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->error = $error;
|
|
|
|
}
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
}
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
/**
|
|
|
|
* Returns the ID of the RPC request.
|
|
|
|
*
|
|
|
|
* @return string The ID of the RPC request.
|
|
|
|
*/
|
|
|
|
public function getId(): string
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2024-09-03 12:46:53 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
/**
|
|
|
|
* Returns the error code.
|
|
|
|
*
|
|
|
|
* @return StandardError The error code.
|
|
|
|
*/
|
|
|
|
public function getCode(): StandardError
|
|
|
|
{
|
|
|
|
return $this->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the error message.
|
|
|
|
*
|
|
|
|
* @return string The error message.
|
|
|
|
*/
|
|
|
|
public function getError(): string
|
|
|
|
{
|
|
|
|
return $this->error;
|
|
|
|
}
|
2024-09-13 13:52:38 -04:00
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
/**
|
|
|
|
* Returns an array representation of the object.
|
|
|
|
*
|
|
|
|
* @return array The array representation of the object.
|
|
|
|
*/
|
|
|
|
public function toArray(): array
|
2024-09-13 13:52:38 -04:00
|
|
|
{
|
2024-12-19 15:09:22 -05:00
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'code' => $this->code->value,
|
|
|
|
'error' => $this->error
|
|
|
|
];
|
2024-09-13 13:52:38 -04:00
|
|
|
}
|
|
|
|
|
2024-12-19 15:09:22 -05:00
|
|
|
/**
|
|
|
|
* Converts the current object to an RpcException instance.
|
|
|
|
*
|
|
|
|
* @return RpcException The RpcException generated from the current object.
|
|
|
|
*/
|
|
|
|
public function toRpcException(): RpcException
|
|
|
|
{
|
|
|
|
return new RpcException($this->error, $this->code->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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['code'], $data['error']);
|
|
|
|
}
|
|
|
|
}
|