Skip to content

Commit

Permalink
Merge 5b49a81 into f17d958
Browse files Browse the repository at this point in the history
  • Loading branch information
slavcodev committed Sep 4, 2020
2 parents f17d958 + 5b49a81 commit 279fe0b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
106 changes: 53 additions & 53 deletions src/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2800,119 +2800,113 @@ public function hasWithIsoAlpha2(string $isoAlpha2): bool
}

/**
* Retrieve a country by official name.
* Lookup a country by ISO numeric code.
*/
public function findByOfficialName(string $officialName): Country
public function findByIsoNumeric(int $isoNumeric): Country
{
$country = $this->findCountryByCaseSensitiveProperty('officialName', $officialName);
$country = $this->findCountryByCode('isoNumeric', $isoNumeric);

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with official name "%s"', $officialName));
throw new RecordNotFoundException(sprintf('Cannot find country with ISO numeric code "%s"', $isoNumeric));
}

return $country;
}

/**
* Lookup whether the repository has a country by official name.
* Lookup whether the repository has a country with specified ISO numeric code.
*/
public function hasWithOfficialName(string $officialName): bool
public function hasWithIsoNumeric(int $isoNumeric): bool
{
return $this->findCountryByCaseSensitiveProperty('officialName', $officialName) instanceof Country;
return $this->findCountryByCode('isoNumeric', $isoNumeric) instanceof Country;
}

/**
* Lookup a country by ISO numeric code.
* Lookup a country by official name, common name, ignoring case and accents.
*/
public function findByIsoNumeric(int $isoNumeric): Country
public function findByName(string $name): Country
{
$country = $this->findCountryByCaseInsensitiveProperty('isoNumeric', $isoNumeric);
$country = $this->findCountryByName($name);

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with ISO numeric code "%s"', $isoNumeric));
throw new RecordNotFoundException(sprintf('Cannot find country with name "%s"', $name));
}

return $country;
}

/**
* Lookup whether the repository has a country with specified ISO numeric code.
* Lookup whether the repository has a country with specified name as either official name or common name,
* ignoring case and accents.
*/
public function hasWithIsoNumeric(int $isoNumeric): bool
public function hasWithName(string $name): bool
{
return $this->findCountryByCaseInsensitiveProperty('isoNumeric', $isoNumeric) instanceof Country;
return $this->findCountryByName($name) instanceof Country;
}

/**
* Lookup a country by common name.
* Retrieve a country by its ISO alpha3 code.
*/
public function findByCommonName(string $commonName): Country
public function findByIsoAlpha3(string $isoAlpha3): Country
{
$country = $this->findCountryByCaseSensitiveProperty('commonName', $commonName);
$country = $this->findCountryByCode('isoAlpha3', $isoAlpha3);

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with common name "%s"', $commonName));
throw new RecordNotFoundException(sprintf('Cannot find country with ISO alpha3 code "%s"', $isoAlpha3));
}

return $country;
}

/**
* Lookup whether the repository has a country with specified common name.
* Lookup whether the repository has a country with specified ISO alpha3 code.
*/
public function hasWithCommonName(string $commonName): bool
public function hasWithIsoAlpha3(string $isoAlpha3): bool
{
return $this->findCountryByCaseSensitiveProperty('commonName', $commonName) instanceof Country;
return $this->findCountryByCode('isoAlpha3', $isoAlpha3) instanceof Country;
}

/**
* Lookup a country by official name, common name, ignoring case and accents.
* Lookup a country by common name.
*
* @deprecated In favour of findByName
*/
public function findByName(string $name): Country
public function findByCommonName(string $name): Country
{
if ($this->hasWithOfficialName($name)) {
return $this->findByOfficialName($name);
}

if ($this->hasWithCommonName($name)) {
return $this->findByCommonName($name);
}

throw new RecordNotFoundException(sprintf('Cannot find country with name "%s"', $name));
return $this->findByName($name);
}

/**
* Lookup whether the repository has a country with specified name as either official name or common name,
* ignoring case and accents.
* Lookup whether the repository has a country with specified common name.
*
* @deprecated In favour of hasWithName
*/
public function hasWithName(string $name): bool
public function hasWithCommonName(string $name): bool
{
return $this->hasWithOfficialName($name) || $this->hasWithCommonName($name);
return $this->hasWithName($name);
}

/**
* Retrieve a country by its ISO alpha3 code.
* Retrieve a country by official name.
*
* @deprecated In favour of findByName
*/
public function findByIsoAlpha3(string $isoAlpha3): Country
public function findByOfficialName(string $name): Country
{
$country = $this->findCountryByCaseInsensitiveProperty('isoAlpha3', $isoAlpha3);

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with ISO alpha3 code "%s"', $isoAlpha3));
}

return $country;
return $this->findByName($name);
}

/**
* Lookup whether the repository has a country with specified ISO alpha3 code.
* Lookup whether the repository has a country by official name.
*
* @deprecated In favour of hasWithName
*/
public function hasWithIsoAlpha3(string $isoAlpha3): bool
public function hasWithOfficialName(string $name): bool
{
return $this->findCountryByCaseInsensitiveProperty('isoAlpha3', $isoAlpha3) instanceof Country;
return $this->hasWithName($name);
}

private function findCountryByCaseInsensitiveProperty(string $property, $value): ?Country
private function findCountryByCode(string $property, $value): ?Country
{
$alpha2Code = array_search($value, array_column(self::COUNTRIES, $property, 'isoAlpha2'), true);

Expand All @@ -2921,15 +2915,21 @@ private function findCountryByCaseInsensitiveProperty(string $property, $value):
: null;
}

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

foreach (self::COUNTRIES as $code => $countryData) {
if ($value === mb_strtolower($countryData[$property])) {
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);
}
if ($value === mb_strtolower(iconv('utf8', 'ASCII//TRANSLIT', $countryData[$property]))) {
if ($name === mb_strtolower(iconv('utf8', 'ASCII//TRANSLIT', $countryData['commonName']))) {
return $this->findByIsoAlpha2($code);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CountryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function findByOfficialNameWithInvalidNameThrowsException(): void
public function hasWithOfficialName(): void
{
$this->assertTrue($this->getCountryRepository()->hasWithOfficialName('United States of America'));
$this->assertFalse($this->getCountryRepository()->hasWithOfficialName('United States'));
$this->assertFalse($this->getCountryRepository()->hasWithOfficialName('States'));
}

/**
Expand All @@ -109,7 +109,7 @@ public function hasWithOfficialName(): void
public function hasWithCommonName(): void
{
$this->assertTrue($this->getCountryRepository()->hasWithCommonName('United States'));
$this->assertFalse($this->getCountryRepository()->hasWithCommonName('United States of America'));
$this->assertFalse($this->getCountryRepository()->hasWithCommonName('States'));
}

/**
Expand Down

0 comments on commit 279fe0b

Please sign in to comment.