2929 */
3030class 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}
0 commit comments