savannabits/countries

PHP 国家和货币

维护者

详细信息

github.com/savannabits/countries

安装: 3

依赖者: 0

建议: 0

安全: 0

星标: 0

观察: 0

分支: 281

v1.0.0 2023-07-31 17:07 UTC

This package is auto-updated.

Last update: 2024-08-30 01:26:47 UTC


README

国家

World Map

Latest Stable Version License Code Quality Build

Coverage PHP Downloads StyleCI

这会给你带来什么?

此包包含有关国家的各种信息

地质和拓扑地图

在众多其他信息中,您还可以绘制国家地图

Switzerland

要求

  • PHP 7.0+

安装

使用 Composer 安装它

composer require pragmarx/countries

实例化

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();

应当都返回

€1

覆盖默认配置

use PragmaRX\Countries\Package\Services\Config;

$countries = new Countries(new Config([
    'hydrate' => [
        'elements' => [
            'currencies' => true,
            'flag' => true,
            'timezones' => true,
        ],
    ],
]));

用法

此包与 Laravel 无关,也不需要安装 Laravel(我们为此目的有一个桥梁),但它在其核心中包含 Laravel Collections,Collections 中的所有方法都可用,这样您就可以执行过滤、映射、归约、搜索、排序、拒绝等操作。它实际上使用 Coollection,这是一个具有流畅语法的 Laravel Collections,允许我们通过对象属性访问数组键(和值)。

要获取数据库中的所有国家,只需这样做

use PragmaRX\Countries\Package\Countries;

$countries = new Countries();

$all = $countries->all();

要获取 JSON

return $countries->toJson();

通过键和值过滤

$countries->where('name.common', 'Brazil');

可以通过其常用名称“巴西”找到巴西,这是一个

#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"
      ]
    ]
  ]

或者您也可以这样过滤

$countries->whereNameCommon('Brazil');

您还可以更深入

$countries->where('name.native.por.common', 'Brasil');

通过国家顶级域名搜索

$countries->where('tld.0', '.ch');

获取

"name" => array:3 [▼
  "common" => "Switzerland"
  "official" => "Swiss Confederation"
  "native" => array:4 [▶]
]
"tld" => array:1 [▼
  0 => ".ch"
]

使用 pluck 等功能

$countries->where('cca3', 'USA')->first()->hydrateStates()->states->pluck('name', 'postal')->toArray();

获取

"MA" => "Massachusetts"
"MN" => "Minnesota"
"MT" => "Montana"
"ND" => "North Dakota"
...

此包使用修改后的 Collection,允许您以对象的方式访问属性和方法

$countries->where('cca3', 'FRA')
         ->first()
         ->borders
         ->first()
         ->name
         ->official;

应当给出

Principality of Andorra

边界默认禁用,但您可以通过调用 hydrate 方法轻松地使边界活化

$countries->where('name.common', 'United Kingdom')
         ->hydrate('borders')
         ->first()
         ->borders
         ->reverse()
         ->first()
         ->name
         ->common;

应当返回

Ireland

活化

为了提高性能,默认启用的活化可以在大多数国家属性上禁用,这是您手动活化的属性

$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;

这些是其中一些可活化的属性

  • 边界
  • 城市
  • 货币
  • 国旗
  • 几何形状
  • 语言
  • 时区
  • 拓扑

额外的 WHERE 规则

一些属性存储方式不同,因此我们需要访问它们时需要特殊的规则,这些属性是

  • ISO639_3 => 3个字母的语言代码。
  • ISO4217 => 3个字母的货币代码。

当然,您也可以像其他属性一样访问它们

$countries->whereISO639_3('por')->count();
$countries->where('ISO639_3', 'por')->count();

映射

有时您可能希望通过不同的名称访问一个属性,这可以在设置中完成,这样

'maps' => [
    'lca3' => 'ISO639_3'
]

在这里,我们将 ISO 3 字母短码语言格式绑定到 lca3,它是 language code alpha 3-letter 的缩写。现在我们可以通过以下方式访问属性

$countries->whereLca3('por');

或者

$countries->where('lca3', 'por');

一些来自 Laravel News 和其他贡献者的示例

使用代码和常用名称生成所有国家的列表

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();

应当给出 267(或更多)个国家,例如

