From df9ccde62166fed087a526c26c762dbe0502762e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 29 May 2017 22:23:53 -0400 Subject: [PATCH] Add remaining fluent methods for routes. I've made the multibyte option an implicit setting. I feel this makes routing simpler to use as we can infer from the patterns used whether or not multiple patterns should be used. Having an additional method call required makes Route clunkier to use. Refs #10689 --- src/Routing/Route/Route.php | 42 +++++++++++++++++++++ tests/TestCase/Routing/Route/RouteTest.php | 44 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/Routing/Route/Route.php b/src/Routing/Route/Route.php index 86d966a896c..4e905f82f50 100644 --- a/src/Routing/Route/Route.php +++ b/src/Routing/Route/Route.php @@ -184,11 +184,18 @@ public function setMethods(array $methods) /** * Set regexp patterns for routing parameters * + * If any of your patterns contain multibyte values, the `multibytePattern` + * mode will be enabled. + * * @param array $patterns The patterns to apply to routing elements * @return $this */ public function setPatterns(array $patterns) { + $patternValues = implode("", $patterns); + if (mb_strlen($patternValues) < strlen($patternValues)) { + $this->options['multibytePattern'] = true; + } $this->options = array_merge($this->options, $patterns); return $this; @@ -207,6 +214,41 @@ public function setHost($host) return $this; } + /** + * Set the names of parameters that will be converted into passed parameters + * + * @param array $names The names of the parameters that should be passed. + * @return $this + */ + public function setPass(array $names) + { + $this->options['pass'] = $names; + + return $this; + } + + /** + * Set the names of parameters that will persisted automatically + * + * Persistent parametesr allow you to define which route parameters should be automatically + * included when generating new URLs. You can override persistent parameters + * by redefining them in a URL or remove them by setting the persistent parameter to `false`. + * + * ``` + * // remove a persistent 'date' parameter + * Router::url(['date' => false', ...]); + * ``` + * + * @param array $names The names of the parameters that should be passed. + * @return $this + */ + public function setPersist(array $names) + { + $this->options['persist'] = $names; + + return $this; + } + /** * Check if a Route has been compiled into a regular expression. * diff --git a/tests/TestCase/Routing/Route/RouteTest.php b/tests/TestCase/Routing/Route/RouteTest.php index 86fbe8a37bd..b0458e7b5f5 100644 --- a/tests/TestCase/Routing/Route/RouteTest.php +++ b/tests/TestCase/Routing/Route/RouteTest.php @@ -1570,11 +1570,29 @@ public function testSetPatterns() $this->assertArrayHasKey('id', $route->options); $this->assertArrayHasKey('date', $route->options); $this->assertSame('[a-z]+', $route->options['id']); + $this->assertArrayNotHasKey('multibytePattern', $route->options); $this->assertFalse($route->parse('/reviews/a-b-c/xyz')); $this->assertNotEmpty($route->parse('/reviews/2016-05-12/xyz')); } + /** + * Test setting patterns enables multibyte mode + * + * @return void + */ + public function testSetPatternsMultibyte() + { + $route = new Route('/reviews/:accountid/:slug', ['controller' => 'Reviews', 'action' => 'view']); + $result = $route->setPatterns([ + 'date' => '[A-zА-я\-\ ]+', + 'accountid' => '[a-z]+' + ]); + $this->assertArrayHasKey('multibytePattern', $route->options); + + $this->assertNotEmpty($route->parse('/reviews/abcs/bla-blan-тест')); + } + /** * Test setting host requirements * @@ -1598,4 +1616,30 @@ public function testSetHost() $request = $request->withUri($uri->withHost('blog.example.com')); $this->assertNotEmpty($route->parseRequest($request)); } + + /** + * Test setting pass parameters + * + * @return void + */ + public function testSetPass() + { + $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']); + $result = $route->setPass(['date', 'id']); + $this->assertSame($result, $route, 'Should return this'); + $this->assertEquals(['date', 'id'], $route->options['pass']); + } + + /** + * Test setting persisted parameters + * + * @return void + */ + public function testSetPersist() + { + $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']); + $result = $route->setPersist(['date']); + $this->assertSame($result, $route, 'Should return this'); + $this->assertEquals(['date'], $route->options['persist']); + } }