Skip to content

Commit

Permalink
Refactoring and fixing Url Utility Class
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 22, 2015
1 parent 116e9d9 commit 9a86937
Showing 1 changed file with 99 additions and 79 deletions.
178 changes: 99 additions & 79 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,83 +42,7 @@ public static function extractAttributes($url = false)
if ( ! empty($segment)) $url[] = $segment;
}

return self::extractAttributesFormRoutes($url, $router->getRoutes());
}

/**
* Extract attributes from routes.
*
* @param array $url
* @param \Illuminate\Routing\RouteCollection $routes
*
* @return array
*/
private static function extractAttributesFormRoutes($url, $routes)
{
$attributes = [];

foreach ($routes as $route) {
/** @var Route $route */
$path = $route->getUri();

if ( ! preg_match('/{[\w]+}/', $path)) {
continue;
}

$i = 0;
$match = true;

foreach (explode('/', $path) as $j => $segment) {
if (isset($url[$i])) {
if ($segment !== $url[$i]) {
self::extractAttributesFromSegment($url, $i, $j, $segment, $attributes);
}

$i++;
continue;
}
elseif ( ! preg_match('/{[\w]+\?}/', $segment)) {
// No optional parameters but no more $url given this route does not match the url
$match = false;
break;
}
}

if (isset($url[$i + 1])) {
$match = false;
}

if ($match) { break; }
}

return $attributes;
}

/**
* Extract attribute from a segment.
*
* @param array $url
* @param int $i
* @param int $j
* @param string $segment
* @param array $attributes
*/
private static function extractAttributesFromSegment($url, $i, $j, $segment, &$attributes)
{
// Required parameters
if (preg_match('/{[\w]+}/', $segment)) {
$attributeName = preg_replace(['/{/', '/\?/', '/}/'], '', $segment);
$attributes[$attributeName] = $url[$i];
}

// Optional parameter
if (
preg_match('/{[\w]+\?}/', $segment) &&
( ! isset($path[$j + 1]) || $path[$j + 1] !== $url[$i])
) {
$attributeName = preg_replace(['/{/', '/\?/', '/}/'], '', $segment);
$attributes[$attributeName] = $url[$i];
}
return self::extractAttributesFromRoutes($url, $router->getRoutes());
}

/**
Expand Down Expand Up @@ -159,11 +83,11 @@ public static function unparse($parsed)

return $url;
}

/* ------------------------------------------------------------------------------------------------
| Extract Functions
| ------------------------------------------------------------------------------------------------
*/

/**
* Extract Attributes From Router.
*
Expand All @@ -190,11 +114,107 @@ private static function extractAttributesFromCurrentRoute($route)

return $attributes;
}

/**
* Extract attributes from routes.
*
* @param array $url
* @param \Illuminate\Routing\RouteCollection $routes
*
* @return array
*/
private static function extractAttributesFromRoutes($url, $routes)
{
$attributes = [];

foreach ($routes as $route) {
/** @var Route $route */
$path = $route->getUri();

if ( ! preg_match('/{[\w]+}/', $path)) {
continue;
}

$match = self::hasAttributesFromUriPath($url, $path, $attributes);

if ($match) {
break;
}
}

return $attributes;
}

/**
* Check if has attributes from a route.
*
* @param array $url
* @param string $path
* @param array $attributes
*
* @return bool
*/
private static function hasAttributesFromUriPath($url, $path, &$attributes)
{
$i = 0;
$match = true;
$path = explode('/', $path);

foreach ($path as $j => $segment) {
if (isset($url[$i])) {
if ($segment !== $url[$i]) {
self::extractAttributesFromSegment($url, $path, $i, $j, $segment, $attributes);
}

$i++;
continue;
}
elseif ( ! preg_match('/{[\w]+\?}/', $segment)) {
// No optional parameters but no more $url given this route does not match the url
$match = false;
break;
}
}

if (isset($url[$i + 1])) {
$match = false;
}

return $match;
}

/**
* Extract attribute from a segment.
*
* @param array $url
* @param array $path
* @param int $i
* @param int $j
* @param string $segment
* @param array $attributes
*/
private static function extractAttributesFromSegment($url, $path, $i, $j, $segment, &$attributes)
{
// Required parameters
if (preg_match('/{[\w]+}/', $segment)) {
$attributeName = preg_replace(['/{/', '/\?/', '/}/'], '', $segment);
$attributes[$attributeName] = $url[$i];
}

// Optional parameter
if (
preg_match('/{[\w]+\?}/', $segment) &&
( ! isset($path[$j + 1]) || $path[$j + 1] !== $url[$i])
) {
$attributeName = preg_replace(['/{/', '/\?/', '/}/'], '', $segment);
$attributes[$attributeName] = $url[$i];
}
}

/* ------------------------------------------------------------------------------------------------
| Unparse Functions
| ------------------------------------------------------------------------------------------------
*/

/**
* Check parsed URL.
*
Expand Down

0 comments on commit 9a86937

Please sign in to comment.