Skip to content

Commit

Permalink
Add remaining fluent methods for routes.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed May 31, 2017
1 parent 1d9350e commit df9ccde
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Routing/Route/Route.php
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Routing/Route/RouteTest.php
Expand Up @@ -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
*
Expand All @@ -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']);
}
}

0 comments on commit df9ccde

Please sign in to comment.