Refactor RPC framework and enhance error handling.
This commit is contained in:
parent
42ba7013f7
commit
ef3b10b286
10 changed files with 383 additions and 245 deletions
|
@ -1,84 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Objects;
|
||||
namespace Socialbox\Objects;
|
||||
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
use Socialbox\Interfaces\SerializableInterface;
|
||||
|
||||
class RpcResponse implements SerializableInterface
|
||||
{
|
||||
private string $id;
|
||||
private mixed $result;
|
||||
|
||||
/**
|
||||
* Constructs the response object.
|
||||
*
|
||||
* @param string $id The ID of the response.
|
||||
* @param mixed|null $result The result of the response.
|
||||
*/
|
||||
public function __construct(string $id, mixed $result)
|
||||
class RpcResponse implements SerializableInterface
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->result = $result;
|
||||
}
|
||||
private string $id;
|
||||
private mixed $result;
|
||||
|
||||
/**
|
||||
* Returns the ID of the response.
|
||||
*
|
||||
* @return string The ID of the response.
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of the response.
|
||||
*
|
||||
* @return mixed|null The result of the response.
|
||||
*/
|
||||
public function getResult(): mixed
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given data to an array.
|
||||
*
|
||||
* @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
|
||||
* @return mixed The converted data as an array if applicable, or the original data.
|
||||
*/
|
||||
private function convertToArray(mixed $data): mixed
|
||||
{
|
||||
// If the data is an instance of SerializableInterface, call toArray on it
|
||||
if ($data instanceof SerializableInterface)
|
||||
/**
|
||||
* Constructs the response object.
|
||||
*
|
||||
* @param string $id The ID of the response.
|
||||
* @param mixed|null $result The result of the response.
|
||||
*/
|
||||
public function __construct(string $id, mixed $result)
|
||||
{
|
||||
return $data->toArray();
|
||||
$this->id = $id;
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* Returns the ID of the response.
|
||||
*
|
||||
* @return string The ID of the response.
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object.
|
||||
*
|
||||
* @return array The array representation of the object.
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'result' => $this->convertToArray($this->result)
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Returns the result of the response.
|
||||
*
|
||||
* @return mixed|null The result of the response.
|
||||
*/
|
||||
public function getResult(): mixed
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response object from an array of data.
|
||||
*
|
||||
* @param array $data The data to construct the response from.
|
||||
* @return RpcResponse The response object.
|
||||
*/
|
||||
public static function fromArray(array $data): RpcResponse
|
||||
{
|
||||
return new RpcResponse($data['id'], $data['result']);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Converts the given data to an array.
|
||||
*
|
||||
* @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
|
||||
* @return mixed The converted data as an array if applicable, or the original data.
|
||||
*/
|
||||
private function convertToArray(mixed $data): mixed
|
||||
{
|
||||
// If the data is an instance of SerializableInterface, call toArray on it
|
||||
if ($data instanceof SerializableInterface)
|
||||
{
|
||||
return $data->toArray();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the object.
|
||||
*
|
||||
* @return array The array representation of the object.
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'result' => $this->convertToArray($this->result)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response object from an array of data.
|
||||
*
|
||||
* @param array $data The data to construct the response from.
|
||||
* @return RpcResponse The response object.
|
||||
*/
|
||||
public static function fromArray(array $data): RpcResponse
|
||||
{
|
||||
return new RpcResponse($data['id'], $data['result']);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue