diff --git a/src/Config/Listener.php b/src/Config/Listener.php index e0206e1..d12453b 100644 --- a/src/Config/Listener.php +++ b/src/Config/Listener.php @@ -39,6 +39,10 @@ public function __construct( $this->parsePass($pass); } + /** + * @param $pass + * @return void + */ private function parsePass($pass): void { if (is_string($pass)) { diff --git a/src/Config/Listener/Forwarded.php b/src/Config/Listener/Forwarded.php index 7a463bf..e9f898d 100644 --- a/src/Config/Listener/Forwarded.php +++ b/src/Config/Listener/Forwarded.php @@ -23,7 +23,7 @@ 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. @@ -31,7 +31,7 @@ class Forwarded implements Arrayable * * @var string|null */ - private ?string $protocol; + private ?string $protocol = null; /** * Controls how the client_ip fields are traversed diff --git a/src/Config/Route.php b/src/Config/Route.php index 3252fcb..3e5d110 100644 --- a/src/Config/Route.php +++ b/src/Config/Route.php @@ -3,10 +3,11 @@ 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; /** @@ -14,7 +15,7 @@ * * @implements RouteInterface */ -class Route implements RouteInterface, Arrayable, Jsonable +class Route implements RouteInterface, Uploadable { use HasListeners; @@ -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); @@ -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); } /** @@ -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(); } } diff --git a/src/Config/Routes/RouteAction.php b/src/Config/Routes/RouteAction.php index 4d3ff9c..7e96b98 100644 --- a/src/Config/Routes/RouteAction.php +++ b/src/Config/Routes/RouteAction.php @@ -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()); diff --git a/src/Config/Routes/RouteBlock.php b/src/Config/Routes/RouteBlock.php index 8db44db..7c796a6 100644 --- a/src/Config/Routes/RouteBlock.php +++ b/src/Config/Routes/RouteBlock.php @@ -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; } } diff --git a/src/Contracts/RouteInterface.php b/src/Contracts/RouteInterface.php index a447c7c..32780a7 100644 --- a/src/Contracts/RouteInterface.php +++ b/src/Contracts/RouteInterface.php @@ -4,7 +4,7 @@ use UnitPhpSdk\Config\Listener; -interface RouteInterface +interface RouteInterface extends Arrayable, Jsonable { /** * Get route name