Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added aliases #76

Merged
merged 11 commits into from
Jan 23, 2023
76 changes: 76 additions & 0 deletions src/ISO3166WithAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

/*
* (c) Rob Bast <[email protected]>
*
* 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
{
/** @var ISO3166DataProvider */
private $source;

public function __construct(ISO3166DataProvider $iso3166)
{
$this->source = $iso3166;
}

public function name(string $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)',
'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)',
'Vietnam' => 'Viet Nam',
];

foreach ($aliases as $alias => $full) {
if (0 === strcasecmp($alias, $name)) {
$name = $full;
break;
}
}

return $this->source->name($name);
}

public function alpha2(string $alpha2): array
{
return $this->source->alpha2($alpha2);
}

public function alpha3(string $alpha3): array
{
return $this->source->alpha3($alpha3);
}

public function numeric(string $numeric): array
{
return $this->source->numeric($numeric);
}
}
35 changes: 35 additions & 0 deletions tests/ISO3166WithAliasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* (c) Rob Bast <[email protected]>
*
* 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\ISO3166DataProvider;
use League\ISO3166\Exception\DomainException;
alcohol marked this conversation as resolved.
Show resolved Hide resolved
use League\ISO3166\Exception\OutOfBoundsException;
use PHPUnit\Framework\TestCase;

class ISO3166WithAliasesTest extends TestCase
{
public ISO3166DataProvider $iso3166;
alcohol marked this conversation as resolved.
Show resolved Hide resolved

protected function setUp(): void
{
$this->iso3166 = new ISO3166WithAliases(new ISO3166);
}

public function testAlias(): void
{
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');
}
}