Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
},
"require": {
"php": ">=7.0",
"php": ">=7.4",
"ext-json": "*",
"crell/api-problem": "^2.0",
"akrabat/rka-content-type-renderer": "^0.7.3",
Expand Down
38 changes: 32 additions & 6 deletions src/RouteMap/RouteMapAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,29 @@ abstract class RouteMapAbstract extends DataIterator implements RouteMapInterfac
{

use ContainerTrait;

protected int $apiVersion = 1;

/**
* @var string|ActionInterface
*/
protected $actionClass;
/**
* @var string
* @var string|null
*/
protected $packageName;
protected ?string $packageName = null;
/**
* @var string|EntityRequestInterface
*/
protected $requestClass;
protected string $requestClass;
/**
* @var string
*/
protected $resourceName;
protected string $resourceName;
/**
* @var string
*/
protected $routesPrefix;
protected string $routesPrefix = "";

/**
* RouteMapAbstract constructor.
Expand All @@ -60,6 +63,24 @@ final public function __construct(ContainerInterface $container)
$this->initialize();
}

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

/**
* @param int $apiVersion
* @return RouteMapAbstract
*/
public function setApiVersion(int $apiVersion): RouteMapAbstract
{
$this->apiVersion = $apiVersion;
return $this;
}

/**
* Routes
*
Expand Down Expand Up @@ -100,6 +121,11 @@ public function add($method, $pattern): RouteInterface
# Add prefix before resource
array_unshift($prefixes, $this->routesPrefix);
}
if ($this->getApiVersion() > 1) {
# Add version before resource
array_unshift($prefixes, 'v' . $this->getApiVersion());
}

$pattern = $this->trailingSlash($pattern);
$pattern = implode('/', $prefixes) . $pattern;
$pattern = '/' . ltrim($pattern, '/');
Expand Down Expand Up @@ -238,7 +264,7 @@ private function trailingSlash($routeName)
{
$routeName = rtrim($routeName, ']');
$routeName = rtrim($routeName, '[/');
$missing = substr_count($routeName, '[') - substr_count($routeName, ']');
$missing = substr_count($routeName, '[') - substr_count($routeName, ']');
$routeName .= '[/]' . str_repeat(']', $missing);

return $routeName;
Expand Down
4 changes: 4 additions & 0 deletions src/RouteMap/RouteMapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function get($pattern): RouteInterface;
*/
public function getActionClass(): string;

public function getApiVersion(): int;

/**
* @return ContainerInterface
*/
Expand Down Expand Up @@ -115,6 +117,8 @@ public function put($pattern): RouteInterface;
*/
public function registerRoutes(RouterInterface $router);

public function setApiVersion(int $apiVersion): self;

/**
* @param ContainerInterface $c
*
Expand Down