Skip to content

Commit

Permalink
Fixed the @todo that was preventing multiple chunks of the same type …
Browse files Browse the repository at this point in the history
…in a route.
  • Loading branch information
Vic Cherubini committed Nov 12, 2010
1 parent 7c5b6f7 commit 28c1f30
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 77 deletions.
78 changes: 36 additions & 42 deletions Jolt/Route/Named.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,77 +10,77 @@ class Named extends Route {
private $action = NULL;
private $controller = NULL;
private $requestMethod = NULL;

private $argv = array();
private $requestMethods = array();

public function __construct($requestMethod, $route, $controller, $action) {
$this->requestMethods = array('GET' => true, 'POST' => true, 'PUT' => true, 'DELETE' => true);

$this->setRequestMethod($requestMethod)
->setRoute($route)
->setController($controller)
->setAction($action)
->setArgv(array());
}

public function __destruct() {
$this->route = $this->controller = $this->action = NULL;
}

public function setAction($action) {
$action = ( !is_string($action) ? NULL : trim($action) );
if ( empty($action) ) {
throw new \Jolt\Exception('Passed action is empty and invalid.');
}

$this->action = $action;

return $this;
}

public function setArgv(array $argv) {
$this->argv = $argv;
return $this;
}

public function setController($controller) {
$controller = ( !is_string($controller) ? NULL : trim($controller) );
if ( empty($controller) ) {
throw new \Jolt\Exception('Passed Controller is empty and invalid.');
}

$this->controller = $controller;

return $this;
}

public function setRequestMethod($requestMethod) {
$requestMethod = ( !is_string($requestMethod) ? NULL : strtoupper(trim($requestMethod)) );
if ( !isset($this->requestMethods[$requestMethod]) ) {
throw new \Jolt\Exception("Request Method '{$requestMethod}' is invalid.");
}

$this->requestMethod = $requestMethod;
return $this;
}

public function getAction() {
return $this->action;
}

public function getArgv() {
return $this->argv;
}

public function getController() {
return $this->controller;
}

public function getRequestMethod() {
return $this->requestMethod;
}

public function isEqual(Route $route) {
return (
$route instanceof \Jolt\Route\Named &&
Expand All @@ -90,39 +90,39 @@ public function isEqual(Route $route) {
$route->getAction() === $this->getAction()
);
}

public function isValid() {
$route = $this->getRoute();

/* Special case of a valid route. */
if ( '/' == $route ) {
return true;
}

/**
* Jolt is more restrictive about routes than the normal Internet RFC
* standards. This is to keep them clean, legible and readible.
*
*
* @see tests/Jolt/Route/Route/NamedTest.php
*/
if ( 0 === preg_match('#^/([a-z]+)([a-z0-9_\-/%\.]*)$#i', $route) ) {
return false;
}

return true;
}

public function isValidPath($uri) {
// Remove the beginning / from the URI and route.
$uri = ltrim($uri, '/');
$uriChunkList = explode('/', $uri);
$uriChunkCount = count($uriChunkList);

$route = $this->getRoute();
$route = ltrim($route, '/');
$routeChunkList = explode('/', $route);
$routeChunkCount = count($routeChunkList);

// If all of the chunks eventually match, we have a matched route.
$matchedChunkCount = 0;

Expand All @@ -133,60 +133,54 @@ public function isValidPath($uri) {
for ( $i=0; $i<$routeChunkCount; $i++ ) {
// ucv == uri chunk value
$ucv = $uriChunkList[$i];

// rcv = route chunk value
$rcv = $routeChunkList[$i];

if ( $ucv == $rcv ) {
// If the two are exactly the same, no expansion is needed.
$matchedChunkCount++;
} else {
/**
* More investigation is required. See if there is a % character followed by a (n|s), and if so, expand it.
* A limitation is that only a single % replacement can exist in a chunk at once, for now.
*
* @todo Allow multiple %n or %s characters in a chunk at once.
*/
$offset = stripos($rcv, '%');
if ( false !== $offset && true === isset($rcv[$offset+1]) ) {
$rcvType = $rcv[$offset+1];
$rcvLength = strlen($rcv);

if ( 0 !== $offset ) {
$ucv = trim(substr_replace($ucv, NULL, 0, $offset));
}

if ( ($offset+2) < $rcvLength ) {
$goto = strlen(substr($rcv, $offset+2));
$ucv = substr_replace($ucv, NULL, -$goto);
}

// Now that we have the correct $ucv values, let's make sure they're types are correct.
$matched = false;

switch ( $rcvType ) {
case 'n': {
$matched = is_numeric($ucv);
break;
}

case 's': {
$matched = is_string($ucv) && !is_numeric($ucv);
break;
}
}

if ( true === $matched ) {
$matchedChunkCount++;
$argv[$rcv] = $ucv;
$argv[] = $ucv;
}
}
}
}
}

$this->argv = $argv;

return ( $matchedChunkCount === $routeChunkCount );
}
}
Loading

0 comments on commit 28c1f30

Please sign in to comment.