diff --git a/src/Contracts/UrlInterface.php b/src/Contracts/UrlInterface.php new file mode 100644 index 0000000..bfef22e --- /dev/null +++ b/src/Contracts/UrlInterface.php @@ -0,0 +1,42 @@ + + */ +interface UrlInterface +{ + /* ------------------------------------------------------------------------------------------------ + | Main Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Extract attributes for current url. + * + * @param bool|false|string $url + * + * @return array + */ + public static function extractAttributes($url = false); + + /** + * Change uri attributes (wildcards) for the ones in the $attributes array. + * + * @param array $attributes + * @param string $uri + * + * @return string + */ + public static function substituteAttributes(array $attributes, $uri); + + /** + * Build URL using array data from parse_url. + * + * @param array|false $parsed + * + * @return string + */ + public static function unparse($parsed); +} diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index 827670c..2197e60 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -1,4 +1,7 @@ current()) { - return []; - } - - $attributes = app('router')->current()->parameters(); - $response = event('routes.translation', [$attributes]); - - if ( ! empty($response)) { - $response = array_shift($response); - } - - if (is_array($response)) { - $attributes = array_merge($attributes, $response); - } - - return $attributes; + return self::extractAttributesFromCurrentRoute($router); } - $parse = parse_url($url); - $parse = isset($parse['path']) ? explode('/', $parse['path']) : []; - $url = []; + $attributes = []; + $parse = parse_url($url); + $parse = isset($parse['path']) ? explode('/', $parse['path']) : []; + $url = []; foreach ($parse as $segment) { if ( ! empty($segment)) $url[] = $segment; } - foreach (app('router')->getRoutes() as $route) { - /** @var \Illuminate\Routing\Route $route */ + foreach ($router->getRoutes() as $route) { + /** @var Route $route */ $path = $route->getUri(); if ( ! preg_match('/{[\w]+}/', $path)) { @@ -150,7 +139,41 @@ public static function unparse($parsed) } /* ------------------------------------------------------------------------------------------------ - | Other Functions + | Extract Functions + | ------------------------------------------------------------------------------------------------ + */ + /** + * Extract Attributes From Router. + * + * @return array + */ + private static function extractAttributesFromCurrentRoute(Router $router) + { + /** @var Route $route */ + $route = $router->current(); + + if (is_null($route)) { + return []; + } + + $attributes = $route->parameters(); + $response = event('routes.translation', [ + $attributes + ]); + + if ( ! empty($response)) { + $response = array_shift($response); + } + + if (is_array($response)) { + $attributes = array_merge($attributes, $response); + } + + return $attributes; + } + + /* ------------------------------------------------------------------------------------------------ + | Unparse Functions | ------------------------------------------------------------------------------------------------ */ /**