/** * 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. */ 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; }
/** * 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 $constraints An optional associative array of additional configurations. The only reserved * key is 'constraints' which is used to extract specific constraints out of the * URL pattern. */ public function add(string $identifier, string $pattern, array $resources, array $constraints = []) { if (!$resources) { throw new \LogicException('Resource array cannot be empty.'); } $variables = []; if (strpos($pattern, '{') !== false) { list($pattern, $variables) = $this->interpolation->interpolate($pattern, $constraints); } $this->collection->add(new Route($identifier, $pattern, $resources, $variables)); }
/** * 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); }