public function getRoutePath(Route $route, array $params)
 {
     $routes = $this->routeParser->parse($route->getPattern());
     foreach ($routes as $route) {
         $path = '';
         $index = 0;
         foreach ($route as $part) {
             // Fixed segment in the route
             if (is_string($part)) {
                 $path .= $part;
                 continue;
             }
             // Placeholder in the route
             if ($index === count($params)) {
                 throw new \InvalidArgumentException('Too few parameters given');
             }
             if ($this->validateParams && $part[1] !== '[^/]+') {
                 if (!preg_match("/^{$part[1]}\$/", $params[$index])) {
                     throw new \InvalidArgumentException("Route parameter pattern mismatch: " . "Parameter #{$index} \"{$params[$index]}\" does not match pattern {$part[1]}");
                 }
             }
             $path .= $params[$index++];
         }
         // If number of params in route matches with number of params given, use that route.
         // Otherwise try to find a route that has more params
         if ($index === count($params)) {
             return $path;
         }
     }
     throw new \InvalidArgumentException('Too many parameters given');
 }
Beispiel #2
0
 public function addRoute($method, $path, $name, $handler)
 {
     $routeData = $this->routeParser->parse($path);
     $this->dataGenerator->addRoute($method, $routeData, $handler);
     $routeDate['method'] = $method;
     $this->reverse[$name] = $routeData;
     return $this;
 }
Beispiel #3
0
 public function addRoute($method, $path, $name, $handler)
 {
     $routeDatas = $this->routeParser->parse($path);
     foreach ($routeDatas as $routeData) {
         $this->dataGenerator->addRoute($method, $routeData, $handler);
     }
     $this->reverse[$name] = $routeDatas;
     return $this;
 }
 /**
  * Build the path for a named route
  *
  * @param string $name        Route name
  * @param array  $data        Named argument replacement data
  * @param array  $queryParams Optional query string parameters
  *
  * @return string
  *
  * @throws RuntimeException         If named route does not exist
  * @throws InvalidArgumentException If required data not provided
  */
 public function pathFor($name, array $data = [], array $queryParams = [])
 {
     $route = $this->getNamedRoute($name);
     $pattern = $route->getPattern();
     $routeDatas = $this->routeParser->parse($pattern);
     // $routeDatas is an array of all possible routes that can be made. There is
     // one routedata for each optional parameter plus one for no optional parameters.
     //
     // The most specific is last, so we look for that first.
     $routeDatas = array_reverse($routeDatas);
     $segments = [];
     foreach ($routeDatas as $routeData) {
         foreach ($routeData as $item) {
             if (is_string($item)) {
                 // this segment is a static string
                 $segments[] = $item;
                 continue;
             }
             // This segment has a parameter: first element is the name
             if (!array_key_exists($item[0], $data)) {
                 // we don't have a data element for this segment: cancel
                 // testing this routeData item, so that we can try a less
                 // specific routeData item.
                 $segments = [];
                 $segmentName = $item[0];
                 break;
             }
             $segments[] = $data[$item[0]];
         }
         if (!empty($segments)) {
             // we found all the parameters for this route data, no need to check
             // less specific ones
             break;
         }
     }
     if (empty($segments)) {
         throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName);
     }
     $url = implode('', $segments);
     if ($this->basePath) {
         $url = $this->basePath . $url;
     }
     if ($queryParams) {
         $url .= '?' . http_build_query($queryParams);
     }
     return $url;
 }