Exemplo n.º 1
0
 public function testFindNthOccurence()
 {
     $this->assertNull(StringUtil::findNthOccurence('aaaa', 'a', 5));
     $this->assertNull(StringUtil::findNthOccurence('ciao ciao ciao', 'ciao', 4));
     $this->assertNull(StringUtil::findNthOccurence('ciao', 'b', 1));
     $this->assertEquals(2, StringUtil::findNthOccurence('aaaa', 'a', 3));
     $this->assertEquals(3, StringUtil::findNthOccurence('aaaa', 'a', 4));
     $this->assertEquals(5, StringUtil::findNthOccurence('ciao ciao ciao', 'ciao', 2));
     $this->assertEquals(10, StringUtil::findNthOccurence('/home/user/dir/dir', '/', 3));
 }
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);
 }