Skip to content

Commit 0e8ab22

Browse files
committed
drop older laravel/php versions, better types
1 parent bec02cc commit 0e8ab22

File tree

6 files changed

+53
-109
lines changed

6 files changed

+53
-109
lines changed

.github/workflows/phpunit.yml

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,16 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php: [
16-
{
17-
version: 7.4,
18-
phpunitConfig: phpunit.xml
19-
},
20-
{
21-
version: 8.0,
22-
phpunitConfig: phpunit.xml
23-
},
24-
{
25-
version: 8.1,
26-
phpunitConfig: phpunit.xml
27-
},
28-
{
29-
version: 8.2,
30-
phpunitConfig: phpunit-new.xml
31-
},
32-
{
33-
version: 8.3,
34-
phpunitConfig: phpunit-new.xml
35-
}
36-
]
15+
php: [8.2, 8.3, 8.4]
3716

38-
name: PHP ${{ matrix.php.version }} tests
17+
name: PHP ${{ matrix.php }} tests
3918
steps:
4019
- uses: actions/checkout@v4
4120

4221
- name: Setup PHP
4322
uses: shivammathur/setup-php@v2
4423
with:
45-
php-version: ${{ matrix.php.version }}
24+
php-version: ${{ matrix.php }}
4625
coverage: none
4726

4827
- name: Validate composer.json and composer.lock
@@ -52,4 +31,4 @@ jobs:
5231
run: composer install --prefer-dist --no-progress --no-suggest
5332

5433
- name: Run test suite
55-
run: vendor/bin/phpunit --configuration ${{ matrix.php.phpunitConfig }}
34+
run: vendor/bin/phpunit

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
"authors": [
1414
{
1515
"name": "Doug Sisk",
16-
"homepage": "http://dougsisk.com"
16+
"homepage": "https://dougsisk.com"
1717
}
1818
],
1919
"support": {
2020
"issues": "https://github.com/DougSisk/laravel-country-state/issues"
2121
},
2222
"require": {
23-
"php": "^7.4|^8.0",
24-
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
25-
"rinvex/countries": "^7.3|^8.0"
23+
"php": "^8.2",
24+
"illuminate/support": "^11.0|^12.0",
25+
"rinvex/countries": "^8.0"
2626
},
2727
"require-dev": {
28-
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0|^9.0",
29-
"phpunit/phpunit": "^8.0|^9.0|^10.0|^11.0"
28+
"orchestra/testbench": "^9.0|^10.0",
29+
"phpunit/phpunit": "^10.0|^11.0|^12.0"
3030
},
3131
"autoload": {
3232
"psr-4": {

phpunit-new.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

phpunit.xml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
11-
<testsuites>
12-
<testsuite name="CountryState Test Suite">
13-
<file>tests/CountryStateTest.php</file>
14-
</testsuite>
15-
<testsuite name="CountryState Laravel Test Suite">
16-
<file>tests/laravel/CountryStateLaravelTest.php</file>
17-
</testsuite>
18-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="CountryState Test Suite">
5+
<file>tests/CountryStateTest.php</file>
6+
</testsuite>
7+
<testsuite name="CountryState Laravel Test Suite">
8+
<file>tests/laravel/CountryStateLaravelTest.php</file>
9+
</testsuite>
10+
</testsuites>
1911
</phpunit>

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Laravel Country & State Helper
44
[![Total Downloads](https://poser.pugx.org/dougsisk/laravel-country-state/downloads)](https://packagist.org/packages/dougsisk/laravel-country-state)
55
[![License](https://poser.pugx.org/dougsisk/laravel-country-state/license)](https://packagist.org/packages/dougsisk/laravel-country-state)
66

7-
A helper to list countries & states in English in **Laravel 6.0+**.
7+
A helper to list countries & states in English in **Laravel 11.0+**.
88

9-
What's Changed in 4.0
9+
What's Changed in 5.0
1010
-----------------
1111

12-
* **PHP 7.4+ or 8.0+ required**
12+
* **PHP 8.2+ required**
13+
14+
_I'm aware the underlying country/state data package I utilize has not been actively maintained. I've looked into other packages, but have yet to find one that can easily be swapped in. Please feel free to submit a PR if you find one you think is a suitable replacement._
1315

1416
Installation
1517
------------

src/CountryState.php

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88

99
class 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

Comments
 (0)