Exemplo n.º 1
0
 public function testDecoding()
 {
     $varargs = VarargsHelper::deserialize('/var1/val1/var2/val2');
     $this->assertEquals(array('var1' => 'val1', 'var2' => 'val2'), $varargs);
 }
Exemplo n.º 2
0
 /**
  * @see IRoute
  */
 public function decode($path)
 {
     // Removing the first token, which is always empty
     $tokens = array_slice(explode('/', $path), 1);
     $controller = $this->controller;
     $action = $this->action;
     $params = array();
     foreach (array_values($this->parsedRule['components']) as $i => $component) {
         if ($component['isVar']) {
             if (!isset($tokens[$i])) {
                 return null;
             }
             if ($component['choices'] && !in_array($tokens[$i], $component['choices'])) {
                 return null;
             }
             $paramName = $component['name'];
             $paramValue = $tokens[$i];
             $params[$paramName] = urldecode($paramValue);
             $controller = str_replace(":{$paramName}", ucfirst($paramValue), $controller);
             $action = str_replace(":{$paramName}", $paramValue, $action);
         } else {
             if ($component['name'] !== $tokens[$i]) {
                 return null;
             }
         }
         unset($tokens[$i]);
     }
     if (!empty($tokens)) {
         if ($this->parsedRule['varargs']) {
             $varargsStartsAt = StringUtil::findNthOccurence($path, '/', count($this->parsedRule['components']) + 1);
             $params = array_merge($params, VarargsHelper::deserialize(substr($path, $varargsStartsAt)));
         } else {
             return null;
         }
     }
     return new ControllerActionParams($controller, $action, $params);
 }