From 372f93bc6ecbff3b313d5412b048e6d53a5ffb09 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Fri, 15 Jul 2022 11:28:13 +0500 Subject: [PATCH 01/10] Added aliases --- src/ISO3166WithAliases.php | 65 ++++++++++++++++++++++++++++++++ tests/ISO3166WithAliasesTest.php | 35 +++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/ISO3166WithAliases.php create mode 100644 tests/ISO3166WithAliasesTest.php diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php new file mode 100644 index 0000000..f7236c6 --- /dev/null +++ b/src/ISO3166WithAliases.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace League\ISO3166; + +use League\ISO3166\ISO3166; +use League\ISO3166\ISO3166DataProvider; + +class ISO3166WithAliases implements ISO3166DataProvider +{ + private ISO3166DataProvider $source; + + public function __construct(ISO3166DataProvider $iso3166) + { + $this->source = $iso3166; + } + + public function name($name): array + { + $aliases = [ + 'Bolivia' => 'Bolivia (Plurinational State of)', + 'Congo-Kinshasa' => 'Congo (Democratic Republic of the)', + 'Iran' => 'Iran (Islamic Republic of)', + 'North Korea' => 'Korea (Democratic People\'s Republic of)', + 'South Korea' => 'Korea (Republic of)', + 'Laos' => 'Lao People\'s Democratic Republic', + 'Micronesia' => 'Micronesia (Federated States of)', + 'Moldova' => 'Moldova (Republic of)', + 'Palestine' => 'Palestine, State of', + 'Russia' => 'Russian Federation', + 'Saint Martin' => 'Saint Martin (French part)', + 'Sint Maarten' => 'Sint Maarten (Dutch part)', + 'Taiwan' => 'Taiwan (Province of China)', + 'Tanzania' => 'Tanzania, United Republic of', + 'United Kingdom' => 'United Kingdom of Great Britain and Northern Ireland', + 'United States' => 'United States of America', + 'Venezuela' => 'Venezuela (Bolivarian Republic of)', + ]; + + return $this->source->name(array_key_exists($name, $aliases) ? $aliases[$name] : $name); + } + + public function alpha2($alpha2): array + { + return $this->source->alpha2($alpha2); + } + + public function alpha3($alpha3): array + { + return $this->source->alpha3($alpha3); + } + + public function numeric($numeric): array + { + return $this->source->numeric($numeric); + } +} diff --git a/tests/ISO3166WithAliasesTest.php b/tests/ISO3166WithAliasesTest.php new file mode 100644 index 0000000..78e3445 --- /dev/null +++ b/tests/ISO3166WithAliasesTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace League\ISO3166; + +use League\ISO3166\Exception\DomainException; +use League\ISO3166\Exception\OutOfBoundsException; +use PHPUnit\Framework\TestCase; + +class ISO3166WithAliasesTest extends TestCase +{ + /** @var ISO3166WithAliases */ + public $iso3166; + + protected function setUp(): void + { + $this->iso3166 = new ISO3166WithAliases(new ISO3166); + } + + public function testAlias(): void + { + $this->assertEquals($this->iso3166->name('United States')['name'], 'United States of America'); + $this->assertEquals($this->iso3166->alpha2('US')['alpha2'], 'US'); + $this->assertEquals($this->iso3166->alpha3('USA')['alpha3'], 'USA'); + $this->assertEquals($this->iso3166->numeric('840')['numeric'], '840'); + } +} From fec7c728599480e2b18423e2c57ccdee8b56e94f Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Fri, 15 Jul 2022 11:37:42 +0500 Subject: [PATCH 02/10] Added Czechia alias --- src/ISO3166WithAliases.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index f7236c6..8b320c9 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -28,6 +28,7 @@ public function name($name): array $aliases = [ 'Bolivia' => 'Bolivia (Plurinational State of)', 'Congo-Kinshasa' => 'Congo (Democratic Republic of the)', + 'Czech Republic' => 'Czechia', 'Iran' => 'Iran (Islamic Republic of)', 'North Korea' => 'Korea (Democratic People\'s Republic of)', 'South Korea' => 'Korea (Republic of)', From d6c07850727deff3466a3a9293becdae07d3a596 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Fri, 15 Jul 2022 12:05:23 +0500 Subject: [PATCH 03/10] Fixed alias search must be case insensitive --- src/ISO3166WithAliases.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index 8b320c9..8a20a1e 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -46,7 +46,14 @@ public function name($name): array 'Venezuela' => 'Venezuela (Bolivarian Republic of)', ]; - return $this->source->name(array_key_exists($name, $aliases) ? $aliases[$name] : $name); + foreach ($aliases as $alias => $full) { + if (0 === strcasecmp($alias, $name)) { + $name = $full; + break; + } + } + + return $this->source->name($name); } public function alpha2($alpha2): array From 450aaad452930b61a40fcf9c0186976db9a02b52 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Fri, 15 Jul 2022 12:21:13 +0500 Subject: [PATCH 04/10] Added Vietnam alias --- src/ISO3166WithAliases.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index 8a20a1e..5994140 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -44,6 +44,7 @@ public function name($name): array 'United Kingdom' => 'United Kingdom of Great Britain and Northern Ireland', 'United States' => 'United States of America', 'Venezuela' => 'Venezuela (Bolivarian Republic of)', + 'Vietnam' => 'Viet Nam', ]; foreach ($aliases as $alias => $full) { From 33194f9349b609ab5f9fef352cf9fd95fa624b65 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Tue, 26 Jul 2022 10:14:07 +0500 Subject: [PATCH 05/10] Added another alias for Bolivia --- src/ISO3166WithAliases.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index 5994140..018cfa2 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -27,6 +27,7 @@ public function name($name): array { $aliases = [ 'Bolivia' => 'Bolivia (Plurinational State of)', + 'Bolivia, Plurinational State of' => 'Bolivia (Plurinational State of)', 'Congo-Kinshasa' => 'Congo (Democratic Republic of the)', 'Czech Republic' => 'Czechia', 'Iran' => 'Iran (Islamic Republic of)', From ea0159e1dd4d3e14109650c30045a4ce2013d4fe Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Jan 2023 13:05:56 +0100 Subject: [PATCH 06/10] Apply suggestions from code review --- src/ISO3166WithAliases.php | 8 ++++---- tests/ISO3166WithAliasesTest.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index 018cfa2..5bed4c8 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -23,7 +23,7 @@ public function __construct(ISO3166DataProvider $iso3166) $this->source = $iso3166; } - public function name($name): array + public function name(string $name): array { $aliases = [ 'Bolivia' => 'Bolivia (Plurinational State of)', @@ -58,17 +58,17 @@ public function name($name): array return $this->source->name($name); } - public function alpha2($alpha2): array + public function alpha2(string $alpha2): array { return $this->source->alpha2($alpha2); } - public function alpha3($alpha3): array + public function alpha3(string $alpha3): array { return $this->source->alpha3($alpha3); } - public function numeric($numeric): array + public function numeric(string $numeric): array { return $this->source->numeric($numeric); } diff --git a/tests/ISO3166WithAliasesTest.php b/tests/ISO3166WithAliasesTest.php index 78e3445..ef4877a 100644 --- a/tests/ISO3166WithAliasesTest.php +++ b/tests/ISO3166WithAliasesTest.php @@ -27,9 +27,9 @@ protected function setUp(): void public function testAlias(): void { - $this->assertEquals($this->iso3166->name('United States')['name'], 'United States of America'); - $this->assertEquals($this->iso3166->alpha2('US')['alpha2'], 'US'); - $this->assertEquals($this->iso3166->alpha3('USA')['alpha3'], 'USA'); - $this->assertEquals($this->iso3166->numeric('840')['numeric'], '840'); + self::assertEquals($this->iso3166->name('United States')['name'], 'United States of America'); + self::assertEquals($this->iso3166->alpha2('US')['alpha2'], 'US'); + self::assertEquals($this->iso3166->alpha3('USA')['alpha3'], 'USA'); + self::assertEquals($this->iso3166->numeric('840')['numeric'], '840'); } } From 6ed6160360c159d818c00332da952f3cbd019f6f Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Jan 2023 13:07:31 +0100 Subject: [PATCH 07/10] Apply suggestions from code review --- tests/ISO3166WithAliasesTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/ISO3166WithAliasesTest.php b/tests/ISO3166WithAliasesTest.php index ef4877a..5e2c839 100644 --- a/tests/ISO3166WithAliasesTest.php +++ b/tests/ISO3166WithAliasesTest.php @@ -17,8 +17,7 @@ class ISO3166WithAliasesTest extends TestCase { - /** @var ISO3166WithAliases */ - public $iso3166; + public ISO3166DataProvider $iso3166; protected function setUp(): void { From dcb5c9b9bb668ab291d8c716a05852e9649e08e2 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Jan 2023 13:08:03 +0100 Subject: [PATCH 08/10] Update tests/ISO3166WithAliasesTest.php --- tests/ISO3166WithAliasesTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ISO3166WithAliasesTest.php b/tests/ISO3166WithAliasesTest.php index 5e2c839..3ca8d9b 100644 --- a/tests/ISO3166WithAliasesTest.php +++ b/tests/ISO3166WithAliasesTest.php @@ -11,6 +11,7 @@ namespace League\ISO3166; +use League\ISO3166\ISO3166DataProvider; use League\ISO3166\Exception\DomainException; use League\ISO3166\Exception\OutOfBoundsException; use PHPUnit\Framework\TestCase; From 8f4629ee072a8ffd18edf332acfd1fe60b52b9d1 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Jan 2023 13:10:08 +0100 Subject: [PATCH 09/10] Update src/ISO3166WithAliases.php --- src/ISO3166WithAliases.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ISO3166WithAliases.php b/src/ISO3166WithAliases.php index 5bed4c8..6369b76 100644 --- a/src/ISO3166WithAliases.php +++ b/src/ISO3166WithAliases.php @@ -16,7 +16,8 @@ class ISO3166WithAliases implements ISO3166DataProvider { - private ISO3166DataProvider $source; + /** @var ISO3166DataProvider */ + private $source; public function __construct(ISO3166DataProvider $iso3166) { From bf5426d6b6a5e21c1ad9c62ac902247bdae3e043 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Jan 2023 13:10:44 +0100 Subject: [PATCH 10/10] Update tests/ISO3166WithAliasesTest.php --- tests/ISO3166WithAliasesTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ISO3166WithAliasesTest.php b/tests/ISO3166WithAliasesTest.php index 3ca8d9b..68691d1 100644 --- a/tests/ISO3166WithAliasesTest.php +++ b/tests/ISO3166WithAliasesTest.php @@ -18,7 +18,8 @@ class ISO3166WithAliasesTest extends TestCase { - public ISO3166DataProvider $iso3166; + /** @var ISO3166DataProvider */ + public $iso3166; protected function setUp(): void {