Skip to content

Commit eb6dc21

Browse files
authored
Merge pull request #2 from glhd/development
0.4.3
2 parents c95cc94 + e4d3e48 commit eb6dc21

File tree

8 files changed

+129
-53
lines changed

8 files changed

+129
-53
lines changed

.idea/composerJson.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ GET /galahad/addressing/countries?locale=pt
141141

142142
### Thanks!
143143

144-
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.
144+
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.

src/Entity/Country.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function administrativeAreas()
6767
*/
6868
public function administrativeArea($code)
6969
{
70+
$code = strtoupper($code);
7071
if (strpos($code, '-') === false) {
7172
$code = $this->getCountryCode().'-'.$code;
7273
}
@@ -83,7 +84,7 @@ public function administrativeArea($code)
8384
public function administrativeAreaByName($administrativeAreaName)
8485
{
8586
foreach ($this->getAdministrativeAreasList() as $code => $name) {
86-
if ($name == $administrativeAreaName) {
87+
if (0 === strcasecmp($name, $administrativeAreaName)) {
8788
return $this->administrativeArea($code);
8889
}
8990
}
@@ -97,12 +98,11 @@ public function administrativeAreaByName($administrativeAreaName)
9798
*/
9899
public function findAdministrativeArea($codeOrName)
99100
{
100-
$administrativeArea = $this->administrativeArea($codeOrName);
101-
if (! $administrativeArea instanceof AdministrativeArea) {
102-
return $this->administrativeAreaByName($codeOrName);
101+
if ($administrativeArea = $this->administrativeArea($codeOrName)) {
102+
return $administrativeArea;
103103
}
104-
105-
return $administrativeArea;
104+
105+
return $this->administrativeAreaByName($codeOrName);
106106
}
107107

108108
/**
@@ -130,4 +130,4 @@ public function getLocale()
130130
{
131131
return $this->addressing->getLocale();
132132
}
133-
}
133+
}

src/LaravelAddressing.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function __construct($locale = 'en')
5555
*/
5656
public function country($countryCode)
5757
{
58+
$countryCode = strtoupper($countryCode);
5859
return $this->getCountryRepository()->get($countryCode, $this->locale);
5960
}
6061

@@ -83,10 +84,13 @@ public function countryByName($countryName)
8384
*/
8485
public function findCountry($codeOrName)
8586
{
87+
$code = strtoupper($codeOrName);
8688
$countryList = $this->getCountryList();
87-
if (isset($countryList[$codeOrName])) {
88-
return $this->country($codeOrName);
89+
90+
if (isset($countryList[$code])) {
91+
return $this->country($code);
8992
}
93+
9094
return $this->countryByName($codeOrName);
9195
}
9296

@@ -163,4 +167,4 @@ public function getAdministrativeAreaRepository()
163167

164168
return $this->administrativeAreaRepository;
165169
}
166-
}
170+
}

src/Repository/AdministrativeAreaRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function createSubdivisionFromDefinitions($id, array $definitions, $lo
5050
*/
5151
public function getAll($countryCode, $parentId = null, $locale = null)
5252
{
53+
$countryCode = strtoupper($countryCode);
5354
$subdivisions = parent::getAll($countryCode, $parentId, $locale);
5455

5556
return new AdministrativeAreaCollection($subdivisions);
@@ -66,4 +67,4 @@ public function get($id, $locale = null)
6667
{
6768
return parent::get($id, $locale);
6869
}
69-
}
70+
}

src/ServiceProvider.php

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Galahad\LaravelAddressing\Validator\AdministrativeAreaValidator;
66
use Galahad\LaravelAddressing\Validator\CountryValidator;
77
use Galahad\LaravelAddressing\Validator\PostalCodeValidator;
8+
use Illuminate\Contracts\Container\BindingResolutionException;
89
use Illuminate\Validation\Factory;
910

1011
/**
@@ -21,56 +22,89 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
2122
*/
2223
public function boot()
2324
{
24-
if (!$this->app->routesAreCached()) {
25-
require_once __DIR__ . '/routes.php';
26-
}
27-
$this->registerValidators($this->app->validator);
25+
$this->bootRoutes();
2826
$this->loadTranslationsFrom(__DIR__.'/../lang', 'laravel-addressing');
2927
}
28+
29+
/**
30+
* Boot routes if routing is supported
31+
*/
32+
protected function bootRoutes()
33+
{
34+
if (method_exists($this->app, 'routesAreCached') && $this->app->routesAreCached()) {
35+
return;
36+
}
37+
38+
try {
39+
$route = $this->app->make('router');
40+
$route->group(['prefix' => 'galahad/addressing'], function($route) {
41+
$route->get('/{country}/administrative-areas', [
42+
'as' => 'galahad.addressing.administrative-areas',
43+
'uses' => '\\Galahad\\LaravelAddressing\\Controller@getAdministrativeAreas',
44+
]);
45+
$route->get('/{country}/{administrativeArea}/cities', [
46+
'as' => 'galahad.addressing.cities',
47+
'uses' => '\\Galahad\\LaravelAddressing\\Controller@getCities',
48+
]);
49+
$route->get('/countries', [
50+
'as' => 'galahad.addressing.countries',
51+
'uses' => '\\Galahad\\LaravelAddressing\\Controller@getCountries',
52+
]);
53+
});
54+
} catch (BindingResolutionException $exception) {
55+
// Skip routes if no router exists
56+
}
57+
}
3058

3159
/**
3260
* Register the LaravelAddressing instance
3361
*/
3462
public function register()
3563
{
36-
$this->app->singleton(LaravelAddressing::class, function ($app) {
37-
return new LaravelAddressing();
38-
});
64+
$this->app->singleton(LaravelAddressing::class, function($app) {
65+
return new LaravelAddressing(
66+
$app->make('config')->get('app.locale', 'en')
67+
);
68+
});
69+
70+
$this->registerValidators();
3971
}
4072

4173
/**
4274
* Register all custom validators
43-
*
44-
* @param Factory $validatorFactory
4575
*/
46-
protected function registerValidators(Factory $validatorFactory)
76+
protected function registerValidators()
4777
{
48-
// Country validators
49-
$validatorFactory->extend(
50-
'country_code',
51-
CountryValidator::class.'@validateCountryCode'
52-
);
53-
$validatorFactory->extend(
54-
'country_name',
55-
CountryValidator::class.'@validateCountryName'
56-
);
57-
// Administrative Area validators
58-
$validatorFactory->extend(
59-
'administrative_area_code',
60-
AdministrativeAreaValidator::class.'@validateAdministrativeAreaCode'
61-
);
62-
$validatorFactory->extend(
63-
'administrative_area_name',
64-
AdministrativeAreaValidator::class.'@validateAdministrativeAreaName'
65-
);
66-
$validatorFactory->extend(
67-
'administrative_area',
68-
AdministrativeAreaValidator::class.'@validateAdministrativeArea'
69-
);
70-
// Postal Code validator
71-
$validatorFactory->extend(
72-
'postal_code',
73-
PostalCodeValidator::class.'@validatePostalCode'
74-
);
78+
$this->app->resolving('validator', function($validator, $app) {
79+
// Country validators
80+
$validator->extend(
81+
'country_code',
82+
CountryValidator::class.'@validateCountryCode'
83+
);
84+
$validator->extend(
85+
'country_name',
86+
CountryValidator::class.'@validateCountryName'
87+
);
88+
89+
// Administrative Area validators
90+
$validator->extend(
91+
'administrative_area_code',
92+
AdministrativeAreaValidator::class.'@validateAdministrativeAreaCode'
93+
);
94+
$validator->extend(
95+
'administrative_area_name',
96+
AdministrativeAreaValidator::class.'@validateAdministrativeAreaName'
97+
);
98+
$validator->extend(
99+
'administrative_area',
100+
AdministrativeAreaValidator::class.'@validateAdministrativeArea'
101+
);
102+
103+
// Postal Code validator
104+
$validator->extend(
105+
'postal_code',
106+
PostalCodeValidator::class.'@validatePostalCode'
107+
);
108+
});
75109
}
76110
}

tests/LaravelAddressingTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,13 @@ public function testFindAdministrativeArea()
130130
$alabama = $country->findAdministrativeArea('Alabama');
131131
$this->assertEquals($alabama->getCode(), 'AL');
132132
}
133-
}
133+
134+
public function testIsCaseInsensitive()
135+
{
136+
$country = $this->addressing->country('us');
137+
$alabama = $country->findAdministrativeArea('al');
138+
$this->assertEquals($alabama->getName(), 'Alabama');
139+
$alabama = $country->findAdministrativeArea('alabama');
140+
$this->assertEquals($alabama->getCode(), 'AL');
141+
}
142+
}

