Add client metadata to session creation and enforce TOS/PP

This commit is contained in:
netkas 2024-12-12 14:55:44 -05:00
parent d2119df824
commit 756297671f
10 changed files with 414 additions and 206 deletions

View file

@ -1,193 +1,193 @@
<?php
namespace Socialbox\Objects;
namespace Socialbox\Objects;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Classes\Logger;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
use InvalidArgumentException;
use ncc\ThirdParty\nikic\PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Socialbox\Classes\Logger;
use Socialbox\Enums\StandardError;
use Socialbox\Exceptions\RpcException;
use Socialbox\Exceptions\StandardException;
use Socialbox\Interfaces\SerializableInterface;
class RpcRequest implements SerializableInterface
{
private ?string $id;
private string $method;
private ?array $parameters;
/**
* Constructs the object from an array of data.
*
* @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(string $method, ?string $id, ?array $parameters)
class RpcRequest implements SerializableInterface
{
$this->method = $method;
$this->parameters = $parameters;
$this->id = $id;
}
private ?string $id;
private string $method;
private ?array $parameters;
/**
* Returns the ID of the request.
*
* @return string|null The ID of the request.
*/
public function getId(): ?string
{
return $this->id;
}
/**
* Returns the method of the request.
*
* @return string The method of the request.
*/
public function getMethod(): string
{
return $this->method;
}
/**
* Returns 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))
/**
* Constructs the object from an array of data.
*
* @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(string $method, ?string $id, ?array $parameters)
{
return null;
$this->method = $method;
$this->parameters = $parameters;
$this->id = $id;
}
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)
/**
* Returns the ID of the request.
*
* @return string|null The ID of the request.
*/
public function getId(): ?string
{
return null;
return $this->id;
}
$valid = false;
if(is_array($result))
/**
* Returns the method of the request.
*
* @return string The method of the request.
*/
public function getMethod(): string
{
$valid = true;
}
elseif($result instanceof SerializableInterface)
{
$valid = true;
}
elseif(is_string($result))
{
$valid = true;
}
elseif(is_bool($result))
{
$valid = true;
}
elseif(is_int($result))
{
$valid = true;
}
elseif(is_null($result))
{
$valid = true;
return $this->method;
}
if(!$valid)
/**
* Returns the parameters of the request.
*
* @return array|null The parameters of the request, null if the request is a notification.
*/
public function getParameters(): ?array
{
throw new InvalidArgumentException('The \'$result\' property must either be string, boolean, integer, array, null or SerializableInterface');
return $this->parameters;
}
Logger::getLogger()->verbose(sprintf('Producing response for request %s', $this->id));
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)
/**
* 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 null;
return isset($this->parameters[$parameter]);
}
if($message == null)
/**
* 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
{
$message = $error->getMessage();
if(!$this->containsParameter($parameter))
{
return null;
}
return $this->parameters[$parameter];
}
return new RpcError($this->id, $error, $message);
}
/**
* 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;
}
/**
* @param StandardException $e
* @return RpcError|null
*/
public function handleStandardException(StandardException $e): ?RpcError
{
return $this->produceError($e->getStandardError(), $e->getMessage());
}
$valid = false;
if(is_array($result))
{
$valid = true;
}
elseif($result instanceof SerializableInterface)
{
$valid = true;
}
elseif(is_string($result))
{
$valid = true;
}
elseif(is_bool($result))
{
$valid = true;
}
elseif(is_int($result))
{
$valid = true;
}
elseif(is_null($result))
{
$valid = true;
}
/**
* Returns an array representation of the object.
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'method' => $this->method,
'parameters' => $this->parameters
];
}
if(!$valid)
{
throw new InvalidArgumentException('The \'$result\' property must either be string, boolean, integer, array, null or SerializableInterface');
}
/**
* Returns the request object from an array of data.
*
* @param array $data The data to construct the object from.
* @return RpcRequest The request object.
*/
public static function fromArray(array $data): RpcRequest
{
return new RpcRequest($data['method'], $data['id'] ?? null, $data['parameters'] ?? null);
}
}
Logger::getLogger()->verbose(sprintf('Producing response for request %s', $this->id));
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.
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'method' => $this->method,
'parameters' => $this->parameters
];
}
/**
* Returns the request object from an array of data.
*
* @param array $data The data to construct the object from.
* @return RpcRequest The request object.
*/
public static function fromArray(array $data): RpcRequest
{
return new RpcRequest($data['method'], $data['id'] ?? null, $data['parameters'] ?? null);
}
}