Skip to content

Commit

Permalink
Update Route class interfaces and enhance JSON handling
Browse files Browse the repository at this point in the history
In this commit, the Route class was updated to implement the Uploadable interface instead of Arrayable and Jsonable. The upload() and remove() methods were added. Also, enhancements were made to handle the JSON data in the RouteAction and RouteBlock classes. Default values were added in the Forwarded class, and the doc block in the Listener class was updated.
  • Loading branch information
Pavlusha311245 committed Apr 23, 2024
1 parent ed730bd commit 6ba5c42
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/Config/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function __construct(
$this->parsePass($pass);
}

/**
* @param $pass
* @return void
*/
private function parsePass($pass): void
{
if (is_string($pass)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Listener/Forwarded.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class Forwarded implements Arrayable
*
* @var string|null
*/
private ?string $client_ip;
private ?string $client_ip = null;

/**
* Defines the relevant HTTP header field to look for in the request.
* Unit expects it to follow the X-Forwarded-Proto notation, with the field value itself being http, https, or on.
*
* @var string|null
*/
private ?string $protocol;
private ?string $protocol = null;

/**
* Controls how the client_ip fields are traversed
Expand Down
41 changes: 34 additions & 7 deletions src/Config/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
namespace UnitPhpSdk\Config;

use UnitPhpSdk\Config\Routes\RouteBlock;
use UnitPhpSdk\Contracts\Arrayable;
use UnitPhpSdk\Contracts\Jsonable;
use UnitPhpSdk\Contracts\RouteInterface;
use UnitPhpSdk\Contracts\Uploadable;
use UnitPhpSdk\Enums\HttpMethodsEnum;
use UnitPhpSdk\Exceptions\UnitException;
use UnitPhpSdk\Http\UnitRequest;
use UnitPhpSdk\Traits\HasListeners;

/**
* This class presents "routes" section from config
*
* @implements RouteInterface
*/
class Route implements RouteInterface, Arrayable, Jsonable
class Route implements RouteInterface, Uploadable
{
use HasListeners;

Expand All @@ -33,9 +34,10 @@ class Route implements RouteInterface, Arrayable, Jsonable
*/
public function __construct(
private readonly string $name,
$data = [],
$data = [],
bool $single = false
) {
)
{
if (!empty($data)) {
if ($single) {
$this->routeBlocks[] = new RouteBlock($data);
Expand Down Expand Up @@ -76,7 +78,7 @@ public function getRouteBlocks(): array
*/
#[\Override] public function toArray(): array
{
return array_map(fn (RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
return array_map(fn(RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
}

/**
Expand All @@ -85,6 +87,31 @@ public function getRouteBlocks(): array
*/
#[\Override] public function toJson(int $options = 0): string
{
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)));
return json_encode(array_filter($this->toArray(), fn($item) => !empty($item)));
}

/**
* @inheritDoc
*/
#[\Override] public function upload(UnitRequest $request)
{
$request->setMethod(HttpMethodsEnum::PUT->value)->send(
$this->getApiEndpoint(),
true,
['json' => array_filter($this->toArray(), fn($item) => !empty($item))]
);
}

/**
* @inheritDoc
*/
#[\Override] public function remove(UnitRequest $request)
{
$request->setMethod(HttpMethodsEnum::DELETE->value)->send($this->getApiEndpoint());
}

private function getApiEndpoint()
{
return '/config/routes/' . $this->getName();
}
}
13 changes: 9 additions & 4 deletions src/Config/Routes/RouteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,15 @@ public function parseFromArray(array $data): void
*/
#[\Override] public function toArray(): array
{
$data = [
'response_headers' => $this->getResponseHeaders(),
'rewrite' => $this->getRewrite(),
];
$data = [];

if (!empty($this->getResponseHeaders())) {
$data['response_headers'] = $this->getResponseHeaders();
}

if (!empty($this->getRewrite())) {
$data['rewrite'] = $this->getRewrite();
}

if ($this->getPass() !== null) {
return array_merge($data, $this->getPass()->toArray());
Expand Down
9 changes: 7 additions & 2 deletions src/Config/Routes/RouteBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ public function getActionType(): RouteActionTypeEnum

#[Override] public function toArray(): array
{
return [
$data = [
'action' => $this->getAction()->toArray(),
'match' => $this->getMatch()?->toArray(),
];

if ($this->hasMatch()) {
$data['match'] = $this->getMatch()?->toArray();
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Contracts/RouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use UnitPhpSdk\Config\Listener;

interface RouteInterface
interface RouteInterface extends Arrayable, Jsonable
{
/**
* Get route name
Expand Down

0 comments on commit 6ba5c42

Please sign in to comment.