tests/TranslationTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,33 @@ class TranslationTest extends TestCase
1616
*/
1717
protected $validator;
1818

19+
/**
20+
* @var string
21+
*/
22+
protected $translationDirectory;
23+
1924
public function setUp()
2025
{
2126
parent::setUp();
2227
$this->validator = $this->app['validator'];
28+
$this->translationDirectory = __DIR__.'/../lang';
2329
}
2430

2531
protected function getPackageProviders($app)
2632
{
2733
return [ServiceProvider::class];
2834
}
2935

36+
protected function getMessage($locale, $filename, $key, $field)
37+
{
38+
$filePath = sprintf('%s/%s/%s.php', $this->translationDirectory, $locale, $filename);
39+
$arrayContent = include $filePath;
40+
41+
if (isset($arrayContent[$key])) {
42+
return str_replace(':attribute', $field, $arrayContent[$key]);
43+
}
44+
}
45+
3046
public function testENMessages()
3147
{
3248
$this->app->setLocale('en');
@@ -37,7 +53,8 @@ public function testENMessages()
3753
if ($validator->fails()) {
3854
$messages = $validator->errors()->get('country');
3955
$first = array_shift($messages);
40-
$this->assertEquals($first, 'The country field is not a valid country code.');
56+
$expectedMessage = $this->getMessage('en', 'validation', 'country_code', 'country');
57+
$this->assertEquals($first, $expectedMessage);
4158
}
4259
}
4360

@@ -51,7 +68,8 @@ public function testPT_BRMessages()
5168
if ($validator->fails()) {
5269
$messages = $validator->errors()->get('country');
5370
$first = array_shift($messages);
54-
$this->assertEquals($first, 'O campo country não possui um código de país válido.');
71+
$expectedMessage = $this->getMessage('pt-br', 'validation', 'country_code', 'country');
72+
$this->assertEquals($first, $expectedMessage);
5573
}
5674
}
5775

@@ -65,7 +83,8 @@ public function testENMessagesForAdministrativeAreaValidator()
6583
if ($validator->fails()) {
6684
$messages = $validator->errors()->get('state');
6785
$first = array_shift($messages);
68-
$this->assertEquals($first, 'The state field is not a valid state/province.');
86+
$expectedMessage = $this->getMessage('en', 'validation', 'administrative_area', 'state');
87+
$this->assertEquals($first, $expectedMessage);
6988
}
7089
}
7190
}

0 commit comments

Comments
 (0)