PHP code example of pragmarx / countries
1. Go to this page and download the library: Download pragmarx/countries library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pragmarx / countries example snippets
use PragmaRX\Countries\Package\Countries;
$countries = new Countries();
echo $countries->where('cca2', 'IT')->first()->hydrateCurrencies()->currencies->EUR->coins->frequent->first();
// or calling it statically
echo Countries::where('cca2', 'IT')->first()->hydrateCurrencies()->currencies->EUR->coins->frequent->first();
use PragmaRX\Countries\Package\Services\Config;
$countries = new Countries(new Config([
'hydrate' => [
'elements' => [
'currencies' => true,
'flag' => true,
'timezones' => true,
],
],
]));
use PragmaRX\Countries\Package\Countries;
$countries = new Countries();
$all = $countries->all();
return $countries->toJson();
$countries->where('name.common', 'Brazil');
$countries->whereNameCommon('Brazil');
$countries->where('name.native.por.common', 'Brasil');
$countries->where('tld.0', '.ch');
$countries->where('cca3', 'USA')->first()->hydrateStates()->states->pluck('name', 'postal')->toArray();
"MA" => "Massachusetts"
"MN" => "Minnesota"
"MT" => "Montana"
"ND" => "North Dakota"
...
$countries->where('cca3', 'FRA')
->first()
->borders
->first()
->name
->official;
$countries->where('name.common', 'United Kingdom')
->hydrate('borders')
->first()
->borders
->reverse()
->first()
->name
->common;
$countries->where('name.common', 'United States')->first()->hydrate('timezones')->timezones->first()->zone_name;
$countries->where('name.common', 'United States')->first()->hydrate('timezones')->timezones->first()->zone_name;
$countries->whereISO639_3('por')->count();
$countries->where('ISO639_3', 'por')->count();
'maps' => [
'lca3' => 'ISO639_3'
]
$countries->whereLca3('por');
$countries->where('lca3', 'por');
app(PragmaRX\Countries\Package\Countries::class)
->all()
->map(function ($country) {
$commonName = $country->name->common;
$languages = $country->languages ?? collect();
$language = $languages->keys()->first() ?? null;
$nativeNames = $country->name->native ?? null;
if (
filled($language) &&
filled($nativeNames) &&
filled($nativeNames[$language]) ?? null
) {
$native = $nativeNames[$language]['common'] ?? null;
}
if (blank($native ?? null) && filled($nativeNames)) {
$native = $nativeNames->first()['common'] ?? null;
}
$native = $native ?? $commonName;
if ($native !== $commonName && filled($native)) {
$native = "$native ($commonName)";
}
return [$country->cca2 => $native];
})
->values()
->toArray();
$countries->all()->pluck('name.common')->toArray();
[
"Aruba",
"Afghanistan",
"Angola",
"Anguilla",
"Åland Islands",
....
$countries->all()->pluck('currencies')->toArray();
[
[
"AWG",
],
[
"AFN",
],
[
"AOA",
],
[
"XCD",
],
[
"EUR",
],
....
$countries->where('name.common', 'Brazil')->first()->hydrate('currencies')->currencies->BRL->units->major->symbol;
$countries->where('name.common', 'United States')
->first()
->hydrateStates()
->states
->sortBy('name')
->pluck('name', 'postal');
[
"AL": "Alabama",
"AK": "Alaska",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California",
....
....
$countries->where('cca3', 'FRA')
->first()
->hydrate('cities')
->cities
->paris
->timezone;
$countries->where('name.common', 'United States')->first()->currencies;
[{
"alternativeSigns": [],
"ISO4217Code": "USD",
"ISO4217Number": "840",
"sign": "$",
"subunits": 100,
"title": "U.S. dollar",
....
$countries->currencies();
[
0 => "AED"
1 => "AFN"
2 => "ALL"
3 => "AMD"
4 => "ANG"
5 => "AOA"
6 => "ARS"
7 => "AUD"
8 => "AWG"
9 => "AZN"
10 => "BAM"
....
return $countries->where('name.common', 'United States')->first()->timezone->NC;
America/New_York
$countries->where('name.common', 'Brazil')
->first()
->hydrateTimezones()
->timezones
->map(function ($timezone) {
return $timezone->zone_name;
})->values()
->unique()
->toArray();
return $countries->where('name.common', 'United States Virgin Islands')->first()->hydrate('timezones_times')->timezones->first()->times;
"times" => [
"abbreviation" => "LMT"
"dst" => "0"
"gmt_offset" => "-14764"
"time_start" => "-1825098837"
"zone_id" => "415"
1 => [
"abbreviation" => "AST"
"dst" => "0"
"gmt_offset" => "-14400"
"time_start" => "-1825098836"
"zone_id" => "415"
]
]
#items: array:22 [▼
"name" => array:3 [▼
"common" => "Brazil"
"official" => "Federative Republic of Brazil"
"native" => array:1 [▼
"por" => array:2 [▼
"official" => "República Federativa do Brasil"
"common" => "Brasil"
]
]
]
"name" => array:3 [▼
"common" => "Switzerland"
"official" => "Swiss Confederation"
"native" => array:4 [▶]
]
"tld" => array:1 [▼
0 => ".ch"
]
text
Europe/Paris
$unitedStatesFlag =
$this->countries->where('cca3', 'USA')
->first()
->flag
->flag_icon;
php artisan vendor:publish --provider=PragmaRX\\Countries\\ServiceProvider
bash
cd vendor/pragmarx/countries
composer install
php update.php