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'); }