Skip to content

Commit cf4f1d6

Browse files
Update RouteAction class and ActionType sub-classes
This revision makes significant updates to the RouteAction class and introduces ActionType sub-classes. It changes the setState method to work with these new classes and updates the toArray method accordingly. Additionally, this commit improved the route block test to include ActionType instances instead of raw data and restructured RouteActionTest to use these new ActionType classes. Most property values in the RouteAction class are now instances of ActionType subclasses, such as PassAction, ProxyAction, ShareAction, and ReturnAction.
1 parent 2a032d5 commit cf4f1d6

File tree

11 files changed

+644
-370
lines changed

11 files changed

+644
-370
lines changed

src/Config/Route.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
use UnitPhpSdk\Config\Routes\RouteBlock;
66
use UnitPhpSdk\Contracts\Arrayable;
7+
use UnitPhpSdk\Contracts\Jsonable;
78
use UnitPhpSdk\Contracts\RouteInterface;
9+
use UnitPhpSdk\Exceptions\UnitException;
810
use UnitPhpSdk\Traits\HasListeners;
911

1012
/**
1113
* This class presents "routes" section from config
1214
*
1315
* @implements RouteInterface
1416
*/
15-
class Route implements RouteInterface, Arrayable
17+
class Route implements RouteInterface, Arrayable, Jsonable
1618
{
1719
use HasListeners;
1820

@@ -26,6 +28,9 @@ class Route implements RouteInterface, Arrayable
2628
*/
2729
private array $listeners = [];
2830

31+
/**
32+
* @throws UnitException
33+
*/
2934
public function __construct(
3035
private readonly string $name,
3136
$data = [],
@@ -71,13 +76,14 @@ public function getRouteBlocks(): array
7176
*/
7277
#[\Override] public function toArray(): array
7378
{
74-
return $this->getRouteBlocks();
79+
return array_map(fn (RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
7580
}
7681

7782
/**
78-
* @return string|false
83+
* @param int $options
84+
* @return string
7985
*/
80-
public function toJson(): string|false
86+
#[\Override] public function toJson(int $options = 0): string
8187
{
8288
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)));
8389
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Config\Routes\ActionType;
4+
5+
use UnitPhpSdk\Contracts\Arrayable;
6+
7+
class PassAction implements Arrayable
8+
{
9+
public function __construct(
10+
private string $pass
11+
)
12+
{
13+
$this->setPass($pass);
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getPass(): string
20+
{
21+
return $this->pass;
22+
}
23+
24+
/**
25+
* @param string $pass
26+
*/
27+
public function setPass(string $pass): void
28+
{
29+
$this->pass = $pass;
30+
}
31+
32+
public function toArray(): array
33+
{
34+
return [
35+
'pass' => $this->getPass()
36+
];
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Config\Routes\ActionType;
4+
5+
use UnitPhpSdk\Contracts\Arrayable;
6+
7+
class ProxyAction implements Arrayable
8+
{
9+
public function __construct(
10+
private string $proxy
11+
)
12+
{
13+
$this->setProxy($proxy);
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getProxy(): string
20+
{
21+
return $this->proxy;
22+
}
23+
24+
/**
25+
* @param string $proxy
26+
*/
27+
public function setProxy(string $proxy): void
28+
{
29+
$this->proxy = $proxy;
30+
}
31+
32+
33+
public function toArray(): array
34+
{
35+
return [
36+
'proxy' => $this->getProxy()
37+
];
38+
}
39+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Config\Routes\ActionType;
4+
5+
use OutOfRangeException;
6+
use UnitPhpSdk\Contracts\Arrayable;
7+
8+
class ReturnAction implements Arrayable
9+
{
10+
public function __construct(
11+
12+
/**
13+
* HTTP status code with a context-dependent redirect location.
14+
* Integer (000–999); defines the HTTP response status code to be returned.
15+
*/
16+
private int $return,
17+
18+
/**
19+
* String URI; used if the return value implies redirection.
20+
*
21+
* @var string
22+
*/
23+
private string $location = ''
24+
) {
25+
if ($return > 999 || $return < 0) {
26+
throw new OutOfRangeException('Return must be between 0 and 999');
27+
}
28+
29+
if (!empty($location)) {
30+
$this->setLocation($location);
31+
}
32+
}
33+
34+
/**
35+
* @param int $return
36+
*/
37+
public function setReturn(int $return): void
38+
{
39+
$this->return = $return;
40+
}
41+
42+
/**
43+
* @param string $location
44+
*/
45+
public function setLocation(string $location): void
46+
{
47+
$this->location = $location;
48+
}
49+
50+
/**
51+
* @return int
52+
*/
53+
public function getReturn(): int
54+
{
55+
return $this->return;
56+
}
57+
58+
59+
/**
60+
* @return string
61+
*/
62+
public function getLocation(): string
63+
{
64+
return $this->location;
65+
}
66+
67+
/**
68+
* Converts the object to an array.
69+
*
70+
* @return array The converted array representation of the object.
71+
*/
72+
public function toArray(): array
73+
{
74+
return [
75+
'return' => $this->return,
76+
'location' => $this->location,
77+
];
78+
}
79+
}

0 commit comments

Comments
 (0)