diff --git a/.idea/composerJson.xml b/.idea/composerJson.xml new file mode 100644 index 0000000..4199499 --- /dev/null +++ b/.idea/composerJson.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 0b86cdb..ed0fc77 100644 --- a/README.md +++ b/README.md @@ -141,4 +141,4 @@ GET /galahad/addressing/countries?locale=pt ### Thanks! -Special thanks to [Commerce Guys](https://github.com/commerceguys) for their amazing [addressing](https://github.com/commerceguys/addressing) and [intl](https://github.com/commerceguys/intl) packages, which this project relies heavily on. \ No newline at end of file +Special thanks to [Commerce Guys](https://github.com/commerceguys) for their amazing [addressing](https://github.com/commerceguys/addressing) and [intl](https://github.com/commerceguys/intl) packages, which this project relies heavily on. diff --git a/src/Entity/Country.php b/src/Entity/Country.php index 7af993e..ec8f309 100644 --- a/src/Entity/Country.php +++ b/src/Entity/Country.php @@ -67,6 +67,7 @@ public function administrativeAreas() */ public function administrativeArea($code) { + $code = strtoupper($code); if (strpos($code, '-') === false) { $code = $this->getCountryCode().'-'.$code; } @@ -83,7 +84,7 @@ public function administrativeArea($code) public function administrativeAreaByName($administrativeAreaName) { foreach ($this->getAdministrativeAreasList() as $code => $name) { - if ($name == $administrativeAreaName) { + if (0 === strcasecmp($name, $administrativeAreaName)) { return $this->administrativeArea($code); } } @@ -97,12 +98,11 @@ public function administrativeAreaByName($administrativeAreaName) */ public function findAdministrativeArea($codeOrName) { - $administrativeArea = $this->administrativeArea($codeOrName); - if (! $administrativeArea instanceof AdministrativeArea) { - return $this->administrativeAreaByName($codeOrName); + if ($administrativeArea = $this->administrativeArea($codeOrName)) { + return $administrativeArea; } - - return $administrativeArea; + + return $this->administrativeAreaByName($codeOrName); } /** @@ -130,4 +130,4 @@ public function getLocale() { return $this->addressing->getLocale(); } -} \ No newline at end of file +} diff --git a/src/LaravelAddressing.php b/src/LaravelAddressing.php index 65e4e41..ae112f9 100644 --- a/src/LaravelAddressing.php +++ b/src/LaravelAddressing.php @@ -55,6 +55,7 @@ public function __construct($locale = 'en') */ public function country($countryCode) { + $countryCode = strtoupper($countryCode); return $this->getCountryRepository()->get($countryCode, $this->locale); } @@ -83,10 +84,13 @@ public function countryByName($countryName) */ public function findCountry($codeOrName) { + $code = strtoupper($codeOrName); $countryList = $this->getCountryList(); - if (isset($countryList[$codeOrName])) { - return $this->country($codeOrName); + + if (isset($countryList[$code])) { + return $this->country($code); } + return $this->countryByName($codeOrName); } @@ -163,4 +167,4 @@ public function getAdministrativeAreaRepository() return $this->administrativeAreaRepository; } -} \ No newline at end of file +} diff --git a/src/Repository/AdministrativeAreaRepository.php b/src/Repository/AdministrativeAreaRepository.php index 764d68a..be6c907 100644 --- a/src/Repository/AdministrativeAreaRepository.php +++ b/src/Repository/AdministrativeAreaRepository.php @@ -50,6 +50,7 @@ protected function createSubdivisionFromDefinitions($id, array $definitions, $lo */ public function getAll($countryCode, $parentId = null, $locale = null) { + $countryCode = strtoupper($countryCode); $subdivisions = parent::getAll($countryCode, $parentId, $locale); return new AdministrativeAreaCollection($subdivisions); @@ -66,4 +67,4 @@ public function get($id, $locale = null) { return parent::get($id, $locale); } -} \ No newline at end of file +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index d7aada5..3557c94 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -5,6 +5,7 @@ use Galahad\LaravelAddressing\Validator\AdministrativeAreaValidator; use Galahad\LaravelAddressing\Validator\CountryValidator; use Galahad\LaravelAddressing\Validator\PostalCodeValidator; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Validation\Factory; /** @@ -21,56 +22,89 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider */ public function boot() { - if (!$this->app->routesAreCached()) { - require_once __DIR__ . '/routes.php'; - } - $this->registerValidators($this->app->validator); + $this->bootRoutes(); $this->loadTranslationsFrom(__DIR__.'/../lang', 'laravel-addressing'); } + + /** + * Boot routes if routing is supported + */ + protected function bootRoutes() + { + if (method_exists($this->app, 'routesAreCached') && $this->app->routesAreCached()) { + return; + } + + try { + $route = $this->app->make('router'); + $route->group(['prefix' => 'galahad/addressing'], function($route) { + $route->get('/{country}/administrative-areas', [ + 'as' => 'galahad.addressing.administrative-areas', + 'uses' => '\\Galahad\\LaravelAddressing\\Controller@getAdministrativeAreas', + ]); + $route->get('/{country}/{administrativeArea}/cities', [ + 'as' => 'galahad.addressing.cities', + 'uses' => '\\Galahad\\LaravelAddressing\\Controller@getCities', + ]); + $route->get('/countries', [ + 'as' => 'galahad.addressing.countries', + 'uses' => '\\Galahad\\LaravelAddressing\\Controller@getCountries', + ]); + }); + } catch (BindingResolutionException $exception) { + // Skip routes if no router exists + } + } /** * Register the LaravelAddressing instance */ public function register() { - $this->app->singleton(LaravelAddressing::class, function ($app) { - return new LaravelAddressing(); - }); + $this->app->singleton(LaravelAddressing::class, function($app) { + return new LaravelAddressing( + $app->make('config')->get('app.locale', 'en') + ); + }); + + $this->registerValidators(); } /** * Register all custom validators - * - * @param Factory $validatorFactory */ - protected function registerValidators(Factory $validatorFactory) + protected function registerValidators() { - // Country validators - $validatorFactory->extend( - 'country_code', - CountryValidator::class.'@validateCountryCode' - ); - $validatorFactory->extend( - 'country_name', - CountryValidator::class.'@validateCountryName' - ); - // Administrative Area validators - $validatorFactory->extend( - 'administrative_area_code', - AdministrativeAreaValidator::class.'@validateAdministrativeAreaCode' - ); - $validatorFactory->extend( - 'administrative_area_name', - AdministrativeAreaValidator::class.'@validateAdministrativeAreaName' - ); - $validatorFactory->extend( - 'administrative_area', - AdministrativeAreaValidator::class.'@validateAdministrativeArea' - ); - // Postal Code validator - $validatorFactory->extend( - 'postal_code', - PostalCodeValidator::class.'@validatePostalCode' - ); + $this->app->resolving('validator', function($validator, $app) { + // Country validators + $validator->extend( + 'country_code', + CountryValidator::class.'@validateCountryCode' + ); + $validator->extend( + 'country_name', + CountryValidator::class.'@validateCountryName' + ); + + // Administrative Area validators + $validator->extend( + 'administrative_area_code', + AdministrativeAreaValidator::class.'@validateAdministrativeAreaCode' + ); + $validator->extend( + 'administrative_area_name', + AdministrativeAreaValidator::class.'@validateAdministrativeAreaName' + ); + $validator->extend( + 'administrative_area', + AdministrativeAreaValidator::class.'@validateAdministrativeArea' + ); + + // Postal Code validator + $validator->extend( + 'postal_code', + PostalCodeValidator::class.'@validatePostalCode' + ); + }); } } diff --git a/tests/LaravelAddressingTest.php b/tests/LaravelAddressingTest.php index 4bf3a3c..7c6d620 100644 --- a/tests/LaravelAddressingTest.php +++ b/tests/LaravelAddressingTest.php @@ -130,4 +130,13 @@ public function testFindAdministrativeArea() $alabama = $country->findAdministrativeArea('Alabama'); $this->assertEquals($alabama->getCode(), 'AL'); } -} \ No newline at end of file + + public function testIsCaseInsensitive() + { + $country = $this->addressing->country('us'); + $alabama = $country->findAdministrativeArea('al'); + $this->assertEquals($alabama->getName(), 'Alabama'); + $alabama = $country->findAdministrativeArea('alabama'); + $this->assertEquals($alabama->getCode(), 'AL'); + } +} diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 7ffe0dd..d931948 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -16,10 +16,16 @@ class TranslationTest extends TestCase */ protected $validator; + /** + * @var string + */ + protected $translationDirectory; + public function setUp() { parent::setUp(); $this->validator = $this->app['validator']; + $this->translationDirectory = __DIR__.'/../lang'; } protected function getPackageProviders($app) @@ -27,6 +33,16 @@ protected function getPackageProviders($app) return [ServiceProvider::class]; } + protected function getMessage($locale, $filename, $key, $field) + { + $filePath = sprintf('%s/%s/%s.php', $this->translationDirectory, $locale, $filename); + $arrayContent = include $filePath; + + if (isset($arrayContent[$key])) { + return str_replace(':attribute', $field, $arrayContent[$key]); + } + } + public function testENMessages() { $this->app->setLocale('en'); @@ -37,7 +53,8 @@ public function testENMessages() if ($validator->fails()) { $messages = $validator->errors()->get('country'); $first = array_shift($messages); - $this->assertEquals($first, 'The country field is not a valid country code.'); + $expectedMessage = $this->getMessage('en', 'validation', 'country_code', 'country'); + $this->assertEquals($first, $expectedMessage); } } @@ -51,7 +68,8 @@ public function testPT_BRMessages() if ($validator->fails()) { $messages = $validator->errors()->get('country'); $first = array_shift($messages); - $this->assertEquals($first, 'O campo country não possui um código de país válido.'); + $expectedMessage = $this->getMessage('pt-br', 'validation', 'country_code', 'country'); + $this->assertEquals($first, $expectedMessage); } } @@ -65,7 +83,8 @@ public function testENMessagesForAdministrativeAreaValidator() if ($validator->fails()) { $messages = $validator->errors()->get('state'); $first = array_shift($messages); - $this->assertEquals($first, 'The state field is not a valid state/province.'); + $expectedMessage = $this->getMessage('en', 'validation', 'administrative_area', 'state'); + $this->assertEquals($first, $expectedMessage); } } } \ No newline at end of file