예제 #1
0
 public function testValidate()
 {
     $this->assertTrue(PathMatcher::validate(array('v' => 1), 'v', 'i'));
     $this->assertTrue(PathMatcher::validate(array('v' => '2'), 'v', 'integer'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'ag'), 'v', 'i'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'gf2'), 'v', 'integer'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'alph'), 'v', 'a'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'test'), 'v', 'alpha'));
     $this->assertFalse(PathMatcher::validate(array('v' => '5425'), 'v', 'a'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'test43'), 'v', 'alpha'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'an123'), 'v', 'an'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'hope092'), 'v', 'alnum'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'sam'), 'v', 'alphanumeric'));
     $this->assertTrue(PathMatcher::validate(array('v' => 1544), 'v', 'an'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'an1@23'), 'v', 'an'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'hope 92'), 'v', 'alnum'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'sam?'), 'v', 'alphanumeric'));
     $this->assertFalse(PathMatcher::validate(array('v' => '1544!'), 'v', 'an'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'maybe-134'), 'v', 's'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'test-134-unit-cool'), 'v', 'slug'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'ma!ybe-134'), 'v', 's'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'test-134-uni t-cool'), 'v', 'slug'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'A04F32BC'), 'v', 'h'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'ce413f'), 'v', 'hex'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'A0GF2BC'), 'v', 'h'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'ce41RW3f'), 'v', 'hex'));
     $this->assertTrue(PathMatcher::validate(array('v' => '*****@*****.**'), 'v', 'e'));
     $this->assertTrue(PathMatcher::validate(array('v' => '*****@*****.**'), 'v', 'email'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'woah@example,com'), 'v', 'e'));
     $this->assertFalse(PathMatcher::validate(array('v' => 'runner!'), 'v', 'email'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'words 123?'), 'v', 'any'));
     $this->assertTrue(PathMatcher::validate(array('v' => 'test maybe, 123?'), 'v', 'text'));
     $this->assertTrue(PathMatcher::validate(array('v' => '123'), 'v', '[1-3]+'));
     $this->assertFalse(PathMatcher::validate(array('v' => '4552'), 'v', '[1-3]+'));
 }
예제 #2
0
 /**
  * Generate the URL given a route and its parameters
  * @param  Packfire\Router\RouteInterface $route The route the generate the URL from
  * @param  array $params The parameters of the route
  * @return string Returns the generated URL.
  */
 public function generate(RouteInterface $route, $params)
 {
     $rules = $route->rules();
     if (isset($rules['path'])) {
         $path = $rules['path'];
         if (isset($path['uri'])) {
             $uri = $path['uri'];
             $tokens = PathMatcher::createTokens($uri);
             if ($tokens) {
                 $replacements = array();
                 foreach ($tokens as $token) {
                     $name = $token[3];
                     if (isset($params[$name])) {
                         $replacements[$token[0]] = $token[2] . $params[$name];
                     } elseif ($token[4] == '?') {
                         $replacements[$token[0]] = '';
                     } else {
                         throw new MissingRequiredParameterException($route->name(), $name);
                     }
                 }
                 $uri = str_replace(array_keys($replacements), $replacements, $uri);
             }
             return $uri;
         }
     }
 }