Skip to content

Commit

Permalink
Add compatibility with Laravel 10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
juanparati committed May 23, 2023
1 parent ea0a1fa commit 2e602ab
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 75 deletions.
187 changes: 113 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,84 +89,107 @@ It returns something like this:

Retrieve all the countries as a Collection:

(new ISOCodes)
->countries()
->all();
```php
(new ISOCodes)
->countries()
->all();
```

Retrieve one specific country:

(new ISOCodes)
->countries()
->firstWhere('alpha2', 'ES');
```php
(new ISOCodes)
->countries()
->firstWhere('alpha2', 'ES');
```

or using the shortcut

(new ISOCodes)
->countries()
->findByAlpha2('ES');
```php
(new ISOCodes)
->countries()
->findByAlpha2('ES');
```

Retrieve all the countries located in Europe:

(new ISOCodes)
->countries()
->whereContinent('EU');
```php
(new ISOCodes)
->countries()
->whereContinent('EU');
```

Retrieve all the countries located **only** in Europe:

(new ISOCodes)
->countries()
->whereContinent('EU', true);
```php
(new ISOCodes)
->countries()
->whereContinent('EU', true);
```

Retrieve all the countries located in Europe and Asia:

(new ISOCodes)
->countries()
->whereContinent(['EU', 'AS'], true);
```php
(new ISOCodes)
->countries()
->whereContinent(['EU', 'AS'], true);
```

Retrieve all the countries located in Europe **or** Asia

(new ISOCodes)
->countries()
->whereContinent(['EU', 'AS']);
```php
(new ISOCodes)
->countries()
->whereContinent(['EU', 'AS']);
```

Retrieve all the countries sorted by numeric code descending that uses **only** Euro as currency:

(new ISOCodes)
->countries()
->all()
->where('currencies', ['EUR'])
->sortByDesc('numeric');
```php
(new ISOCodes)
->countries()
->all()
->where('currencies', ['EUR'])
->sortByDesc('numeric');
```

or

(new ISOCodes)
->countries()
->whereCurrency('EUR', true)
->sortByDesc('numeric');
```php
(new ISOCodes)
->countries()
->whereCurrency('EUR', true)
->sortByDesc('numeric');
```

Retrieve all the countries that uses **at least** Euro as currency:

(new ISOCodes)
->countries()
->whereCurrency('EUR');

```php
(new ISOCodes)
->countries()
->whereCurrency('EUR');
```

Create a list of countries with their names (useful for generate a listbox options):

(new ISOCodes)
->countries()
->map(fn ($iso) => [
'label' => $iso->name . ' (' . $iso->alpha2 . ')',
'value' => $iso->alpha2
])
->sortBy('label')
->values();
```php
(new ISOCodes)
->countries()
->map(fn ($iso) => [
'label' => $iso->name . ' (' . $iso->alpha2 . ')',
'value' => $iso->alpha2
])
->sortBy('label')
->values();
```

Retrieve a list of countries that has Portuguese as one of their official languages:

(new ISOCodes)
->countries()
->whereLanguage('PT');
```php
(new ISOCodes)
->countries()
->whereLanguage('PT');
```

* Note that most spoken language should be always the first in the list.

Expand All @@ -175,7 +198,9 @@ Retrieve a list of countries that has Portuguese as one of their official langua

Get the list grouped by language:

(new ISOCodes)->languages()->toArray();
```php
(new ISOCodes)->languages()->toArray();
```

It returns something like:

Expand Down Expand Up @@ -214,7 +239,9 @@ Get the list grouped by continent.

Example:

(new ISOCodes)->continents()->toArray();
```php
(new ISOCodes)->continents()->toArray();
```


### Currency model
Expand All @@ -223,33 +250,37 @@ Get the list grouped by currency.

Example:

(new ISOCodes)->currencies()->toArray();

```php
(new ISOCodes)->currencies()->toArray();
```

### CurrencyNumber model

