Пример #1
0
 public function testParent()
 {
     $parentroute = new GitPHP_Route('parent/:parent', array('parent' => 'parentvalue'), array('parentparam' => 'parentvalue'));
     $childroute = new GitPHP_Route('child/:child', array('child' => 'childvalue'), array('childparam' => 'childvalue'), $parentroute);
     $this->assertEquals('parent/:parent/child/:child', $childroute->GetPath());
     $params = array('child' => 'childvalue');
     $this->assertFalse($childroute->Valid($params));
     $params['parent'] = 'parentvalue';
     $this->assertTrue($childroute->Valid($params));
     $usedparams = $childroute->GetUsedParameters();
     $this->assertCount(4, $usedparams);
     $this->assertContains('parent', $usedparams);
     $this->assertContains('child', $usedparams);
     $this->assertContains('parentparam', $usedparams);
     $this->assertContains('childparam', $usedparams);
     $routeparams = $childroute->Match('parent/parentvalue/child/childvalue');
     $this->assertCount(4, $routeparams);
     $this->assertEquals('parentvalue', $routeparams['parent']);
     $this->assertEquals('childvalue', $routeparams['child']);
     $this->assertEquals('parentvalue', $routeparams['parentparam']);
     $this->assertEquals('childvalue', $routeparams['childparam']);
     $this->assertEquals('parent/parentvalue/child/childvalue', $childroute->Build($params));
 }
Пример #2
0
 /**
  * Constructor
  *
  * @param string $path route path
  * @param string[] $constraints route constraints
  * @param string[] $extraParameters additional route parameters
  * @param GitPHP_Route $parent parent route
  */
 public function __construct($path, $constraints = array(), $extraParameters = array(), $parent = null)
 {
     if (empty($path)) {
         throw new Exception('Path is required');
     }
     // initialize path
     if ($parent) {
         $this->path = $parent->GetPath() . '/' . $path;
     } else {
         $this->path = $path;
     }
     // initialize constraints
     if ($parent) {
         $this->constraints = array_merge($parent->constraints, $constraints);
     } else {
         $this->constraints = $constraints;
     }
     // initialise extra parameters
     if ($parent) {
         $this->extraParameters = array_merge($parent->extraParameters, $extraParameters);
     } else {
         $this->extraParameters = $extraParameters;
     }
     // initialize url parameters
     $fullPath = explode('/', $this->path);
     foreach ($fullPath as $pathpiece) {
         if (strncmp($pathpiece, ':', 1) === 0) {
             $param = substr($pathpiece, 1);
             $this->urlParameters[] = $param;
         }
     }
     // initialize used parameters
     $this->usedParameters = array_merge($this->urlParameters, array_keys($extraParameters));
     if ($parent) {
         $this->usedParameters = array_merge($parent->GetUsedParameters(), $this->usedParameters);
     }
     $this->usedParameters = array_unique($this->usedParameters);
 }