Skip to content

Commit

Permalink
Change abbreviation method for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
adiachenko committed Mar 29, 2019
1 parent 48d4cb4 commit a049d60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
35 changes: 28 additions & 7 deletions src/Guide.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ public function name(string $abbreviation)
public function abbreviation(string $name)
{
$countries = $this->config->get('countries');
$found = $this->scanStringNames($name, $countries);

if (array_search(ucwords($name), $countries)) {
return array_search(ucwords($name), $countries);
if ($found) {
return $found;
} else {
foreach ($countries as $key => $value) {
if (is_array($value) && in_array(strtolower($name), array_map('strtolower', $value))) {
return $key;
}
}
return $this->scanArrayNames($name, $countries);
}
}

Expand All @@ -58,4 +55,28 @@ public function all(): array
{
return $this->config->get('countries');
}

/**
* @param string $name
* @param array $countries
* @return string|null
*/
protected function scanStringNames(string $name, array $countries)
{
return array_search(ucwords($name), array_filter($countries, 'is_string'));
}

/**
* @param string $name
* @param array $countries
* @return string|null
*/
protected function scanArrayNames(string $name, array $countries)
{
foreach (array_filter($countries, 'is_array') as $abbreviation => $names) {
if (in_array(strtolower($name), array_map('strtolower', $names))) {
return $abbreviation;
}
}
}
}
8 changes: 7 additions & 1 deletion tests/GuideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ protected function setUp()
$this->config = $this->getMockedConfig($this->countries);
}

public function test_get_country_abbreviation_from_name()
public function test_get_country_abbreviation_from_string_name()
{
$guide = new Guide($this->config);
$this->assertEquals('LB', $guide->abbreviation('Lebanon'));
}

public function test_get_country_abbreviation_from_array_name()
{
$guide = new Guide($this->config);
$this->assertEquals('AE', $guide->abbreviation('United Arab Emirates'));
}

public function test_get_country_name_from_abbreviation()
{
$guide = new Guide($this->config);
Expand Down

0 comments on commit a049d60

Please sign in to comment.