Skip to content

Commit

Permalink
Refactoring extractAttributes method + Adding Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 13, 2015
1 parent f010f45 commit fc3ec07
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 24 deletions.
42 changes: 42 additions & 0 deletions src/Contracts/UrlInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace Arcanedev\Localization\Contracts;

/**
* Interface UrlInterface
*
* @package Arcanedev\Localization\Contracts
* @author ARCANEDEV <[email protected]>
*/
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);
}
71 changes: 47 additions & 24 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php namespace Arcanedev\Localization\Utilities;
use Arcanedev\Localization\Contracts\UrlInterface;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;

/**
* Class Url
Expand All @@ -8,7 +11,7 @@
*
* @todo: Refactoring
*/
class Url
class Url implements UrlInterface
{
/* ------------------------------------------------------------------------------------------------
| Main Functions
Expand All @@ -23,37 +26,23 @@ class Url
*/
public static function extractAttributes($url = false)
{
$attributes = [];
$router = app('router');

if (empty($url)) {
if ( ! app('router')->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)) {
Expand Down Expand Up @@ -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
| ------------------------------------------------------------------------------------------------
*/
/**
Expand Down

0 comments on commit fc3ec07

Please sign in to comment.