Skip to content

Commit 6ba5c42

Browse files
Update Route class interfaces and enhance JSON handling
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.
1 parent ed730bd commit 6ba5c42

File tree

6 files changed

+57
-16
lines changed

6 files changed

+57
-16
lines changed

src/Config/Listener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function __construct(
3939
$this->parsePass($pass);
4040
}
4141

42+
/**
43+
* @param $pass
44+
* @return void
45+
*/
4246
private function parsePass($pass): void
4347
{
4448
if (is_string($pass)) {

src/Config/Listener/Forwarded.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class Forwarded implements Arrayable
2323
*
2424
* @var string|null
2525
*/
26-
private ?string $client_ip;
26+
private ?string $client_ip = null;
2727

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

3636
/**
3737
* Controls how the client_ip fields are traversed

src/Config/Route.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace UnitPhpSdk\Config;
44

55
use UnitPhpSdk\Config\Routes\RouteBlock;
6-
use UnitPhpSdk\Contracts\Arrayable;
7-
use UnitPhpSdk\Contracts\Jsonable;
86
use UnitPhpSdk\Contracts\RouteInterface;
7+
use UnitPhpSdk\Contracts\Uploadable;
8+
use UnitPhpSdk\Enums\HttpMethodsEnum;
99
use UnitPhpSdk\Exceptions\UnitException;
10+
use UnitPhpSdk\Http\UnitRequest;
1011
use UnitPhpSdk\Traits\HasListeners;
1112

1213
/**
1314
* This class presents "routes" section from config
1415
*
1516
* @implements RouteInterface
1617
*/
17-
class Route implements RouteInterface, Arrayable, Jsonable
18+
class Route implements RouteInterface, Uploadable
1819
{
1920
use HasListeners;
2021

@@ -33,9 +34,10 @@ class Route implements RouteInterface, Arrayable, Jsonable
3334
*/
3435
public function __construct(
3536
private readonly string $name,
36-
$data = [],
37+
$data = [],
3738
bool $single = false
38-
) {
39+
)
40+
{
3941
if (!empty($data)) {
4042
if ($single) {
4143
$this->routeBlocks[] = new RouteBlock($data);
@@ -76,7 +78,7 @@ public function getRouteBlocks(): array
7678
*/
7779
#[\Override] public function toArray(): array
7880
{
79-
return array_map(fn (RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
81+
return array_map(fn(RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
8082
}
8183

8284
/**
@@ -85,6 +87,31 @@ public function getRouteBlocks(): array
8587
*/
8688
#[\Override] public function toJson(int $options = 0): string
8789
{
88-
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)));
90+
return json_encode(array_filter($this->toArray(), fn($item) => !empty($item)));
91+
}
92+
93+
/**
94+
* @inheritDoc
95+
*/
96+
#[\Override] public function upload(UnitRequest $request)
97+
{
98+
$request->setMethod(HttpMethodsEnum::PUT->value)->send(
99+
$this->getApiEndpoint(),
100+
true,
101+
['json' => array_filter($this->toArray(), fn($item) => !empty($item))]
102+
);
103+
}
104+
105+
/**
106+
* @inheritDoc
107+
*/
108+
#[\Override] public function remove(UnitRequest $request)
109+
{
110+
$request->setMethod(HttpMethodsEnum::DELETE->value)->send($this->getApiEndpoint());
111+
}
112+
113+
private function getApiEndpoint()
114+
{
115+
return '/config/routes/' . $this->getName();
89116
}
90117
}

src/Config/Routes/RouteAction.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,15 @@ public function parseFromArray(array $data): void
246246
*/
247247
#[\Override] public function toArray(): array
248248
{
249-
$data = [
250-
'response_headers' => $this->getResponseHeaders(),
251-
'rewrite' => $this->getRewrite(),
252-
];
249+
$data = [];
250+
251+
if (!empty($this->getResponseHeaders())) {
252+
$data['response_headers'] = $this->getResponseHeaders();
253+
}
254+
255+
if (!empty($this->getRewrite())) {
256+
$data['rewrite'] = $this->getRewrite();
257+
}
253258

254259
if ($this->getPass() !== null) {
255260
return array_merge($data, $this->getPass()->toArray());

src/Config/Routes/RouteBlock.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ public function getActionType(): RouteActionTypeEnum
9797

9898
#[Override] public function toArray(): array
9999
{
100-
return [
100+
$data = [
101101
'action' => $this->getAction()->toArray(),
102-
'match' => $this->getMatch()?->toArray(),
103102
];
103+
104+
if ($this->hasMatch()) {
105+
$data['match'] = $this->getMatch()?->toArray();
106+
}
107+
108+
return $data;
104109
}
105110
}

src/Contracts/RouteInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use UnitPhpSdk\Config\Listener;
66

7-
interface RouteInterface
7+
interface RouteInterface extends Arrayable, Jsonable
88
{
99
/**
1010
* Get route name

0 commit comments

Comments
 (0)