Skip to content

Commit 74306a9

Browse files
committed
Add countryCode length checker
1 parent 0f46c97 commit 74306a9

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Country.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
*/
3030
class Country
3131
{
32+
private const LENGTH_COUNTRY_CODE = 2;
33+
3234
/**
33-
* @param string $country
35+
* @param string $countryCode
3436
*/
35-
public function __construct(protected string $country)
37+
public function __construct(protected string $countryCode)
3638
{
3739
}
3840

@@ -60,15 +62,19 @@ private function getLanguage(array $values, string $language): string
6062
*/
6163
public function getCode(): string
6264
{
63-
if ($this->country === '') {
65+
if ($this->countryCode === '') {
66+
return CountryUnknown::COUNTRY_CODE_IV;
67+
}
68+
69+
if (strlen($this->countryCode) !== self::LENGTH_COUNTRY_CODE) {
6470
return CountryUnknown::COUNTRY_CODE_IV;
6571
}
6672

67-
if (!array_key_exists($this->country, CountryAll::COUNTRY_NAMES)) {
73+
if (!array_key_exists($this->countryCode, CountryAll::COUNTRY_NAMES)) {
6874
return CountryUnknown::COUNTRY_CODE_UK;
6975
}
7076

71-
return $this->country;
77+
return $this->countryCode;
7278
}
7379

7480
/**
@@ -80,14 +86,20 @@ public function getCode(): string
8086
*/
8187
public function getName(string $language = Language::EN_GB): string
8288
{
83-
if ($this->country === '') {
89+
$countryCode = $this->getCode();
90+
91+
if ($countryCode === CountryUnknown::COUNTRY_CODE_IV) {
8492
return $this->getLanguage(CountryUnknown::COUNTRY_NAME_IV, $language);
8593
}
8694

87-
if (!array_key_exists($this->country, CountryAll::COUNTRY_NAMES)) {
95+
if ($countryCode === CountryUnknown::COUNTRY_CODE_UK) {
96+
return $this->getLanguage(CountryUnknown::COUNTRY_NAME_UK, $language);
97+
}
98+
99+
if (!array_key_exists($countryCode, CountryAll::COUNTRY_NAMES)) {
88100
return $this->getLanguage(CountryUnknown::COUNTRY_NAME_UK, $language);
89101
}
90102

91-
return $this->getLanguage(CountryAll::COUNTRY_NAMES[$this->country], $language);
103+
return $this->getLanguage(CountryAll::COUNTRY_NAMES[$this->countryCode], $language);
92104
}
93105
}

tests/Unit/CountryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public function dataProvider(): array
140140
*/
141141
[++$number, 'getCode', null, '', CountryUnknown::COUNTRY_CODE_IV],
142142
[++$number, 'getName', null, '', CountryUnknown::COUNTRY_NAME_IV[Language::EN_GB]],
143+
[++$number, 'getCode', null, 'DEU', CountryUnknown::COUNTRY_CODE_IV],
144+
[++$number, 'getName', null, 'DEU', CountryUnknown::COUNTRY_NAME_IV[Language::EN_GB]],
145+
[++$number, 'getCode', null, 'D', CountryUnknown::COUNTRY_CODE_IV],
146+
[++$number, 'getName', null, 'D', CountryUnknown::COUNTRY_NAME_IV[Language::EN_GB]],
143147
[++$number, 'getCode', null, 'XX', CountryUnknown::COUNTRY_CODE_UK],
144148
[++$number, 'getName', null, 'XX', CountryUnknown::COUNTRY_NAME_UK[Language::EN_GB]],
145149
];

0 commit comments

Comments
 (0)