Skip to content

Commit

Permalink
Merge pull request #21 from ARCANEDEV/route-bindings-fix
Browse files Browse the repository at this point in the history
Fix the Localization::localesNavbar() issue by using the route bindings
  • Loading branch information
arcanedev-maroc committed Dec 13, 2015
2 parents 3a2e79a + 03268bb commit f3c7593
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Contracts/RouteBindable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace Arcanedev\Localization\Contracts;

/**
* Interface RouteBindable
*
* @package Arcanedev\Localization\Contracts
* @author ARCANEDEV <[email protected]>
*/
interface RouteBindable
{
/**
* Get the wildcard value from the class.
*
* @return int|string
*/
public function getWildcardValue();
}
5 changes: 5 additions & 0 deletions src/Utilities/Url.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Arcanedev\Localization\Utilities;

use Arcanedev\Localization\Contracts\RouteBindable;
use Arcanedev\Localization\Contracts\UrlInterface;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
Expand Down Expand Up @@ -53,6 +54,10 @@ public static function extractAttributes($url = false)
public static function substituteAttributes(array $attributes, $uri)
{
foreach ($attributes as $key => $value) {
if ($value instanceof RouteBindable) {
$value = $value->getWildcardValue();
}

$uri = str_replace(['{' . $key . '?}', '{' . $key . '}'], $value, $uri);
}

Expand Down
51 changes: 51 additions & 0 deletions tests/Stubs/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php namespace Arcanedev\Localization\Tests\Stubs;

use Arcanedev\Localization\Contracts\RouteBindable;

/**
* Class User
*
* @package Arcanedev\Localization\Tests\Stubs
* @author ARCANEDEV <[email protected]>
*/
class User implements RouteBindable
{
/* ------------------------------------------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
*/
/**
* User's username.
*
* @var string
*/
public $username;

/* ------------------------------------------------------------------------------------------------
| Constructor
| ------------------------------------------------------------------------------------------------
*/
/**
* User constructor.
*
* @param string $username
*/
public function __construct($username)
{
$this->username = $username;
}

/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Get the wildcard value from the class.
*
* @return int|string
*/
public function getWildcardValue()
{
return $this->username;
}
}
13 changes: 13 additions & 0 deletions tests/Utilities/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ public function it_can_unparse_url()
$this->assertEquals($url, Url::unparse($parsed));
}
}

/** @test */
public function it_can_substitute_route_bindings()
{
$attributes = [
'user' => new \Arcanedev\Localization\Tests\Stubs\User('admin'),
];

$this->assertEquals(
'users/admin',
Url::substituteAttributes($attributes, 'users/{user}')
);
}
}

0 comments on commit f3c7593

Please sign in to comment.