/** * {@inheritdoc} * @see JsonSerializable::jsonSerialize() */ public function jsonSerialize() { $array = ['pathPart' => isset($this->pathPart) ? ['value' => $this->pathPart->value, 'type' => $this->pathPart->type] : null, 'table' => isset($this->table) ? $this->table->jsonSerialize() : null, 'routes' => []]; if (is_array($this->routes)) { foreach ($this->routes as $route) { $array['routes'][] = $route->getPath(); } } return $array; }
/** * Gets all matched routes * * @param string $method The HTTP method * @param string $uri Resource uri * @param callable $preprocessor optional The path preprocessor, should take the first argument - the method, the second - the path, returns array, the first element of which is the method, the second - the path * * @return array [\Scalr\Api\Rest\Routing\Route] Returns array of the matched routes */ public function getMatchedRoutes($method, $uri, callable $preprocessor = null) { if ($this->matchedRoutes === null) { $this->matchedRoutes = $this->routingTable->getMatchedRoutes(...isset($preprocessor) ? $preprocessor($method, $uri) : [$method, $uri]); } return $this->matchedRoutes; }
/** * Gets all matched routes * * @param string $method The HTTP method * @param string $uri Resource uri * @return array[\Scalr\Api\Rest\Routing\Route] Returns array of the matched routes */ public function getMatchedRoutes($method, $uri) { if ($this->matchedRoutes === null) { $this->matchedRoutes = $this->routingTable->getMatchedRoutes($method, $uri); } return $this->matchedRoutes; }
/** * @test * @depends testAppendRoute * @param Table $table */ public function testGetMatchedRoutes($table) { $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_GET, '/nothing')); //Existing routes $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v12/hotels'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); //Trailing slash should not be a trouble $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v12/hotels/'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertInstanceOf('Scalr\\Api\\Rest\\Routing\\Route', $route); $this->assertEquals(['apiversion' => 12], $route->getParams()); $res = $table->getMatchedRoutes(Request::METHOD_POST, '/api/users/v1/flights'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertInstanceOf('Scalr\\Api\\Rest\\Routing\\Route', $route); $this->assertEquals(['apiversion' => 1], $route->getParams()); //Missing method on existing route $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights')); //Not complete path $this->assertEmpty($table->getMatchedRoutes(Request::METHOD_POST, '/api/users/v1')); //Two different routes on the same path with different methods $getFlightsRoute = (new Route('/api/users/v:apiversion/flights', function () { }, ['apiversion' => '[\\d]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute); $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights'); $this->assertNotEmpty($res); $this->assertEquals(1, count($res)); $route = current($res); $this->assertSame($getFlightsRoute, $route); //Two different routes on the same path with different requirements and handlers $getFlightsRoute2 = (new Route('/api/users/v:apiversion/flights', function () { }, ['apiversion' => '[\\w]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute2); //It matches the first $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/v1/flights'); $this->assertEquals(1, count($res)); $this->assertSame($getFlightsRoute, $res[0]); //It does not matches the first but matches the second $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/version2/flights'); $this->assertEquals(1, count($res)); $this->assertSame($getFlightsRoute2, $res[0]); //Two different routes on the same path with the same requirements, same methods but with different handlers $getFlightsRoute3 = (new Route('/api/users/v:apiversion/flights', function () { /* handler 2 */ }, ['apiversion' => '[\\w]+']))->setMethods([Request::METHOD_GET]); $table->appendRoute($getFlightsRoute3); $res = $table->getMatchedRoutes(Request::METHOD_GET, '/api/users/version2/flights'); $this->assertEquals(2, count($res)); $this->assertContains($getFlightsRoute2, $res); $this->assertContains($getFlightsRoute3, $res); }