Skip to content

Commit

Permalink
Check aliases on looking for country by name
Browse files Browse the repository at this point in the history
  • Loading branch information
slavcodev committed Sep 4, 2020
1 parent 2b1af5a commit 513894f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2921,22 +2921,21 @@ private function findCountryByCode(string $property, $value): ?Country
: null;
}

private function findCountryByName(string $name): ?Country
private function findCountryByName(string $value): ?Country
{
$name = mb_strtolower($name);
$value = mb_strtolower($value);

foreach (self::COUNTRIES as $code => $countryData) {
if ($name === mb_strtolower($countryData['officialName'])) {
return $this->findByIsoAlpha2($code);
}
if ($name === mb_strtolower(iconv('utf8', 'ASCII//TRANSLIT', $countryData['officialName']))) {
return $this->findByIsoAlpha2($code);
}
if ($name === mb_strtolower($countryData['commonName'])) {
return $this->findByIsoAlpha2($code);
$names = [$countryData['officialName'], $countryData['commonName']];

if (isset($countryData['knownNames'])) {
$names = array_unique(array_merge($names, $countryData['knownNames']));
}
if ($name === mb_strtolower(iconv('utf8', 'ASCII//TRANSLIT', $countryData['commonName']))) {
return $this->findByIsoAlpha2($code);

foreach ($names as $name) {
if ($value === mb_strtolower($name) || $value === mb_strtolower(iconv('utf8', 'ASCII//TRANSLIT', $name))) {
return $this->findByIsoAlpha2($code);
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/CountryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public function findByIsoAlpha2WithInvalidIsoAlpha2ThrowsException(): void
$this->getCountryRepository()->findByIsoAlpha2('XY');
}

/**
* @test
*/
public function hasWithIsoAlpha2(): void
{
self::assertTrue($this->getCountryRepository()->hasWithIsoAlpha2('US'));
self::assertFalse($this->getCountryRepository()->hasWithIsoAlpha2('XY'));
}

/**
* @test
*/
Expand Down Expand Up @@ -64,6 +73,25 @@ public function findByNameIgnoresAccentsAndCases(): void
self::assertSame('ax', $country->getTopLevelDomain());
}

/**
* @test
*/
public function findByAliases(): void
{
$country = $this->getCountryRepository()->findByName('North Macedonia');
self::assertSame('North Macedonia', $country->getCommonName());

$aliases = [
'Macedonia',
'Republic of Macedonia',
'The Former Yugoslav Republic of Macedonia',
'Macedonia, The former Yugoslav Republic of',
];
foreach ($aliases as $alias) {
$this->getCountryRepository()->findByName($alias)->equals($country);
}
}

/**
* @test
*/
Expand Down Expand Up @@ -145,6 +173,15 @@ public function findByIsoNumericReturnsCorrectCountry(): void
self::assertSame('us', $country->getTopLevelDomain());
}

/**
* @test
*/
public function hasWithIsoNumeric(): void
{
self::assertTrue($this->getCountryRepository()->hasWithIsoNumeric(840));
self::assertFalse($this->getCountryRepository()->hasWithIsoNumeric(0));
}

/**
* @test
*/
Expand Down

0 comments on commit 513894f

Please sign in to comment.