Skip to content

Commit

Permalink
Update RouteAction class and ActionType sub-classes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pavlusha311245 committed Apr 7, 2024
1 parent 2a032d5 commit cf4f1d6
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 370 deletions.
14 changes: 10 additions & 4 deletions src/Config/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

use UnitPhpSdk\Config\Routes\RouteBlock;
use UnitPhpSdk\Contracts\Arrayable;
use UnitPhpSdk\Contracts\Jsonable;
use UnitPhpSdk\Contracts\RouteInterface;
use UnitPhpSdk\Exceptions\UnitException;
use UnitPhpSdk\Traits\HasListeners;

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

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

/**
* @throws UnitException
*/
public function __construct(
private readonly string $name,
$data = [],
Expand Down Expand Up @@ -71,13 +76,14 @@ public function getRouteBlocks(): array
*/
#[\Override] public function toArray(): array
{
return $this->getRouteBlocks();
return array_map(fn (RouteBlock $routeBlock) => $routeBlock->toArray(), $this->routeBlocks);
}

/**
* @return string|false
* @param int $options
* @return string
*/
public function toJson(): string|false
#[\Override] public function toJson(int $options = 0): string
{
return json_encode(array_filter($this->toArray(), fn ($item) => !empty($item)));
}
Expand Down
38 changes: 38 additions & 0 deletions src/Config/Routes/ActionType/PassAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace UnitPhpSdk\Config\Routes\ActionType;

use UnitPhpSdk\Contracts\Arrayable;

class PassAction implements Arrayable
{
public function __construct(
private string $pass
)
{
$this->setPass($pass);
}

/**
* @return string
*/
public function getPass(): string
{
return $this->pass;
}

/**
* @param string $pass
*/
public function setPass(string $pass): void
{
$this->pass = $pass;
}

public function toArray(): array
{
return [
'pass' => $this->getPass()
];
}
}
39 changes: 39 additions & 0 deletions src/Config/Routes/ActionType/ProxyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace UnitPhpSdk\Config\Routes\ActionType;

use UnitPhpSdk\Contracts\Arrayable;

class ProxyAction implements Arrayable
{
public function __construct(
private string $proxy
)
{
$this->setProxy($proxy);
}

/**
* @return string
*/
public function getProxy(): string
{
return $this->proxy;
}

/**
* @param string $proxy
*/
public function setProxy(string $proxy): void
{
$this->proxy = $proxy;
}


public function toArray(): array
{
return [
'proxy' => $this->getProxy()
];
}
}
79 changes: 79 additions & 0 deletions src/Config/Routes/ActionType/ReturnAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace UnitPhpSdk\Config\Routes\ActionType;

use OutOfRangeException;
use UnitPhpSdk\Contracts\Arrayable;

class ReturnAction implements Arrayable
{
public function __construct(

/**
* HTTP status code with a context-dependent redirect location.
* Integer (000–999); defines the HTTP response status code to be returned.
*/
private int $return,

/**
* String URI; used if the return value implies redirection.
*
* @var string
*/
private string $location = ''
) {
if ($return > 999 || $return < 0) {
throw new OutOfRangeException('Return must be between 0 and 999');
}

if (!empty($location)) {
$this->setLocation($location);
}
}

/**
* @param int $return
*/
public function setReturn(int $return): void
{
$this->return = $return;
}

/**
* @param string $location
*/
public function setLocation(string $location): void
{
$this->location = $location;
}

/**
* @return int
*/
public function getReturn(): int
{
return $this->return;
}


/**
* @return string
*/
public function getLocation(): string
{
return $this->location;
}

/**
* Converts the object to an array.
*
* @return array The converted array representation of the object.
*/
public function toArray(): array
{
return [
'return' => $this->return,
'location' => $this->location,
];
}
}
Loading

0 comments on commit cf4f1d6

Please sign in to comment.