Get the list grouped by currency number.

Example:

(new ISOCodes)->currencyNumbers()->toArray();

```php
(new ISOCodes)->currencyNumbers()->toArray();
```

### Property access

Each record array member can be accessed using the array and object syntax.

Example:

$spain = (new ISOCodes)
->countries()
->findByAlpha2('ES');
```php
$spain = (new ISOCodes)
->countries()
->findByAlpha2('ES');

$spain->name; // Spain
$spain['name']; // Spain
$spain->name; // Spain
$spain['name']; // Spain

$spain->toArray(); // Get record as array
$spain->toJson(); // Get record as Json
$spain->toArray(); // Get record as array
$spain->toJson(); // Get record as Json
```

Each record is serializable, that it make it ideal in order to store the results into a cache.

Expand All @@ -260,10 +291,12 @@ The method `setCurrencyAsNumber` specify if the currency code is returned as a n

Example:

(new ISOCodes)
->countries()
->setCurrencyAsNumber(true)
->all();
```php
(new ISOCodes)
->countries()
->setCurrencyAsNumber(true)
->all();
```


### Node resolution
Expand All @@ -283,13 +316,15 @@ The available node formats are:

Examples:

(new ISOCodes)
->countries()
->setResolution('currencies', CountryModel::NODE_AS_ALL)
->setResolution('languages', CountryModel::NODE_AS_NAME)
->setResolution('continents', CountryModel::NODE_AS_NONE)
->findByAlpha2('PT')
->toArray();
```php
(new ISOCodes)
->countries()
->setResolution('currencies', CountryModel::NODE_AS_ALL)
->setResolution('languages', CountryModel::NODE_AS_NAME)
->setResolution('continents', CountryModel::NODE_AS_NONE)
->findByAlpha2('PT')
->toArray();
```

returns the following:

Expand Down Expand Up @@ -344,7 +379,9 @@ It's possible to register custom datasets and locales during the ISOCodes instan

Example:

new ISOCodes(['countries' => MyCountryTranslation::class])
```php
new ISOCodes(['countries' => MyCountryTranslation::class])
```

See the following example with the [country names](./src/Data/Countries/CountriesEN.php).

Expand All @@ -355,11 +392,13 @@ The models are macroable so it's possible to inject custom methods.

Example:

\Juanparati\ISOCodes\Models\CountryModel::macro('allEUMembers', function () {
return $this->where('eu_member', true)->all();
});
```php
\Juanparati\ISOCodes\Models\CountryModel::macro('allEUMembers', function () {
return $this->where('eu_member', true)->all();
});

(new ISOCodes)->countries()->allEUMembers()->count(); // 27
(new ISOCodes)->countries()->allEUMembers()->count(); // 27
```

## Contributions

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": "^8.0",
"ext-ctype": "*",
"illuminate/support": "^8.34.0|~v9.0"
"illuminate/support": "^8.34.0|~v9.0|~v10.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
Expand Down
23 changes: 23 additions & 0 deletions examples/locale_codes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generate a list of locale codes


```php
\ISOCodes::countries()
->setResolution('languages', \Juanparati\ISOCodes\Models\CountryModel::NODE_AS_ALL)
->all()
->map(function ($r) {
$codes = [];

foreach ($r->languages as $langCode => $lang) {
$codes[] = [
'label' => "$lang ({$r->name})",
'value' => sprintf("%s_%s", strtolower($langCode), $r->alpha2)
];
}

return $codes;
})
->flatten(1)
->sortBy('label')
->values();
```
12 changes: 12 additions & 0 deletions examples/phone_prefixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generate a list of phone prefixes by country

```php
return \ISOCodes::countries()
->all()
->sortBy('name')
->map(fn($r) => [
'label' => "+{$r->phone_code} ({$r->name})",
'value' => "+{$r->phone_code}"
])
->values();
```

0 comments on commit 2e602ab

Please sign in to comment.