在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):antonioribeiro/countries开源软件地址(OpenSource Url):https://github.com/antonioribeiro/countries开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):CountriesWhat does it gives you?This package has all sorts of information about countries:
Geology and topology mapsAmongst many other information you'll be able to plot country maps: Requirements
InstallingUse Composer to install it:
Instantiatinguse 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(); Should both return
Overloading the default configuration: use PragmaRX\Countries\Package\Services\Config;
$countries = new Countries(new Config([
'hydrate' => [
'elements' => [
'currencies' => true,
'flag' => true,
'timezones' => true,
],
],
])); UsageThis package is not tied to Laravel and doesn't require it to be installed (we have a bridge for this purpose), but it has Laravel Collections in its core, all methods in Collections are available, this way you can do things like filter, map, reduce, search, sort, reject, and a lot more. It, actually, uses Coollection, which is Laravel Collections with a fluent syntax, allowing us to have access to array keys (and values) as object properties. To get all countries in the data base you just have to: use PragmaRX\Countries\Package\Countries;
$countries = new Countries();
$all = $countries->all(); To get a json you: return $countries->toJson(); Filter by keys and values: $countries->where('name.common', 'Brazil'); Will find Brazil by its common name, which is a
Or alternatively you can filter like this $countries->whereNameCommon('Brazil'); And, you can go deepeer $countries->where('name.native.por.common', 'Brasil'); Or search by the country top level domain $countries->where('tld.0', '.ch'); To get
And use things like pluck $countries->where('cca3', 'USA')->first()->hydrateStates()->states->pluck('name', 'postal')->toArray(); To get "MA" => "Massachusetts"
"MN" => "Minnesota"
"MT" => "Montana"
"ND" => "North Dakota"
... The package uses a modified Collection which allows you to access properties and methods as objects: $countries->where('cca3', 'FRA')
->first()
->borders
->first()
->name
->official; Should give
Borders hydration is disabled by default, but you can have your borders hydrated easily by calling the hydrate method: $countries->where('name.common', 'United Kingdom')
->hydrate('borders')
->first()
->borders
->reverse()
->first()
->name
->common; Should return
HydrationTo improve performance, hydration, which is enabled by default, can be disable on most country properties, and this is how you manually hydrate properties: $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; Those are some of the hydratable properties:
Extra where rulesSome properties are stored differently and we therefore need special rules for accessing them, these properties are
You can of course access them like other properties $countries->whereISO639_3('por')->count();
$countries->where('ISO639_3', 'por')->count(); MappingSometimes you would like to access a property by a different name, this can be done in settings, this way 'maps' => [
'lca3' => 'ISO639_3'
] Here we bind the language 3 letter short code ISO format to $countries->whereLca3('por'); Or $countries->where('lca3', 'por'); Some other examples from Laravel News and some other contributorsGenerate a list of all countries with code, using native name and commonapp(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(); Should give you 267 (or so) countries like:
Generate a list of countries$countries->all()->pluck('name.common')->toArray(); returns [
"Aruba",
"Afghanistan",
"Angola",
"Anguilla",
"Åland Islands",
.... Generate a list of currencies$countries->all()->pluck('currencies')->toArray(); returns [
[
"AWG",
],
[
"AFN",
],
[
"AOA",
],
[
"XCD",
],
[
"EUR",
],
.... Get the currency symbol$countries->where('name.common', 'Brazil')->first()->hydrate('currencies')->currencies->BRL->units->major->symbol; Generate a list of States$countries->where('name.common', 'United States')
->first()
->hydrateStates()
->states
->sortBy('name')
->pluck('name', 'postal'); returns [
"AL": "Alabama",
"AK": "Alaska",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California",
....
.... Hydrate and get a cities$countries->where('cca3', 'FRA')
->first()
->hydrate('cities')
->cities
->paris
->timezone; Should return
Get a countries currencies$countries->where('name.common', 'United States')->first()->currencies; returns [{
"alternativeSigns": [],
"ISO4217Code": "USD",
"ISO4217Number": "840",
"sign": "$",
"subunits": 100,
"title": "U.S. dollar",
.... Get all currencies$countries->currencies(); returns [
0 => "AED"
1 => "AFN"
2 => "ALL"
3 => "AMD"
4 => "ANG"
5 => "AOA"
6 => "ARS"
7 => "AUD"
8 => "AWG"
9 => "AZN"
10 => "BAM"
.... Get the timezone for a Statereturn $countries->where('name.common', 'United States')->first()->timezone->NC; returns America/New_York Get all timezones for a country$countries->where('name.common', 'Brazil')
->first()
->hydrateTimezones()
->timezones
->map(function ($timezone) {
return $timezone->zone_name;
})->values()
->unique()
->toArray(); Get all times for a timezonereturn $countries->where('name.common', 'United States Virgin Islands')->first()->hydrate('timezones_times')->timezones->first()->times; returns "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"
]
] FlagsCountries provides many different flag sources, including SVG flags. This is how you use one of the available sources: flag-iconInstall
Import it to your project
Use Countries to get the flag span
Render it in your blade template
|