Skip to content

Commit

Permalink
Deprecate findByOfficialName and findByCommonName of CountryRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
slavcodev committed Sep 4, 2020
1 parent f17d958 commit 98f53e6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
72 changes: 36 additions & 36 deletions src/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2801,32 +2801,30 @@ public function hasWithIsoAlpha2(string $isoAlpha2): bool

/**
* Retrieve a country by official name.
*
* @deprecated In favour of findByName
*/
public function findByOfficialName(string $officialName): Country
public function findByOfficialName(string $name): Country
{
$country = $this->findCountryByCaseSensitiveProperty('officialName', $officialName);

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

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

/**
* Lookup whether the repository has a country by official name.
*
* @deprecated In favour of hasWithName
*/
public function hasWithOfficialName(string $officialName): bool
public function hasWithOfficialName(string $name): bool
{
return $this->findCountryByCaseSensitiveProperty('officialName', $officialName) instanceof Country;
return $this->hasWithName($name);
}

/**
* Lookup a country by ISO numeric code.
*/
public function findByIsoNumeric(int $isoNumeric): Country
{
$country = $this->findCountryByCaseInsensitiveProperty('isoNumeric', $isoNumeric);
$country = $this->findCountryByCode('isoNumeric', $isoNumeric);

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with ISO numeric code "%s"', $isoNumeric));
Expand All @@ -2840,45 +2838,41 @@ public function findByIsoNumeric(int $isoNumeric): Country
*/
public function hasWithIsoNumeric(int $isoNumeric): bool
{
return $this->findCountryByCaseInsensitiveProperty('isoNumeric', $isoNumeric) instanceof Country;
return $this->findCountryByCode('isoNumeric', $isoNumeric) instanceof Country;
}

/**
* Lookup a country by common name.
*
* @deprecated In favour of findByName
*/
public function findByCommonName(string $commonName): Country
public function findByCommonName(string $name): Country
{
$country = $this->findCountryByCaseSensitiveProperty('commonName', $commonName);

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

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

/**
* Lookup whether the repository has a country with specified common name.
*
* @deprecated In favour of hasWithName
*/
public function hasWithCommonName(string $commonName): bool
public function hasWithCommonName(string $name): bool
{
return $this->findCountryByCaseSensitiveProperty('commonName', $commonName) instanceof Country;
return $this->hasWithName($name);
}

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

if ($this->hasWithCommonName($name)) {
return $this->findByCommonName($name);
if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with name "%s"', $name));
}

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

/**
Expand All @@ -2887,15 +2881,15 @@ public function findByName(string $name): Country
*/
public function hasWithName(string $name): bool
{
return $this->hasWithOfficialName($name) || $this->hasWithCommonName($name);
return $this->findCountryByName($name) instanceof Country;
}

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

if (!$country instanceof Country) {
throw new RecordNotFoundException(sprintf('Cannot find country with ISO alpha3 code "%s"', $isoAlpha3));
Expand All @@ -2909,10 +2903,10 @@ public function findByIsoAlpha3(string $isoAlpha3): Country
*/
public function hasWithIsoAlpha3(string $isoAlpha3): bool
{
return $this->findCountryByCaseInsensitiveProperty('isoAlpha3', $isoAlpha3) instanceof Country;
return $this->findCountryByCode('isoAlpha3', $isoAlpha3) instanceof Country;
}

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 98f53e6

Please sign in to comment.