88
99class CountryState
1010{
11- protected $ countries = [];
12- protected $ countriesTranslated = [];
13- protected $ language ;
14- protected $ states = [];
11+ protected array $ countries = [];
12+
13+ protected array $ countriesTranslated = [];
14+
15+ protected string $ language ;
16+
17+ protected array $ states = [];
1518
1619 /**
1720 * Create a new country state helper instance.
18- *
19- * @param array $limitCountries
20- * @param array $preloadCountryStates
21- * @param string $language
22- * @return void
2321 */
24- public function __construct ($ limitCountries = null , $ preloadCountryStates = null , $ language = 'eng ' )
22+ public function __construct (? array $ limitCountries = null , ? array $ preloadCountryStates = null , string $ language = 'eng ' )
2523 {
2624 if ($ limitCountries ) {
2725 foreach ($ limitCountries as $ code ) {
@@ -50,11 +48,8 @@ public function __construct($limitCountries = null, $preloadCountryStates = null
5048 * Get a list of countries. If class has been constructed to limit countries, only those countries will be returned.
5149 * The array returned will be countries' names in the class' set language.
5250 * If a different language is desired, pass the three character ISO 639-3 code of the desired language
53- *
54- * @param string $language
55- * @return array
5651 */
57- public function getCountries ($ language = null )
52+ public function getCountries (? string $ language = null ): array
5853 {
5954 if ($ language ) {
6055 $ this ->setLanguage ($ language );
@@ -65,47 +60,34 @@ public function getCountries($language = null)
6560
6661 /**
6762 * Get the information of a country by passing its two character code
68- *
69- * @param string $lookFor
70- * @return Rinvex\Country\Country
7163 */
72- public function getCountry ($ lookFor )
64+ public function getCountry (string $ lookFor ): Country
7365 {
7466 return $ this ->loadCountry ($ lookFor );
7567 }
7668
7769 /**
7870 * Get the name of a country by passing its two character code
79- *
80- * @param string $lookFor
81- * @return string
8271 */
83- public function getCountryName ($ lookFor )
72+ public function getCountryName (string $ lookFor ): string
8473 {
8574 return $ this ->getCountry ($ lookFor )->getName ();
8675 }
8776
8877 /**
8978 * Get a list of states for a given country.
9079 * The country's two character ISO code
91- *
92- * @param string $country
93- * @return array
9480 */
95- public function getStates ($ country )
81+ public function getStates (string $ country ): array
9682 {
9783 return $ this ->findCountryStates ($ country );
9884 }
9985
10086 /**
10187 * Get the name of a state by passing its two character code
10288 * Specifying a two character ISO country code will limit the search to a specific country
103- *
104- * @param string $lookFor
105- * @param string $country
106- * @return string
10789 */
108- public function getStateName ($ lookFor , $ country = null )
90+ public function getStateName (string $ lookFor , ? string $ country = null ): string
10991 {
11092 if ($ country ) {
11193 if (! isset ($ this ->states [$ country ])) {
@@ -130,16 +112,17 @@ public function getStateName($lookFor, $country = null)
130112
131113 /**
132114 * Pass a country code to search a single country or an array of codes to search several countries in the order given
133- * If $country is null all countries will be searched, which can be slow.
134- *
135- * @param string $lookFor
136- * @param mixed $country
137- * @return string|void
115+ * If $countries is null all countries will be searched, which can be slow.
138116 */
139- public function getStateCode ($ lookFor , $ country = null )
117+ public function getStateCode (string $ lookFor , null | string | array $ countries = null ): ? string
140118 {
141119 $ lookFor = mb_strtoupper ($ lookFor );
142- $ countries = is_null ($ country ) ? array_keys ($ this ->countries ) : (array ) $ country ;
120+
121+ if (is_null ($ countries )) {
122+ $ countries = array_keys ($ this ->countries );
123+ } elseif (is_string ($ countries )) {
124+ $ countries = [$ countries ];
125+ }
143126
144127 foreach ($ countries as $ countryCode ) {
145128 $ states = array_map ('mb_strtoupper ' , $ this ->findCountryStates ($ countryCode ));
@@ -148,16 +131,15 @@ public function getStateCode($lookFor, $country = null)
148131 return $ code ;
149132 }
150133 }
134+
135+ return null ;
151136 }
152137
153138 /**
154139 * Change the default translation language using the three character ISO 639-3 code of the desired language.
155140 * Country name translations will be reloaded.
156- *
157- * @param string $language
158- * @return $this
159141 */
160- public function setLanguage ($ language )
142+ public function setLanguage (string $ language ): self
161143 {
162144 $ this ->language = $ language ;
163145
@@ -168,7 +150,7 @@ public function setLanguage($language)
168150 return $ this ;
169151 }
170152
171- protected function loadCountry ($ code )
153+ protected function loadCountry ($ code ): Country
172154 {
173155 try {
174156 return CountryLoader::country (mb_strtolower ($ code ));
@@ -177,7 +159,7 @@ protected function loadCountry($code)
177159 }
178160 }
179161
180- protected function findCountryStates ($ country )
162+ protected function findCountryStates ($ country ): array
181163 {
182164 if (! array_key_exists ($ country , $ this ->states )) {
183165 $ this ->addCountryStates ($ country );
@@ -186,7 +168,7 @@ protected function findCountryStates($country)
186168 return $ this ->states [$ country ];
187169 }
188170
189- protected function addCountryStates ($ country )
171+ protected function addCountryStates ($ country ): void
190172 {
191173 if (! $ country instanceof Country) {
192174 $ country = $ this ->loadCountry ($ country );
@@ -197,9 +179,9 @@ protected function addCountryStates($country)
197179 $ this ->states [$ countryCode ] = [];
198180 $ states = $ country ->getDivisions ();
199181
200- if ( ! is_null ($ states ) ) {
182+ if (! is_null ($ states )) {
201183 foreach ($ states as $ code => $ division ) {
202- $ code = preg_replace (" /([A-Z]{2}-)/ " , '' , $ code );
184+ $ code = preg_replace (' /([A-Z]{2}-)/ ' , '' , $ code );
203185 $ this ->states [$ countryCode ][$ code ] = $ division ['name ' ];
204186 }
205187 }
0 commit comments