Skip to content

Commit 2d755f2

Browse files
committed
feat(routes): Add apiVersion property in RouteMap
1 parent 079a039 commit 2d755f2

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
}
2222
},
2323
"require": {
24-
"php": ">=7.0",
24+
"php": ">=7.4",
2525
"ext-json": "*",
2626
"crell/api-problem": "^2.0",
2727
"akrabat/rka-content-type-renderer": "^0.7.3",

src/RouteMap/RouteMapAbstract.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,29 @@ abstract class RouteMapAbstract extends DataIterator implements RouteMapInterfac
2828
{
2929

3030
use ContainerTrait;
31+
32+
protected int $apiVersion = 1;
33+
3134
/**
3235
* @var string|ActionInterface
3336
*/
3437
protected $actionClass;
3538
/**
36-
* @var string
39+
* @var string|null
3740
*/
38-
protected $packageName;
41+
protected ?string $packageName = null;
3942
/**
4043
* @var string|EntityRequestInterface
4144
*/
42-
protected $requestClass;
45+
protected string $requestClass;
4346
/**
4447
* @var string
4548
*/
46-
protected $resourceName;
49+
protected string $resourceName;
4750
/**
4851
* @var string
4952
*/
50-
protected $routesPrefix;
53+
protected string $routesPrefix = "";
5154

5255
/**
5356
* RouteMapAbstract constructor.
@@ -60,6 +63,24 @@ final public function __construct(ContainerInterface $container)
6063
$this->initialize();
6164
}
6265

66+
/**
67+
* @return int
68+
*/
69+
public function getApiVersion(): int
70+
{
71+
return $this->apiVersion;
72+
}
73+
74+
/**
75+
* @param int $apiVersion
76+
* @return RouteMapAbstract
77+
*/
78+
public function setApiVersion(int $apiVersion): RouteMapAbstract
79+
{
80+
$this->apiVersion = $apiVersion;
81+
return $this;
82+
}
83+
6384
/**
6485
* Routes
6586
*
@@ -100,6 +121,11 @@ public function add($method, $pattern): RouteInterface
100121
# Add prefix before resource
101122
array_unshift($prefixes, $this->routesPrefix);
102123
}
124+
if ($this->getApiVersion() > 1) {
125+
# Add version before resource
126+
array_unshift($prefixes, 'v' . $this->getApiVersion());
127+
}
128+
103129
$pattern = $this->trailingSlash($pattern);
104130
$pattern = implode('/', $prefixes) . $pattern;
105131
$pattern = '/' . ltrim($pattern, '/');
@@ -238,7 +264,7 @@ private function trailingSlash($routeName)
238264
{
239265
$routeName = rtrim($routeName, ']');
240266
$routeName = rtrim($routeName, '[/');
241-
$missing = substr_count($routeName, '[') - substr_count($routeName, ']');
267+
$missing = substr_count($routeName, '[') - substr_count($routeName, ']');
242268
$routeName .= '[/]' . str_repeat(']', $missing);
243269

244270
return $routeName;

src/RouteMap/RouteMapInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function get($pattern): RouteInterface;
5151
*/
5252
public function getActionClass(): string;
5353

54+
public function getApiVersion(): int;
55+
5456
/**
5557
* @return ContainerInterface
5658
*/
@@ -115,6 +117,8 @@ public function put($pattern): RouteInterface;
115117
*/
116118
public function registerRoutes(RouterInterface $router);
117119

120+
public function setApiVersion(int $apiVersion): self;
121+
118122
/**
119123
* @param ContainerInterface $c
120124
*

0 commit comments

Comments
 (0)