public function buildUri(RouteData $routeData, $language = null) { $name = $routeData->getName(); if (!isset($this->routes[$name])) { throw new \Exception('route "' . $name . '" not found'); } $path = $this->routes[$name]->buildUri($routeData, $language); return $path; }
public function buildUri(RouteData $routeData, $language = null) { $placeholders = []; foreach ($routeData->getParams() as $name => $value) { if (is_array($value)) { $value = implode('/', $value); } $placeholders[':' . $name] = $value; } $url = ''; $explodedRoute = explode('(/', $this->path); $requiredRoutePart = array_shift($explodedRoute); if ($requiredRoutePart) { $url .= str_replace(array_keys($placeholders), array_values($placeholders), $requiredRoutePart); if (strpos($url, ':') !== false) { throw new \RuntimeException('Parameters are missing (' . $url . ')'); } if (!$explodedRoute) { return str_replace('+', '', $url); } } $optionalRouteParts = array_map(function ($value) { return rtrim($value, ')'); }, $explodedRoute); $optionalRouteParts = array_filter($optionalRouteParts, function ($v) { return $v !== ''; }); $defaults = []; foreach ($this->defaults as $k => $v) { $defaults[':' . $k] = $v; } $tmpUrl = ''; foreach ($optionalRouteParts as $routePart) { $urlPart = str_replace(array_keys($placeholders), array_values($placeholders), $routePart); $replaced = $urlPart !== $routePart; if (strpos($urlPart, ':') !== false) { $urlPart = str_replace(array_keys($defaults), array_values($defaults), $urlPart); } if (strpos($urlPart, ':') !== false) { if ($replaced) { throw new \RuntimeException('Parameters are missing (' . $urlPart . ')'); } break; } $tmpUrl .= '/' . $urlPart; if ($urlPart !== str_replace(array_keys($defaults), array_values($defaults), $routePart)) { $url .= $tmpUrl; $tmpUrl = ''; } } return str_replace('+', '', $url); }