"AW" => "Aruba"
"AF" => "افغانستان (Afghanistan)"
"AO" => "Angola"
"AI" => "Anguilla"
"AX" => "Åland (Åland Islands)"
"AL" => "Shqipëria (Albania)"
"AD" => "Andorra"
"AE" => "دولة الإمارات العربية المتحدة (United Arab Emirates)"
"AR" => "Argentina"
"AM" => "Հայաստան (Armenia)"
"AS" => "American Samoa"
"AQ" => "Antarctica"
"TF" => "Terres australes et antarctiques françaises (French Southern and Antarctic Lands)"
"AG" => "Antigua and Barbuda"
"AU" => "Australia"
"AT" => "Österreich (Austria)"
"AZ" => "Azərbaycan (Azerbaijan)"
"BI" => "Burundi"
"BE" => "Belgien (Belgium)"
"BJ" => "Bénin (Benin)"
"BF" => "Burkina Faso"
"BD" => "বাংলাদেশ (Bangladesh)"
"BG" => "България (Bulgaria)"
"BH" => "‏البحرين (Bahrain)"
"BS" => "Bahamas"
"BA" => "Bosna i Hercegovina (Bosnia and Herzegovina)"
"BL" => "Saint-Barthélemy (Saint Barthélemy)"
"SH" => "Saint Helena, Ascension and Tristan da Cunha"
"BY" => "Белару́сь (Belarus)"
"BZ" => "Belize"
"BM" => "Bermuda"
"BO" => "Wuliwya (Bolivia)"
"BQ" => "Caribisch Nederland (Caribbean Netherlands)"
"BR" => "Brasil (Brazil)"
"BB" => "Barbados"
"BN" => "Negara Brunei Darussalam (Brunei)"
"BT" => "འབྲུག་ཡུལ་ (Bhutan)"
"BV" => "Bouvetøya (Bouvet Island)"
"BW" => "Botswana"
"CF" => "République centrafricaine (Central African Republic)"
"CA" => "Canada"
"CC" => "Cocos (Keeling) Islands"
"CH" => "Suisse (Switzerland)"
"CL" => "Chile"
"CN" => "中国 (China)"
"CI" => "Côte d'Ivoire (Ivory Coast)"
"CM" => "Cameroon"
"CD" => "RD Congo (DR Congo)"
"CG" => "République du Congo (Republic of the Congo)"
"CK" => "Cook Islands"
"CO" => "Colombia"
"KM" => "القمر‎ (Comoros)"
"CV" => "Cabo Verde (Cape Verde)"
...

生成国家的列表

$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;

应当返回

Europe/Paris

获取国家的货币

$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"
    ]
]

国旗

Countries 提供了许多不同的国旗来源,包括 SVG 国旗。这是您使用可用来源之一的方式

安装 flag-icon

npm install --save-dev flag-icon-css

将其导入您的项目中

@import '~flag-icon-css/sass/flag-icon.scss';

使用国家获取旗帜区域

$unitedStatesFlag =
    $this->countries->where('cca3', 'USA')
    ->first()
    ->flag
    ->flag_icon;

在您的模板中渲染

{!! $unitedStatesFlag !!}

发布资产

您可以通过以下方式发布配置

php artisan vendor:publish --provider=PragmaRX\\Countries\\ServiceProvider

数据

来源

此包使用了一些其他开源包,直到我们构建了更好的文档,您可以在mledoze/countries上找到有关数据的更多信息,以及如何在Laravel News文章中使用它。

请检查版权部分以获取此包使用的所有包的完整列表。

更新

警告

此项目上的所有数据文件(JSON、图像、图标等)都不能手动更新。我们将关闭所有需要手动更新这些文件的拉取请求,因为更新脚本会删除它们。

如果您发现数据存在问题,请,您必须要求包管理器(列出的数据源和版权部分)修复它们,然后您可以自己运行更新脚本来正确地在此存储库上更新它们。

update.php

此包包含update.php脚本,您必须使用它来更新数据文件。它将从所有包中下载所有信息,并自动构建.json(以及一些其他如.svg等)。这是您要这样做的方式

cd vendor/pragmarx/countries
composer install
php update.php

并且等待很长时间(有时它看起来像是卡住了,但实际上并没有)直到它完成所有重建,然后只是需要暂存、提交、推送所有重新生成的文件,并起草一个新的PR。

缓存

由于这些数据不应该改变,调用将自动缓存。如果您想更改此行为,可以在发布后编辑config/countries.php文件。

框架桥梁

示例文件

作者

Antonio Carlos Ribeiro 所有贡献者

版权

为了构建国家数据库和关系,此包使用了以下来源和包

许可证

Countries采用BSD 3-Clause许可证 - 请参阅LICENSE文件以获取详细信息

贡献

欢迎提交拉取请求和问题。