Add new classes and methods for session management

This commit is contained in:
netkas 2024-09-13 13:52:38 -04:00
parent b9c84aeb27
commit 764ec51fa4
12 changed files with 1026 additions and 28 deletions

View file

@ -2,6 +2,11 @@
namespace Socialbox\Objects;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
class RpcRequest implements SerializableInterface
@ -13,13 +18,15 @@ class RpcRequest implements SerializableInterface
/**
* Constructs the object from an array of data.
*
* @param array $data The data to construct the object from.
* @param string $method The method of the request.
* @param string|null $id The ID of the request.
* @param array|null $parameters The parameters of the request.
*/
public function __construct(array $data)
public function __construct(string $method, ?string $id, ?array $parameters)
{
$this->id = $data['id'] ?? null;
$this->method = $data['method'];
$this->parameters = $data['parameters'] ?? null;
$this->method = $method;
$this->parameters = $parameters;
$this->id = $id;
}
/**
@ -45,13 +52,106 @@ class RpcRequest implements SerializableInterface
/**
* Returns the parameters of the request.
*
* @return array|null The parameters of the request.
* @return array|null The parameters of the request, null if the request is a notification.
*/
public function getParameters(): ?array
{
return $this->parameters;
}
/**
* Checks if the parameter exists within the RPC request
*
* @param string $parameter The parameter to check
* @return bool True if the parameter exists, False otherwise.
*/
public function containsParameter(string $parameter): bool
{
return isset($this->parameters[$parameter]);
}
/**
* Returns the parameter value from the RPC request
*
* @param string $parameter The parameter name to get
* @return mixed The parameter value, null if the parameter value is null or not found.
*/
public function getParameter(string $parameter): mixed
{
if(!$this->containsParameter($parameter))
{
return null;
}
return $this->parameters[$parameter];
}
/**
* Produces a response based off the request, null if the request is a notification
*
* @param mixed|null $result
* @return RpcResponse|null
*/
public function produceResponse(mixed $result=null): ?RpcResponse
{
if($this->id == null)
{
return null;
}
$valid = false;
if(is_array($result))
{
$valid = true;
}
elseif($result instanceof SerializableInterface)
{
$valid = true;
}
elseif(is_null($result))
{
$valid = true;
}
if(!$valid)
{
throw new InvalidArgumentException('The \'$result\' property must either be array, null or SerializableInterface');
}
return new RpcResponse($this->id, $result);
}
/**
* Produces an error response based off the request, null if the request is a notification
*
* @param StandardError $error
* @param string|null $message
* @return RpcError|null
*/
public function produceError(StandardError $error, ?string $message=null): ?RpcError
{
if($this->id == null)
{
return null;
}
if($message == null)
{
$message = $error->getMessage();
}
return new RpcError($this->id, $error, $message);
}
/**
* @param StandardException $e
* @return RpcError|null
*/
public function handleStandardException(StandardException $e): ?RpcError
{
return $this->produceError($e->getStandardError(), $e->getMessage());
}
/**
* Returns an array representation of the object.
*
@ -74,6 +174,6 @@ class RpcRequest implements SerializableInterface
*/
public static function fromArray(array $data): RpcRequest
{
return static($data);
return new RpcRequest($data['method'], $data['id'] ?? null, $data['parameters'] ?? null);
}
}