From 18700855f18d47e74a379bb9a3ae12d2cb01ef6f Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 27 Apr 2017 13:25:25 +0100 Subject: [PATCH 1/4] Updating travis config file & composer.json --- .travis.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5c475de..a65ee86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ env: before_script: - travis_retry composer self-update - - travis_retry composer require --prefer-source --no-interaction --dev "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}" "orchestra/database:${TESTBENCH_VERSION}" + - travis_retry composer require --prefer-dist --no-interaction "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}" script: - composer validate diff --git a/composer.json b/composer.json index 13e116b..7261e19 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ } }, "scripts": { - "testbench": "composer require --dev \"orchestra/testbench-browser-kit=~3.4\" \"orchestra/database=~3.4\"" + "testbench": "composer require --dev \"orchestra/testbench-browser-kit=~3.4\"" }, "suggest": { "ext-intl": "Use Intl extension for 'Locale' class (an identifier used to get language)." From 5d530aea8a29dcfa42367fb473dd28a19e0277bc Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 27 Apr 2017 13:57:48 +0100 Subject: [PATCH 2/4] Updating the package --- src/Contracts/LocalesManager.php | 2 ++ src/Contracts/Localization.php | 3 +++ src/Contracts/Negotiator.php | 1 + src/Contracts/RouteBindable.php | 5 ++++ src/Contracts/RouteTranslator.php | 3 +++ src/Contracts/Url.php | 1 + src/Entities/Locale.php | 5 ++++ src/Entities/LocaleCollection.php | 5 +++- src/Localization.php | 14 +++++++--- src/LocalizationServiceProvider.php | 21 +++++++-------- src/Middleware/LocaleCookieRedirect.php | 1 + src/Middleware/LocaleSessionRedirect.php | 1 + src/Middleware/LocalizationRedirect.php | 2 ++ src/Middleware/LocalizationRoutes.php | 1 + src/Middleware/Middleware.php | 4 +++ src/Middleware/TranslationRedirect.php | 3 +++ src/Providers/RoutingServiceProvider.php | 24 +++++------------ src/Providers/UtilitiesServiceProvider.php | 20 +++++++-------- src/Routing/Router.php | 13 ++++++---- src/Traits/LocalizationKernelTrait.php | 5 ++++ src/Utilities/LocalesManager.php | 5 ++++ src/Utilities/Negotiator.php | 15 +++++++---- src/Utilities/RouteTranslator.php | 5 ++++ src/Utilities/Url.php | 29 ++++++++++----------- tests/LocalizationServiceProviderTest.php | 20 ++++++++------- tests/LocalizationTest.php | 19 +++++++------- tests/Stubs/User.php | 15 ++++++----- tests/TestCase.php | 19 +++++++++----- tests/Utilities/LocalesManagerTest.php | 23 +++++++++-------- tests/Utilities/NegotiatorTest.php | 30 ++++++++++++---------- tests/Utilities/UrlTest.php | 7 ++--- 31 files changed, 197 insertions(+), 124 deletions(-) diff --git a/src/Contracts/LocalesManager.php b/src/Contracts/LocalesManager.php index cbc99fc..2cda3b5 100644 --- a/src/Contracts/LocalesManager.php +++ b/src/Contracts/LocalesManager.php @@ -12,6 +12,7 @@ interface LocalesManager | Getters & Setters | ----------------------------------------------------------------- */ + /** * Set and return current locale. * @@ -94,6 +95,7 @@ public function setSupportedLocales(array $supportedLocales); | Check Methods | ----------------------------------------------------------------- */ + /** * Check if default is supported. * diff --git a/src/Contracts/Localization.php b/src/Contracts/Localization.php index b6fe73c..ae199c3 100644 --- a/src/Contracts/Localization.php +++ b/src/Contracts/Localization.php @@ -14,6 +14,7 @@ interface Localization | Getters & Setters | ----------------------------------------------------------------- */ + /** * Returns default locale. * @@ -127,6 +128,7 @@ public function setRouteNameFromRequest(Request $request); | Main Methods | ----------------------------------------------------------------- */ + /** * Translate routes and save them to the translated routes array (used in the localize route filter). * @@ -194,6 +196,7 @@ public function getUrlFromRouteName($locale, $transKey, $attributes = []); | Check Methods | ----------------------------------------------------------------- */ + /** * Hide the default locale in URL ?? * diff --git a/src/Contracts/Negotiator.php b/src/Contracts/Negotiator.php index 42e72ec..8c1735c 100644 --- a/src/Contracts/Negotiator.php +++ b/src/Contracts/Negotiator.php @@ -15,6 +15,7 @@ interface Negotiator | Main Methods | ----------------------------------------------------------------- */ + /** * Make Negotiator instance. * diff --git a/src/Contracts/RouteBindable.php b/src/Contracts/RouteBindable.php index 9f2ece8..e18669e 100644 --- a/src/Contracts/RouteBindable.php +++ b/src/Contracts/RouteBindable.php @@ -8,6 +8,11 @@ */ interface RouteBindable { + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + /** * Get the wildcard value from the class. * diff --git a/src/Contracts/RouteTranslator.php b/src/Contracts/RouteTranslator.php index a81ecab..d2fe32e 100644 --- a/src/Contracts/RouteTranslator.php +++ b/src/Contracts/RouteTranslator.php @@ -14,6 +14,7 @@ interface RouteTranslator | Getters & Setters | ----------------------------------------------------------------- */ + /** * Get current route. * @@ -41,6 +42,7 @@ public function getTranslatedRoutes(); | Main Methods | ----------------------------------------------------------------- */ + /** * Translate routes and save them to the translated routes array (used in the localize route filter). * @@ -104,6 +106,7 @@ public function getUrlFromRouteName( | Check Methods | ----------------------------------------------------------------- */ + /** * Check if has current route. * diff --git a/src/Contracts/Url.php b/src/Contracts/Url.php index 03a73a5..9ca0831 100644 --- a/src/Contracts/Url.php +++ b/src/Contracts/Url.php @@ -12,6 +12,7 @@ interface Url | Main Methods | ----------------------------------------------------------------- */ + /** * Extract attributes for current url. * diff --git a/src/Entities/Locale.php b/src/Entities/Locale.php index 8e4d437..44247fd 100644 --- a/src/Entities/Locale.php +++ b/src/Entities/Locale.php @@ -16,6 +16,7 @@ class Locale implements Arrayable, Jsonable, JsonSerializable | Properties | ----------------------------------------------------------------- */ + /** * Locale key. * @@ -69,6 +70,7 @@ class Locale implements Arrayable, Jsonable, JsonSerializable | Constructor | ----------------------------------------------------------------- */ + /** * Create Locale instance. * @@ -89,6 +91,7 @@ public function __construct($key, array $data) | Getters & Setters | ----------------------------------------------------------------- */ + /** * Get local key. * @@ -268,6 +271,7 @@ private function setDefault() | Main Methods | ----------------------------------------------------------------- */ + /** * Create Locale instance. * @@ -285,6 +289,7 @@ public static function make($key, array $data) | Other Methods | ----------------------------------------------------------------- */ + /** * Get the locale entity as an array. * diff --git a/src/Entities/LocaleCollection.php b/src/Entities/LocaleCollection.php index d2b68e2..332be0f 100644 --- a/src/Entities/LocaleCollection.php +++ b/src/Entities/LocaleCollection.php @@ -14,6 +14,7 @@ class LocaleCollection extends Collection | Properties | ----------------------------------------------------------------- */ + /** * Supported locales. * @@ -25,6 +26,7 @@ class LocaleCollection extends Collection | Getters & Setters | ----------------------------------------------------------------- */ + /** * Set supported locales keys. * @@ -43,13 +45,14 @@ public function setSupportedKeys(array $supported) | Main Methods | ----------------------------------------------------------------- */ + /** * Get the first locale from the collection. * * @param callable|null $callback * @param mixed $default * - * @return Locale|mixed + * @return \Arcanedev\Localization\Entities\Locale|mixed */ public function first(callable $callback = null, $default = null) { diff --git a/src/Localization.php b/src/Localization.php index f5174a6..f0b3224 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -21,6 +21,7 @@ class Localization implements LocalizationContract | Properties | ----------------------------------------------------------------- */ + /** * Base url. * @@ -53,6 +54,7 @@ class Localization implements LocalizationContract | Constructor | ----------------------------------------------------------------- */ + /** * Localization constructor. * @@ -78,6 +80,7 @@ public function __construct( | Getters & Setters | ----------------------------------------------------------------- */ + /** * Get Request instance. * @@ -244,6 +247,7 @@ public function setBaseUrl($url) | Main Methods | ----------------------------------------------------------------- */ + /** * Translate routes and save them to the translated routes array (used in the localize route filter). * @@ -374,15 +378,18 @@ public function createUrlFromUri($uri) */ public function localesNavbar() { - return $this->app[ViewFactoryContract::class] - ->make('localization::navbar', ['supportedLocales' => $this->getSupportedLocales()]) - ->render(); + /** @var \Illuminate\Contracts\View\Factory $view */ + $view = $this->app[ViewFactoryContract::class]; + + return $view->make('localization::navbar', ['supportedLocales' => $this->getSupportedLocales()]) + ->render(); } /* ----------------------------------------------------------------- | Translation Methods | ----------------------------------------------------------------- */ + /** * Returns the translated route for an url and the attributes given and a locale * @@ -452,6 +459,7 @@ public function setRouteNameFromRequest(Request $request) | Check Methods | ----------------------------------------------------------------- */ + /** * Hide the default locale in URL ?? * diff --git a/src/LocalizationServiceProvider.php b/src/LocalizationServiceProvider.php index f991909..ce8f097 100644 --- a/src/LocalizationServiceProvider.php +++ b/src/LocalizationServiceProvider.php @@ -10,10 +10,11 @@ */ class LocalizationServiceProvider extends PackageServiceProvider { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Package name. * @@ -21,10 +22,11 @@ class LocalizationServiceProvider extends PackageServiceProvider */ protected $package = 'localization'; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + /** * Register the service provider. */ @@ -61,21 +63,20 @@ public function provides() { return [ Contracts\Localization::class, - 'arcanedev.localization', ]; } - /* ------------------------------------------------------------------------------------------------ - | Services Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Services Methods + | ----------------------------------------------------------------- */ + /** * Register Localization. */ private function registerLocalization() { $this->singleton(Contracts\Localization::class, Localization::class); - $this->singleton('arcanedev.localization', Contracts\Localization::class); $this->alias( $this->config()->get('localization.facade', 'Localization'), diff --git a/src/Middleware/LocaleCookieRedirect.php b/src/Middleware/LocaleCookieRedirect.php index 9cf3e4c..ae1e844 100644 --- a/src/Middleware/LocaleCookieRedirect.php +++ b/src/Middleware/LocaleCookieRedirect.php @@ -15,6 +15,7 @@ class LocaleCookieRedirect extends Middleware | Main Methods | ----------------------------------------------------------------- */ + /** * Handle an incoming request. * diff --git a/src/Middleware/LocaleSessionRedirect.php b/src/Middleware/LocaleSessionRedirect.php index a4d5af6..1572658 100644 --- a/src/Middleware/LocaleSessionRedirect.php +++ b/src/Middleware/LocaleSessionRedirect.php @@ -17,6 +17,7 @@ class LocaleSessionRedirect extends Middleware | Main Methods | ----------------------------------------------------------------- */ + /** * Handle an incoming request. * diff --git a/src/Middleware/LocalizationRedirect.php b/src/Middleware/LocalizationRedirect.php index 426f8fc..5b97f48 100644 --- a/src/Middleware/LocalizationRedirect.php +++ b/src/Middleware/LocalizationRedirect.php @@ -15,6 +15,7 @@ class LocalizationRedirect extends Middleware | Main Methods | ----------------------------------------------------------------- */ + /** * Handle an incoming request. * @@ -42,6 +43,7 @@ public function handle(Request $request, Closure $next) | Other Methods | ----------------------------------------------------------------- */ + /** * Get redirection. * diff --git a/src/Middleware/LocalizationRoutes.php b/src/Middleware/LocalizationRoutes.php index 0082ec8..56b0206 100644 --- a/src/Middleware/LocalizationRoutes.php +++ b/src/Middleware/LocalizationRoutes.php @@ -15,6 +15,7 @@ class LocalizationRoutes extends Middleware | Main Methods | ----------------------------------------------------------------- */ + /** * Handle an incoming request. * diff --git a/src/Middleware/Middleware.php b/src/Middleware/Middleware.php index 370725b..d9d2c28 100644 --- a/src/Middleware/Middleware.php +++ b/src/Middleware/Middleware.php @@ -18,6 +18,7 @@ abstract class Middleware extends BaseMiddleware | Properties | ----------------------------------------------------------------- */ + /** * The localization instance. * @@ -36,6 +37,7 @@ abstract class Middleware extends BaseMiddleware | Constructor | ----------------------------------------------------------------- */ + /** * Middleware constructor. * @@ -97,6 +99,7 @@ protected function hideDefaultLocaleInURL() | Check Methods | ----------------------------------------------------------------- */ + /** * Check is default locale hidden. * @@ -133,6 +136,7 @@ protected function shouldIgnore($request) | Other Methods | ----------------------------------------------------------------- */ + /** * Get the redirection response. * diff --git a/src/Middleware/TranslationRedirect.php b/src/Middleware/TranslationRedirect.php index 3b279eb..20b6a6c 100644 --- a/src/Middleware/TranslationRedirect.php +++ b/src/Middleware/TranslationRedirect.php @@ -16,12 +16,14 @@ class TranslationRedirect extends Middleware | Constants | ----------------------------------------------------------------- */ + const EVENT_NAME = 'routes.translation'; /* ----------------------------------------------------------------- | Main Methods | ----------------------------------------------------------------- */ + /** * Handle an incoming request. * @@ -44,6 +46,7 @@ public function handle(Request $request, Closure $next) | Other Methods | ----------------------------------------------------------------- */ + /** * Get translated URL. * diff --git a/src/Providers/RoutingServiceProvider.php b/src/Providers/RoutingServiceProvider.php index 9d46d4f..b39bc38 100644 --- a/src/Providers/RoutingServiceProvider.php +++ b/src/Providers/RoutingServiceProvider.php @@ -16,6 +16,7 @@ class RoutingServiceProvider extends ServiceProvider | Properties | ----------------------------------------------------------------- */ + /** * The application's route middleware. * @@ -31,31 +32,20 @@ class RoutingServiceProvider extends ServiceProvider 'translation-redirect' => Middleware\TranslationRedirect::class, ]; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + /** * Register the service provider. */ public function register() - { - $this->registerRouter(); - - parent::register(); - } - - /* ------------------------------------------------------------------------------------------------ - | Router Functions - | ------------------------------------------------------------------------------------------------ - */ - /** - * Register the router instance. - */ - protected function registerRouter() { $this->app->singleton('router', function ($app) { return new Router($app['events'], $app); }); + + parent::register(); } } diff --git a/src/Providers/UtilitiesServiceProvider.php b/src/Providers/UtilitiesServiceProvider.php index b7557ff..80c1af1 100644 --- a/src/Providers/UtilitiesServiceProvider.php +++ b/src/Providers/UtilitiesServiceProvider.php @@ -16,15 +16,18 @@ */ class UtilitiesServiceProvider extends ServiceProvider { - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + /** * Register the service provider. */ public function register() { + parent::register(); + $this->registerRouteTranslator(); $this->registerLocalesManager(); $this->registerLocaleNegotiator(); @@ -39,18 +42,16 @@ public function provides() { return [ RouteTranslatorContract::class, - 'arcanedev.localization.translator', LocalesManagerContract::class, - 'arcanedev.localization.locales-manager', NegotiatorContract::class, - 'arcanedev.localization.negotiator', ]; } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Utilities - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Register RouteTranslator utility. */ @@ -59,7 +60,6 @@ private function registerRouteTranslator() $this->singleton(RouteTranslatorContract::class, function ($app) { return new RouteTranslator($app['translator']); }); - $this->singleton('arcanedev.localization.translator', RouteTranslatorContract::class); } /** @@ -68,7 +68,6 @@ private function registerRouteTranslator() private function registerLocalesManager() { $this->singleton(LocalesManagerContract::class, LocalesManager::class); - $this->singleton('arcanedev.localization.locales-manager', LocalesManagerContract::class); } /** @@ -85,6 +84,5 @@ private function registerLocaleNegotiator() $manager->getSupportedLocales() ); }); - $this->bind('arcanedev.localization.negotiator', NegotiatorContract::class); } } diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 3e4de27..d4bc555 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -11,10 +11,11 @@ */ class Router extends IlluminateRouter { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Get active middlewares. * @@ -27,10 +28,11 @@ protected function getActiveMiddlewares() )); } - /* ------------------------------------------------------------------------------------------------ - | Route Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Route Methods + | ----------------------------------------------------------------- */ + /** * Create a route group with shared attributes. * @@ -156,6 +158,7 @@ public function transAny($trans, $action) | Other Methods | ----------------------------------------------------------------- */ + /** * Translate the route. * diff --git a/src/Traits/LocalizationKernelTrait.php b/src/Traits/LocalizationKernelTrait.php index 58fd549..8ecfcad 100644 --- a/src/Traits/LocalizationKernelTrait.php +++ b/src/Traits/LocalizationKernelTrait.php @@ -13,6 +13,11 @@ */ trait LocalizationKernelTrait { + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + /** * Get the route dispatcher callback. * diff --git a/src/Utilities/LocalesManager.php b/src/Utilities/LocalesManager.php index 09de552..d165a01 100644 --- a/src/Utilities/LocalesManager.php +++ b/src/Utilities/LocalesManager.php @@ -19,6 +19,7 @@ class LocalesManager implements LocalesManagerContract | Properties | ----------------------------------------------------------------- */ + /** * Default locale. * @@ -54,6 +55,7 @@ class LocalesManager implements LocalesManagerContract | Constructor | ----------------------------------------------------------------- */ + /** * LocalesManager constructor. * @@ -82,6 +84,7 @@ private function load() | Getters & Setters | ----------------------------------------------------------------- */ + /** * Set and return current locale. * @@ -281,6 +284,7 @@ private function negotiateLocale() | Methods Functions | ----------------------------------------------------------------- */ + /** * Check if default is supported. * @@ -348,6 +352,7 @@ public function getCurrentOrDefaultLocale() | Other Methods | ----------------------------------------------------------------- */ + /** * Filter locale collection. * diff --git a/src/Utilities/Negotiator.php b/src/Utilities/Negotiator.php index b6e3b1b..64d9d3b 100644 --- a/src/Utilities/Negotiator.php +++ b/src/Utilities/Negotiator.php @@ -28,6 +28,7 @@ class Negotiator implements NegotiatorContract | Properties | ----------------------------------------------------------------- */ + /** * Default Locale. * @@ -53,6 +54,7 @@ class Negotiator implements NegotiatorContract | Constructor | ----------------------------------------------------------------- */ + /** * Make Negotiator instance. * @@ -69,6 +71,7 @@ public function __construct($defaultLocale, LocaleCollection $supportedLanguages | Main Methods | ----------------------------------------------------------------- */ + /** * Make Negotiator instance. * @@ -165,10 +168,11 @@ private function getFromRemoteHostServer() return $this->isSupported($locale) ? $locale : null; } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Check Methods - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Check if matches a supported locale. * @@ -203,10 +207,11 @@ private function isSupported($locale) return $this->supportedLocales->has($locale); } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ + /** * Return all the accepted languages from the browser * diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index 65a4c58..3747f17 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -17,6 +17,7 @@ class RouteTranslator implements RouteTranslatorContract | Properties | ----------------------------------------------------------------- */ + /** * The translator instance. * @@ -42,6 +43,7 @@ class RouteTranslator implements RouteTranslatorContract | Constructor | ----------------------------------------------------------------- */ + /** * Create RouteTranslator instance. * @@ -56,6 +58,7 @@ public function __construct(Translator $translator) | Getters & Setters | ----------------------------------------------------------------- */ + /** * Get current route. * @@ -95,6 +98,7 @@ public function getTranslatedRoutes() | Main Methods | ----------------------------------------------------------------- */ + /** * Translate routes and save them to the translated routes array (used in the localize route filter). * @@ -248,6 +252,7 @@ private function translate($key, $locale = null) | Check Methods | ----------------------------------------------------------------- */ + /** * Check if has current route. * diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index a0e75cf..c529043 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -18,6 +18,7 @@ class Url implements UrlContract | Main Methods | ----------------------------------------------------------------- */ + /** * Extract attributes for current url. * @@ -55,7 +56,7 @@ public static function substituteAttributes(array $attributes, $uri) if ($value instanceof RouteBindable) $value = $value->getWildcardValue(); - $uri = str_replace(['{' . $key . '?}', '{' . $key . '}'], $value, $uri); + $uri = str_replace(['{'.$key.'?}', '{'.$key.'}'], $value, $uri); } // delete empty optional arguments that are not in the $attributes array @@ -73,7 +74,7 @@ public static function unparse($parsed) { if (empty($parsed)) return ''; - self::checkParsedUrl($parsed); + $parsed = self::checkParsedUrl($parsed); $url = self::getUrl($parsed); $url .= self::getQuery($parsed); @@ -86,6 +87,7 @@ public static function unparse($parsed) | Extract Methods | ----------------------------------------------------------------- */ + /** * Extract attributes from routes. * @@ -186,12 +188,15 @@ private static function extractAttributesFromSegment($url, $path, $i, $j, $segme | Other Methods | ----------------------------------------------------------------- */ + /** * Check parsed URL. * * @param array $parsed + * + * @return array */ - private static function checkParsedUrl(array &$parsed) + private static function checkParsedUrl(array $parsed) { $scheme =& $parsed['scheme']; $user =& $parsed['user']; @@ -203,7 +208,7 @@ private static function checkParsedUrl(array &$parsed) $query =& $parsed['query']; $fragment =& $parsed['fragment']; - $parsed = compact( + return compact( 'scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment' ); } @@ -217,9 +222,7 @@ private static function checkParsedUrl(array &$parsed) */ private static function getUrl(array $parsed) { - return strlen($parsed['scheme']) - ? $parsed['scheme'].':'.self::getHierPart($parsed) - : ''; + return strlen($parsed['scheme']) ? $parsed['scheme'].':'.self::getHierPart($parsed) : ''; } /** @@ -247,9 +250,7 @@ private static function getAuthority(array $parsed) { $host = self::getHost($parsed); - return strlen($userInfo = self::getUserInfo($parsed)) - ? $userInfo.'@'.$host - : $host; + return strlen($userInfo = self::getUserInfo($parsed)) ? $userInfo.'@'.$host : $host; } /** @@ -261,9 +262,7 @@ private static function getAuthority(array $parsed) */ private static function getUserInfo(array $parsed) { - return strlen($parsed['pass']) - ? $parsed['user'].':'.$parsed['pass'] - : ''; + return strlen($parsed['pass']) ? $parsed['user'].':'.$parsed['pass'] : ''; } /** @@ -301,8 +300,6 @@ private static function getQuery(array $parsed) */ private static function getFragment(array $parsed) { - return strlen($parsed['fragment']) - ? '#'.$parsed['fragment'] - : ''; + return strlen($parsed['fragment']) ? '#'.$parsed['fragment'] : ''; } } diff --git a/tests/LocalizationServiceProviderTest.php b/tests/LocalizationServiceProviderTest.php index ea22ad4..c3c2df1 100644 --- a/tests/LocalizationServiceProviderTest.php +++ b/tests/LocalizationServiceProviderTest.php @@ -10,17 +10,19 @@ */ class LocalizationServiceProviderTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** @var \Arcanedev\Localization\LocalizationServiceProvider */ private $provider; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + public function setUp() { parent::setUp(); @@ -35,10 +37,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { @@ -59,7 +62,6 @@ public function it_can_provides() { $expected = [ \Arcanedev\Localization\Contracts\Localization::class, - 'arcanedev.localization', ]; $this->assertSame($expected, $this->provider->provides()); diff --git a/tests/LocalizationTest.php b/tests/LocalizationTest.php index f7971db..8693337 100644 --- a/tests/LocalizationTest.php +++ b/tests/LocalizationTest.php @@ -11,10 +11,11 @@ */ class LocalizationTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { @@ -33,8 +34,8 @@ public function it_must_throw_unsupported_locale_exception_on_default_locale() new Localization( $this->app, - $this->app['arcanedev.localization.translator'], - $this->app['arcanedev.localization.locales-manager'] + $this->app[\Arcanedev\Localization\Contracts\RouteTranslator::class], + $this->app[\Arcanedev\Localization\Contracts\LocalesManager::class] ); } @@ -96,7 +97,6 @@ public function it_can_set_locale() $this->assertSame(route('about'), 'http://localhost/acerca'); $this->refreshApplication(); - $this->localization = app('arcanedev.localization'); $this->assertSame('en', localization()->setLocale('en')); $this->assertSame(route('about'), 'http://localhost/about'); @@ -537,10 +537,11 @@ public function it_can_use_facade() ); } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ + /** * Make a call. * diff --git a/tests/Stubs/User.php b/tests/Stubs/User.php index 567be08..224e9bd 100644 --- a/tests/Stubs/User.php +++ b/tests/Stubs/User.php @@ -10,10 +10,11 @@ */ class User implements RouteBindable { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * User's username. * @@ -21,10 +22,11 @@ class User implements RouteBindable */ public $username; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Constructor - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * User constructor. * @@ -35,10 +37,11 @@ public function __construct($username) $this->username = $username; } - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Get the wildcard value from the class. * diff --git a/tests/TestCase.php b/tests/TestCase.php index 8518127..cb24da2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,10 +10,11 @@ */ abstract class TestCase extends BaseTestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** @var string */ protected $defaultLocale = 'en'; @@ -29,10 +30,11 @@ abstract class TestCase extends BaseTestCase /** @var Stubs\Http\RouteRegistrar */ protected $routeRegistrar; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + /** * Get package providers. * @@ -87,7 +89,7 @@ protected function getEnvironmentSetUp($app) */ $config = $app['config']; $translator = $app['translator']; - $localization = $app['arcanedev.localization']; + $localization = $app[\Arcanedev\Localization\Contracts\Localization::class]; $config->set('app.url', $this->testUrlOne); $config->set('app.locale', $this->defaultLocale); @@ -135,6 +137,11 @@ protected function refreshApplication($locale = false, $session = false, $cookie $this->setRoutes($locale); } + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- + */ + /** * Set routes for testing * diff --git a/tests/Utilities/LocalesManagerTest.php b/tests/Utilities/LocalesManagerTest.php index 96ded6a..6189cbe 100644 --- a/tests/Utilities/LocalesManagerTest.php +++ b/tests/Utilities/LocalesManagerTest.php @@ -12,22 +12,24 @@ */ class LocalesManagerTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ - /** @var LocalesManager */ + + /** @var \Arcanedev\Localization\Contracts\LocalesManager */ private $localesManager; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + public function setUp() { parent::setUp(); - $this->localesManager = app('arcanedev.localization.locales-manager'); + $this->localesManager = app(\Arcanedev\Localization\Contracts\LocalesManager::class); $this->localesManager->setCurrentLocale('en'); } @@ -39,10 +41,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { diff --git a/tests/Utilities/NegotiatorTest.php b/tests/Utilities/NegotiatorTest.php index a967feb..7668147 100644 --- a/tests/Utilities/NegotiatorTest.php +++ b/tests/Utilities/NegotiatorTest.php @@ -13,22 +13,24 @@ */ class NegotiatorTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ - /** @var Negotiator */ + + /** @var \Arcanedev\Localization\Contracts\Negotiator */ private $negotiator; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + public function setUp() { parent::setUp(); - $this->negotiator = app('arcanedev.localization.negotiator'); + $this->negotiator = app(\Arcanedev\Localization\Contracts\Negotiator::class); } public function tearDown() @@ -38,10 +40,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { @@ -126,10 +129,11 @@ public function it_can_negotiate_undefined_remote_host_server() $this->assertSame('en', $this->negotiator->negotiate($request)); } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ + /** * Mock request. * diff --git a/tests/Utilities/UrlTest.php b/tests/Utilities/UrlTest.php index 573c614..f34d413 100644 --- a/tests/Utilities/UrlTest.php +++ b/tests/Utilities/UrlTest.php @@ -11,10 +11,11 @@ */ class UrlTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_unparse_url() { From 2b8d8eeb57eea28a98bc1199fbc408b5afe755ef Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 27 Apr 2017 14:07:09 +0100 Subject: [PATCH 3/4] Updating the doc comments --- tests/Entities/LocaleCollectionTest.php | 21 ++++--- tests/Entities/LocaleTest.php | 33 +++++----- tests/Middleware/LocaleCookieRedirectTest.php | 7 ++- .../Middleware/LocaleSessionRedirectTest.php | 7 ++- tests/Routing/RouterTest.php | 21 ++++--- tests/Stubs/Http/Kernel.php | 10 +-- .../Stubs/Http/Middleware/EncryptCookies.php | 5 ++ tests/Stubs/Http/RouteRegistrar.php | 63 +++++++++---------- 8 files changed, 87 insertions(+), 80 deletions(-) diff --git a/tests/Entities/LocaleCollectionTest.php b/tests/Entities/LocaleCollectionTest.php index cc1ee70..d00dc00 100644 --- a/tests/Entities/LocaleCollectionTest.php +++ b/tests/Entities/LocaleCollectionTest.php @@ -12,17 +12,19 @@ */ class LocaleCollectionTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ - /** @var LocaleCollection */ + + /** @var \Arcanedev\Localization\Entities\LocaleCollection */ private $locales; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + public function setUp() { parent::setUp(); @@ -37,10 +39,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { diff --git a/tests/Entities/LocaleTest.php b/tests/Entities/LocaleTest.php index 83b3921..abd567b 100644 --- a/tests/Entities/LocaleTest.php +++ b/tests/Entities/LocaleTest.php @@ -11,21 +11,18 @@ */ class LocaleTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ - /** @var Locale */ + + /** @var \Arcanedev\Localization\Entities\Locale */ private $locale; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ - public function setUp() - { - parent::setUp(); - } public function tearDown() { @@ -34,10 +31,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Test Methods + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { @@ -94,16 +92,17 @@ public function it_can_convert_entity_to_json() $this->assertJson(json_encode($this->locale, JSON_PRETTY_PRINT)); } - /* ------------------------------------------------------------------------------------------------ - | Other Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Other Methods + | ----------------------------------------------------------------- */ + /** * Make a locale. * * @param string $key * - * @return Locale + * @return \Arcanedev\Localization\Entities\Locale */ private function makeLocale($key) { diff --git a/tests/Middleware/LocaleCookieRedirectTest.php b/tests/Middleware/LocaleCookieRedirectTest.php index 306b897..b12e5f2 100644 --- a/tests/Middleware/LocaleCookieRedirectTest.php +++ b/tests/Middleware/LocaleCookieRedirectTest.php @@ -12,10 +12,11 @@ */ class LocaleCookieRedirectTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_redirect_with_locale_cookie() { diff --git a/tests/Middleware/LocaleSessionRedirectTest.php b/tests/Middleware/LocaleSessionRedirectTest.php index 8b1694d..06e1a1e 100644 --- a/tests/Middleware/LocaleSessionRedirectTest.php +++ b/tests/Middleware/LocaleSessionRedirectTest.php @@ -12,10 +12,11 @@ */ class LocaleSessionRedirectTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_redirect_with_locale_session() { diff --git a/tests/Routing/RouterTest.php b/tests/Routing/RouterTest.php index d8309b6..ffa7928 100644 --- a/tests/Routing/RouterTest.php +++ b/tests/Routing/RouterTest.php @@ -11,17 +11,19 @@ */ class RouterTest extends TestCase { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ - /** @var Router */ + + /** @var \Arcanedev\Localization\Routing\Router */ private $router; - /* ------------------------------------------------------------------------------------------------ - | Main Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- */ + public function setUp() { parent::setUp(); @@ -36,10 +38,11 @@ public function tearDown() parent::tearDown(); } - /* ------------------------------------------------------------------------------------------------ - | Test Functions - | ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- */ + /** @test */ public function it_can_be_instantiated() { diff --git a/tests/Stubs/Http/Kernel.php b/tests/Stubs/Http/Kernel.php index 35974e3..926ef7f 100644 --- a/tests/Stubs/Http/Kernel.php +++ b/tests/Stubs/Http/Kernel.php @@ -11,16 +11,18 @@ */ class Kernel extends HttpKernel { - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Traits - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + use LocalizationKernelTrait; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Properties - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * The application's route middleware groups. * diff --git a/tests/Stubs/Http/Middleware/EncryptCookies.php b/tests/Stubs/Http/Middleware/EncryptCookies.php index 27d36a4..827defb 100644 --- a/tests/Stubs/Http/Middleware/EncryptCookies.php +++ b/tests/Stubs/Http/Middleware/EncryptCookies.php @@ -10,6 +10,11 @@ */ class EncryptCookies extends BaseEncrypter { + /* ----------------------------------------------------------------- + | Properties + | ----------------------------------------------------------------- + */ + /** * The names of the cookies that should not be encrypted. * diff --git a/tests/Stubs/Http/RouteRegistrar.php b/tests/Stubs/Http/RouteRegistrar.php index 31e353b..eb26eb3 100644 --- a/tests/Stubs/Http/RouteRegistrar.php +++ b/tests/Stubs/Http/RouteRegistrar.php @@ -10,6 +10,11 @@ */ class RouteRegistrar { + /* ----------------------------------------------------------------- + | Properties + | ----------------------------------------------------------------- + */ + /** * @var Router */ @@ -18,10 +23,11 @@ class RouteRegistrar /** @var array */ protected $routeNames = []; - /* ------------------------------------------------------------------------------------------------ + /* ----------------------------------------------------------------- | Getters & Setters - | ------------------------------------------------------------------------------------------------ + | ----------------------------------------------------------------- */ + /** * Set the router. * @@ -82,6 +88,7 @@ private function setRouteName($name) | Main Functions | ------------------------------------------------------------------------------------------------ */ + /** * Map the routes. * @@ -94,7 +101,7 @@ public function map(Router $router) $this->router->group(['middleware' => 'web'], function () { $this->router->localizedGroup(function () { $this->router->get('/', [ - 'as' => 'index', + 'as' => 'index', function () { return app('translator')->get('localization::routes.hello'); } @@ -102,7 +109,7 @@ function () { $this->setRouteName('index'); $this->router->get('test', [ - 'as' => 'test', + 'as' => 'test', function () { return app('translator')->get('localization::routes.test-text'); } @@ -110,7 +117,7 @@ function () { $this->setRouteName('test'); $this->router->transGet('localization::routes.about', [ - 'as' => 'about', + 'as' => 'about', function () { return localization()->getLocalizedURL('es') ?: 'Not url available'; } @@ -118,7 +125,7 @@ function () { $this->setRouteName('about'); $this->router->transGet('localization::routes.view', [ - 'as' => 'view', + 'as' => 'view', function () { return localization()->getLocalizedURL('es') ?: 'Not url available'; } @@ -126,19 +133,17 @@ function () { $this->setRouteName('view'); $this->router->transGet('localization::routes.view-project', [ - 'as' => 'view-project', + 'as' => 'view-project', function () { return localization()->getLocalizedURL('es') ?: 'Not url available'; } ]); $this->setRouteName('view-project'); - /* ------------------------------------------------------------------------------------------------ - | Other method - | ------------------------------------------------------------------------------------------------ - */ + // Other methods // + $this->router->transPost('localization::routes.methods.post', [ - 'as' => 'method.post', + 'as' => 'method.post', function () { return 'POST method'; } @@ -146,7 +151,7 @@ function () { $this->setRouteName('method.post'); $this->router->transPut('localization::routes.methods.put', [ - 'as' => 'method.put', + 'as' => 'method.put', function () { return 'PUT method'; } @@ -154,7 +159,7 @@ function () { $this->setRouteName('method.put'); $this->router->transPatch('localization::routes.methods.patch', [ - 'as' => 'method.patch', + 'as' => 'method.patch', function () { return 'PATCH method'; } @@ -162,7 +167,7 @@ function () { $this->setRouteName('method.patch'); $this->router->transOptions('localization::routes.methods.options', [ - 'as' => 'method.options', + 'as' => 'method.options', function () { return 'OPTIONS method'; } @@ -170,7 +175,7 @@ function () { $this->setRouteName('method.options'); $this->router->transDelete('localization::routes.methods.delete', [ - 'as' => 'method.delete', + 'as' => 'method.delete', function () { return 'DELETE method'; } @@ -178,26 +183,19 @@ function () { $this->setRouteName('method.delete'); $this->router->transAny('localization::routes.methods.any', [ - 'as' => 'method.any', + 'as' => 'method.any', function () { return 'Any method'; } ]); $this->setRouteName('method.any'); - /* ------------------------------------------------------------------------------------------------ - | Resource Controller - | ------------------------------------------------------------------------------------------------ - */ + // Resource Controllers // + $this->router->resource('dummy', Controllers\DummyController::class); $this->setRouteNames([ - 'dummy.index', - 'dummy.create', - 'dummy.store', - 'dummy.show', - 'dummy.edit', - 'dummy.update', - 'dummy.destroy', + 'dummy.index', 'dummy.create', 'dummy.store', 'dummy.show', + 'dummy.edit', 'dummy.update', 'dummy.destroy', ]); $this->router->group(['prefix' => 'foo'], function () { @@ -205,13 +203,8 @@ function () { }); $this->setRouteNames([ - 'bar.index', - 'bar.create', - 'bar.store', - 'bar.show', - 'bar.edit', - 'bar.update', - 'bar.destroy', + 'bar.index', 'bar.create', 'bar.store', 'bar.show', + 'bar.edit', 'bar.update', 'bar.destroy', ]); }); }); From 08b47fdb4bab6495647ed993d892f3f75f3c997a Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 27 Apr 2017 14:45:59 +0100 Subject: [PATCH 4/4] Adding the ability to force show on hidden default locale in URL --- _docs/3-Usage.md | 27 ++---- src/Contracts/Localization.php | 12 +-- src/Contracts/RouteTranslator.php | 3 +- src/Localization.php | 35 ++++---- src/Utilities/RouteTranslator.php | 5 +- tests/LocalizationTest.php | 132 ++++++++++++++++++++++-------- 6 files changed, 139 insertions(+), 75 deletions(-) diff --git a/_docs/3-Usage.md b/_docs/3-Usage.md index dc5bbd6..fc1c1fb 100644 --- a/_docs/3-Usage.md +++ b/_docs/3-Usage.md @@ -104,17 +104,14 @@ If you want to hide the default locale but always show other locales in the url, /** * Returns an URL adapted to $locale or current locale. * - * - * @param string|bool $locale - * @param string|false $url - * @param array $attributes + * @param string|null $locale + * @param string|null $url + * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false - * - * @throws UndefinedSupportedLocalesException - * @throws UnsupportedLocaleException */ -public function getLocalizedURL($locale = null, $url = null, $attributes = []) +public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false) // OR @@ -124,8 +121,6 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) * @param string $url * @param string|null $locale * - * @throws UnsupportedLocaleException - * * @return string */ public function localizeURL($url = null, $locale = null); @@ -157,13 +152,11 @@ It returns a clean URL of any localization. * @param string|bool $locale * @param string $transKey * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false - * - * @throws UndefinedSupportedLocalesException - * @throws UnsupportedLocaleException */ -public function getUrlFromRouteName($locale, $transKey, $attributes = []) +public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false) ``` It returns a route, localized to the desired locale using the locale passed. @@ -192,8 +185,6 @@ function localized_route($transRoute, array $attributes = [], $locale = null) * Return an array of all supported Locales. * * @return \Arcanedev\Localization\Entities\LocaleCollection - * - * @throws UndefinedSupportedLocalesException */ public function getSupportedLocales() ``` @@ -207,8 +198,6 @@ It returns all locales as a `Arcanedev\Localization\Entities\LocaleCollection` C * Get supported locales keys. * * @return array - * - * @throws UndefinedSupportedLocalesException */ public function getSupportedLocalesKeys() ``` @@ -370,8 +359,6 @@ public function setBaseUrl($url) * * @param string|bool $locale * - * @throws UndefinedSupportedLocalesException - * * @return bool */ public function isLocaleSupported($locale) diff --git a/src/Contracts/Localization.php b/src/Contracts/Localization.php index ae199c3..1f707e1 100644 --- a/src/Contracts/Localization.php +++ b/src/Contracts/Localization.php @@ -160,13 +160,14 @@ public function getNonLocalizedURL($url = null); /** * Returns an URL adapted to $locale. * - * @param string|bool $locale - * @param string|false $url - * @param array $attributes + * @param string|null $locale + * @param string|null $url + * @param array $attributes + * @param bool|bool $showHiddenLocale * * @return string|false */ - public function getLocalizedURL($locale = null, $url = null, $attributes = []); + public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false); /** * Create an url from the uri. @@ -187,10 +188,11 @@ public function createUrlFromUri($uri); * @param string|bool $locale * @param string $transKey * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getUrlFromRouteName($locale, $transKey, $attributes = []); + public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false); /* ----------------------------------------------------------------- | Check Methods diff --git a/src/Contracts/RouteTranslator.php b/src/Contracts/RouteTranslator.php index d2fe32e..f981b34 100644 --- a/src/Contracts/RouteTranslator.php +++ b/src/Contracts/RouteTranslator.php @@ -95,11 +95,12 @@ public function findTranslatedRouteByPath($path, $locale); * @param string $transKey * @param array $attributes * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ public function getUrlFromRouteName( - $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false + $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false, $showHiddenLocale = false ); /* ----------------------------------------------------------------- diff --git a/src/Localization.php b/src/Localization.php index f0b3224..3d54114 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -293,10 +293,11 @@ public function getNonLocalizedURL($url = null) * @param string|null $locale * @param string|null $url * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getLocalizedURL($locale = null, $url = null, $attributes = []) + public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false) { if (is_null($locale)) $locale = $this->getCurrentLocale(); @@ -314,7 +315,8 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) return $this->getUrlFromRouteName( $locale, $this->routeTranslator->getCurrentRoute(), - $attributes + $attributes, + $showHiddenLocale ); } @@ -325,7 +327,7 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) $locale && $translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->getCurrentLocale()) ) { - return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); + return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); } $baseUrl = $this->request()->getBaseUrl(); @@ -336,13 +338,12 @@ public function getLocalizedURL($locale = null, $url = null, $attributes = []) ); if ($translatedRoute !== false) - return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); + return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); - if ( - ! empty($locale) && - ($locale !== $this->getDefaultLocale() || ! $this->isDefaultLocaleHiddenInUrl()) - ) { - $parsedUrl['path'] = $locale . '/' . ltrim($parsedUrl['path'], '/'); + if ( ! empty($locale)) { + if ($locale !== $this->getDefaultLocale() || ! $this->isDefaultLocaleHiddenInUrl() || $showHiddenLocale) { + $parsedUrl['path'] = $locale.'/'.ltrim($parsedUrl['path'], '/'); + } } $parsedUrl['path'] = ltrim(ltrim($baseUrl, '/') . '/' . $parsedUrl['path'], '/'); @@ -368,7 +369,9 @@ public function createUrlFromUri($uri) { $uri = ltrim($uri, '/'); - return empty($this->baseUrl) ? $this->app['url']->to($uri) : $this->baseUrl.$uri; + return empty($this->baseUrl) + ? $this->app[\Illuminate\Contracts\Routing\UrlGenerator::class]->to($uri) + : $this->baseUrl.$uri; } /** @@ -417,13 +420,14 @@ private function findTranslatedRouteByUrl($url, $attributes, $locale) /** * Returns an URL adapted to the route name and the locale given. * - * @param string $locale - * @param string $transKey - * @param array $attributes + * @param string|bool $locale + * @param string $transKey + * @param array $attributes + * @param bool|false $showHiddenLocale * * @return string|false */ - public function getUrlFromRouteName($locale, $transKey, $attributes = []) + public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false) { $this->isLocaleSupportedOrFail($locale); @@ -432,7 +436,8 @@ public function getUrlFromRouteName($locale, $transKey, $attributes = []) $this->getDefaultLocale(), $transKey, $attributes, - $this->isDefaultLocaleHiddenInUrl() + $this->isDefaultLocaleHiddenInUrl(), + $showHiddenLocale ); // This locale does not have any key for this route name diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index 3747f17..941f7e9 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -201,18 +201,19 @@ public function findTranslatedRouteByPath($path, $locale) * @param string $transKey * @param array $attributes * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ public function getUrlFromRouteName( - $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false + $locale, $defaultLocale, $transKey, $attributes = [], $defaultHidden = false, $showHiddenLocale = false ) { if ( ! is_string($locale)) $locale = $defaultLocale; $url = ''; - if ( ! ($locale === $defaultLocale && $defaultHidden)) + if ( ! ($locale === $defaultLocale && $defaultHidden) || $showHiddenLocale) $url = '/'.$locale; if ($this->hasTranslation($transKey, $locale)) diff --git a/tests/LocalizationTest.php b/tests/LocalizationTest.php index 8693337..5c0d4b8 100644 --- a/tests/LocalizationTest.php +++ b/tests/LocalizationTest.php @@ -195,26 +195,6 @@ public function it_can_localize_url() /** @test */ public function it_can_get_localized_url() { - $this->assertSame( - $this->testUrlOne.'es/acerca', - localization()->getLocalizedURL('es', $this->testUrlOne.'en/about') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1/proyecto', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1/project') - ); - $this->assertSame( - $this->testUrlOne.'es/ver/1/proyecto/1', - localization()->getLocalizedURL('es', $this->testUrlOne.'view/1/project/1') - ); - $this->assertSame( - $this->testUrlOne.'en/about', - localization()->getLocalizedURL('en', $this->testUrlOne.'about') - ); $this->assertSame( $this->testUrlOne.localization()->getCurrentLocale(), localization()->getLocalizedURL() @@ -223,18 +203,7 @@ public function it_can_get_localized_url() app('config')->set('localization.hide-default-in-url', true); // testing default language hidden - $this->assertSame( - $this->testUrlOne.'es/acerca', - localization()->getLocalizedURL('es', $this->testUrlOne.'about') - ); - $this->assertSame( - $this->testUrlOne.'about', - localization()->getLocalizedURL('en', $this->testUrlOne.'about') - ); - $this->assertSame( - $this->testUrlOne, - localization()->getLocalizedURL() - ); + $this->assertNotEquals( $this->testUrlOne.localization()->getDefaultLocale(), localization()->getLocalizedURL() @@ -292,6 +261,27 @@ public function it_can_get_localized_url() ); } + /** + * @test + * + * @param bool $hideDefault + * @param bool $forceDefault + * @param string $locale + * @param string $url + * @param string $expected + * + * @dataProvider getLocalizedURLDataProvider + */ + public function it_can_get_localized_url_with_specific_format($hideDefault, $forceDefault, $locale, $url, $expected) + { + $this->app['config']->set('localization.hide-default-in-url', $hideDefault); + + $this->assertEquals( + $expected, + \localization()->getLocalizedURL($locale, $url, [], $forceDefault) + ); + } + /** @test */ public function it_can_get_url_from_route_name_translated() { @@ -554,4 +544,82 @@ public function makeCall($uri, array $server = []) { return $this->call('GET', $uri, [], [], [], $server); } + + /* ----------------------------------------------------------------- + | Providers + | ----------------------------------------------------------------- + */ + /** + * Provide data for `it_can_get_localized_url_with_specific_format`. + * + * @return array + */ + public function getLocalizedURLDataProvider() + { + $url = 'http://localhost/'; + + // [$hideDefault, $forceDefault, $locale, $url, $expected] + + return [ + // Do not hide default with [es] locale + [false, false, 'es', $url, $url.'es'], + [false, false, 'es', $url.'es', $url.'es'], + [false, false, 'es', $url.'en/about', $url.'es/acerca'], + [false, false, 'es', $url.'ver/1', $url.'es/ver/1'], + [false, false, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [false, false, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default with [en] locale + [false, false, 'en', $url.'en', $url.'en'], + [false, false, 'en', $url.'about', $url.'en/about'], + [false, false, 'en', $url.'ver/1', $url.'en/ver/1'], + [false, false, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [false, false, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + + // Hide default with [es] locale + [true, false, 'es', $url, $url.'es'], + [true, false, 'es', $url.'es', $url.'es'], + [true, false, 'es', $url.'en/about', $url.'es/acerca'], + [true, false, 'es', $url.'ver/1', $url.'es/ver/1'], + [true, false, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [true, false, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Hide default with [en] locale + [true, false, 'en', $url.'en', $url.''], + [true, false, 'en', $url.'about', $url.'about'], + [true, false, 'en', $url.'ver/1', $url.'ver/1'], + [true, false, 'en', $url.'view/1/project', $url.'view/1/project'], + [true, false, 'en', $url.'view/1/project/1', $url.'view/1/project/1'], + + // Do not hide default + forcing the show with [es] locale + [false, true, 'es', $url, $url.'es'], + [false, true, 'es', $url.'es', $url.'es'], + [false, true, 'es', $url.'en/about', $url.'es/acerca'], + [false, true, 'es', $url.'ver/1', $url.'es/ver/1'], + [false, true, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [false, true, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default + forcing the show with [en] locale + [false, true, 'en', $url.'en', $url.'en'], + [false, true, 'en', $url.'about', $url.'en/about'], + [false, true, 'en', $url.'ver/1', $url.'en/ver/1'], + [false, true, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [false, true, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + + // Do not hide default + forcing the show with [es] locale + [true, true, 'es', $url, $url.'es'], + [true, true, 'es', $url.'es', $url.'es'], + [true, true, 'es', $url.'en/about', $url.'es/acerca'], + [true, true, 'es', $url.'ver/1', $url.'es/ver/1'], + [true, true, 'es', $url.'view/1/project', $url.'es/ver/1/proyecto'], + [true, true, 'es', $url.'view/1/project/1', $url.'es/ver/1/proyecto/1'], + + // Do not hide default + forcing the show with [en] locale + [true, true, 'en', $url.'en', $url.'en'], + [true, true, 'en', $url.'about', $url.'en/about'], + [true, true, 'en', $url.'ver/1', $url.'en/ver/1'], + [true, true, 'en', $url.'view/1/project', $url.'en/view/1/project'], + [true, true, 'en', $url.'view/1/project/1', $url.'en/view/1/project/1'], + ]; + } }