/** * Reverse resolves a route URL pattern by interpolating provided * values into their placeholders if required. * * @param string $identifier A string declaring an unique route identifier. * @param array $parameters An associative array of parameter values. The array key * must correspond with a placeholder. The value can only store * alphanumeric strings or integers. * @param array $query An associative array which specifies values for an query string appended to the * reversed constructed URI. * * @return string Returns a string representing the URI of the specified route. The string * also has any given parameters interpolated. If an array for a query is provided * it's also appended. * * @throws \hydro\routing\exceptions\RouteIdentifierException */ public function reverse(string $identifier, array $parameters = [], array $query = []) : string { $haystack = $this->collection->fetch($identifier)->interpolate($parameters); if ($query) { $haystack .= '?' . http_build_query($query); } return $haystack; }
/** * Lookup constructor. * * @param Collection $collection */ public function __construct(Collection $collection) { $preparation = []; foreach ($collection->all() as $identifier => $route) { if (strpos($route->pattern(), ')') !== false) { $preparation[$identifier] = $route; continue; } $this->static[$identifier] = $route->pattern(); } $this->prepare($preparation); }
/** * Adds a new route. * * @param string $identifier A string declaring an unique identifier for the route. * @param string $pattern A string declaring the URL pattern the route responds to. This * can also contain dynamic groups. * @param array $resources An associative array of resource handlers. The array key specifies one * or more (separated by a pipe character) HTTP request methods the handler * is capable of processing. The array value can be any arbitrary resource. * @param array $options An optional associative array of additional options. The reserved keys are * 'constraints' which is used to extract specific constraints out of the * URL pattern and 'defaults' which is used to set default values for any * declared capture group. * * @throws \hydro\routing\exceptions\RouteIdentifierException */ public function add(string $identifier, string $pattern, array $resources, array $options = []) { if ($this->finalized) { throw new \LogicException('Cannot add routes to a finalized route architect instance.'); } if (!$resources) { throw new \LogicException('Resource array cannot be empty.'); } $variables = []; $defaults = $options['defaults'] ?? []; $constraints = $options['constraints'] ?? []; if (strpos($pattern, '{') !== false) { list($pattern, $variables, $optionals) = $this->interpolation->interpolate($pattern, $constraints); /* * Update the default values by overwriting values * determined by the interpolator with user defined ones. */ $defaults = array_merge($optionals, $defaults); } $this->collection->add(new Route($identifier, $pattern, $resources, $variables, $defaults)); }