From 2d755f2041c7fb6c5748eb83f84a2907df546446 Mon Sep 17 00:00:00 2001 From: steve Date: Wed, 29 Sep 2021 15:31:14 +0200 Subject: [PATCH] feat(routes): Add apiVersion property in RouteMap --- composer.json | 2 +- src/RouteMap/RouteMapAbstract.php | 38 +++++++++++++++++++++++++----- src/RouteMap/RouteMapInterface.php | 4 ++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index a821068..e8f2e3b 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/RouteMap/RouteMapAbstract.php b/src/RouteMap/RouteMapAbstract.php index 1a9bb17..86f4ead 100644 --- a/src/RouteMap/RouteMapAbstract.php +++ b/src/RouteMap/RouteMapAbstract.php @@ -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. @@ -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 * @@ -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, '/'); @@ -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; diff --git a/src/RouteMap/RouteMapInterface.php b/src/RouteMap/RouteMapInterface.php index 009803d..1a4359a 100644 --- a/src/RouteMap/RouteMapInterface.php +++ b/src/RouteMap/RouteMapInterface.php @@ -51,6 +51,8 @@ public function get($pattern): RouteInterface; */ public function getActionClass(): string; + public function getApiVersion(): int; + /** * @return ContainerInterface */ @@ -115,6 +117,8 @@ public function put($pattern): RouteInterface; */ public function registerRoutes(RouterInterface $router); + public function setApiVersion(int $apiVersion): self; + /** * @param ContainerInterface $c *