Skip to content

Commit

Permalink
Adding transAny method to Router, fixing doc comments & other stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Sep 21, 2015
1 parent c2a562d commit cbeef75
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 42 deletions.
27 changes: 24 additions & 3 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function localizedGroup(Closure $callback, $attributes = [])
}

/**
* Register a new translated GET route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
*
Expand All @@ -60,6 +62,8 @@ public function transGet($trans, $action)
}

/**
* Register a new translated POST route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
*
Expand All @@ -73,6 +77,8 @@ public function transPost($trans, $action)
}

/**
* Register a new translated PUT route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
*
Expand All @@ -86,7 +92,7 @@ public function transPut($trans, $action)
}

/**
* Register a new PATCH route with the router.
* Register a new translated PATCH route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
Expand All @@ -101,7 +107,7 @@ public function transPatch($trans, $action)
}

/**
* Register a new DELETE route with the router.
* Register a new translated DELETE route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
Expand All @@ -116,7 +122,7 @@ public function transDelete($trans, $action)
}

/**
* Register a new OPTIONS route with the router.
* Register a new translated OPTIONS route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
Expand All @@ -129,4 +135,19 @@ public function transOptions($trans, $action)

return $this->options($uri, $action);
}

/**
* Register a new translated any route with the router.
*
* @param string $trans
* @param \Closure|array|string $action
*
* @return \Illuminate\Routing\Route
*/
public function transAny($trans, $action)
{
$uri = localization()->transRoute($trans);

return $this->any($uri, $action);
}
}
132 changes: 132 additions & 0 deletions tests/Stubs/Http/RouteRegistrar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php namespace Arcanedev\Localization\Tests\Stubs\Http;

use Arcanedev\Localization\Routing\Router;

/**
* Class RouteRegistrar
*
* @package Arcanedev\Localization\Tests\Stubs\Http
* @author ARCANEDEV <[email protected]>
*/
class RouteRegistrar
{
/**
* @var Router
*/
protected $router;

/* ------------------------------------------------------------------------------------------------
| Getters & Setters
| ------------------------------------------------------------------------------------------------
*/
/**
* Set the router.
*
* @param Router $router
*
* @return self
*/
private function setRouter(Router $router)
{
$this->router = $router;

return $this;
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Map the routes.
*
* @param Router $router
*/
public function map(Router $router)
{
$this->setRouter($router);

$this->router->localizedGroup(function () {
$this->router->get('/', [
'as' => 'index',
function () {
return app('translator')->get('localization::routes.hello');
}
]);

$this->router->get('test', [
'as' => 'test',
function () {
return app('translator')->get('localization::routes.test-text');
}
]);

$this->router->transGet('localization::routes.about', [
'as' => 'about',
function () {
return localization()->getLocalizedURL('es') ?: 'Not url available';
}
]);

$this->router->transGet('localization::routes.view', [
'as' => 'view',
function () {
return localization()->getLocalizedURL('es') ?: 'Not url available';
}
]);

$this->router->transGet('localization::routes.view-project', [
'as' => 'view-project',
function () {
return localization()->getLocalizedURL('es') ?: 'Not url available';
}
]);

/* ------------------------------------------------------------------------------------------------
| Other method
| ------------------------------------------------------------------------------------------------
*/
$this->router->transPost('localization::routes.methods.post', [
'as' => 'method.post',
function () {
return 'POST method';
}
]);

$this->router->transPut('localization::routes.methods.put', [
'as' => 'method.put',
function () {
return 'PUT method';
}
]);

$this->router->transPatch('localization::routes.methods.patch', [
'as' => 'method.patch',
function () {
return 'PATCH method';
}
]);

$this->router->transOptions('localization::routes.methods.options', [
'as' => 'method.options',
function () {
return 'OPTIONS method';
}
]);

$this->router->transDelete('localization::routes.methods.delete', [
'as' => 'method.delete',
function () {
return 'DELETE method';
}
]);

$this->router->transAny('localization::routes.methods.any', [
'as' => 'method.any',
function () {
return 'Any method';
}
]);
});
}
}
37 changes: 1 addition & 36 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,41 +162,6 @@ protected function setRoutes($locale = false)
localization()->setLocale($locale);
}

app('router')->localizedGroup(function () {
app('router')->get('/', [
'as' => 'index',
function () {
return app('translator')->get('localization::routes.hello');
}
]);

app('router')->get('test', [
'as' => 'test',
function () {
return app('translator')->get('localization::routes.test-text');
}
]);

app('router')->transGet('localization::routes.about', [
'as' => 'about',
function () {
return localization()->getLocalizedURL('es') ? : "Not url available";
}
]);

app('router')->transGet('localization::routes.view', [
'as' => 'view',
function () {
return localization()->getLocalizedURL('es') ? : "Not url available";
}
]);

app('router')->transGet('localization::routes.view-project', [
'as' => 'view-project',
function () {
return localization()->getLocalizedURL('es') ? : "Not url available";
}
]);
});
(new Stubs\Http\RouteRegistrar)->map(app('router'));
}
}
11 changes: 10 additions & 1 deletion tests/fixtures/lang/en/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
'view' => 'view/{id}',
'view-project' => 'view/{id}/project/{project_id?}',
'hello' => 'Hello world',
'test-text' => 'Test text'
'test-text' => 'Test text',

'methods' => [
'post' => 'post',
'put' => 'put',
'patch' => 'patch',
'options' => 'options',
'delete' => 'delete',
'any' => 'any',
],
];
11 changes: 10 additions & 1 deletion tests/fixtures/lang/es/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
'view' => 'ver/{id}',
'view-project' => 'ver/{id}/proyecto/{project_id?}',
'hello' => 'Hola mundo',
'test-text' => 'Texto de prueba'
'test-text' => 'Texto de prueba',

'methods' => [
'post' => 'post',
'put' => 'put',
'patch' => 'patch',
'options' => 'options',
'delete' => 'delete',
'any' => 'any',
],
];
11 changes: 10 additions & 1 deletion tests/fixtures/lang/fr/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@
'view' => 'voir/{id}',
'view-project' => 'voir/{id}/project/{project_id?}',
'hello' => 'Salut le monde',
'test-text' => 'Texte de test'
'test-text' => 'Texte de test',

'methods' => [
'post' => 'post',
'put' => 'put',
'patch' => 'patch',
'options' => 'options',
'delete' => 'delete',
'any' => 'any',
],
];

0 comments on commit cbeef75

Please sign in to comment.