From 28b5aac23656483f18ce71ac7b2fc071d9791f9a Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sun, 13 Dec 2015 17:46:53 +0000 Subject: [PATCH 1/3] Adding RouteBindable Contract --- src/Contracts/RouteBindable.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/Contracts/RouteBindable.php diff --git a/src/Contracts/RouteBindable.php b/src/Contracts/RouteBindable.php new file mode 100644 index 0000000..9f2ece8 --- /dev/null +++ b/src/Contracts/RouteBindable.php @@ -0,0 +1,17 @@ + + */ +interface RouteBindable +{ + /** + * Get the wildcard value from the class. + * + * @return int|string + */ + public function getWildcardValue(); +} From fc69f1a9e11d49ffb89dc7bc161c6c18d5b82b59 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sun, 13 Dec 2015 17:48:18 +0000 Subject: [PATCH 2/3] Fix Url::substituteAttributes() if the request contain a bindable attributes --- src/Utilities/Url.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index 7a22277..d0ccff2 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -1,5 +1,6 @@ $value) { + if ($value instanceof RouteBindable) { + $value = $value->getWildcardValue(); + } + $uri = str_replace(['{' . $key . '?}', '{' . $key . '}'], $value, $uri); } From 03268bb64b106d424ce238f2f1395cdc38f9d37e Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Sun, 13 Dec 2015 18:14:59 +0000 Subject: [PATCH 3/3] Testing the Url Utility class for route bindings fix --- tests/Stubs/User.php | 51 +++++++++++++++++++++++++++++++++++++ tests/Utilities/UrlTest.php | 13 ++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/Stubs/User.php diff --git a/tests/Stubs/User.php b/tests/Stubs/User.php new file mode 100644 index 0000000..567be08 --- /dev/null +++ b/tests/Stubs/User.php @@ -0,0 +1,51 @@ + + */ +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; + } +} diff --git a/tests/Utilities/UrlTest.php b/tests/Utilities/UrlTest.php index 58cb128..6b0234e 100644 --- a/tests/Utilities/UrlTest.php +++ b/tests/Utilities/UrlTest.php @@ -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}') + ); + } }