brianfp/world

提供国家、州和城市关系数据库。

v8.2 2021-08-10 16:14 UTC

This package is not auto-updated.

Last update: 2024-09-20 06:26:56 UTC


README

此包专注于世界国家、地区和城市数据库,支持Laravel的本地化。

概念

此包中有5个主要对象。

  • World: 地球。
  • Continent: 7个大陆
  • Country: 248个国家
  • Division: 州/省等地区。
  • City: 地区的最后一层,一些城市属于国家,一些属于地区。

属性

常见属性

  • name: 地区的通用名称(英文)。
  • full_name: 完整名称或官方名称(英文)。
  • code: ISO-3166-1-alpha2/ISO-3166-2代码
  • local_name: 通用名称的翻译
  • local_full_name: 完整名称的翻译
  • local_alias: 不同的语言中的别名
  • local_abbr: 缩写

国家特定属性

  • emoji: 国家的表情符号国旗
  • capital: 该国家的首都
  • code_alpha3: ISO-3166-1-alpha3代码
  • currency_code: ISO-4177货币代码,例如USD,CNY
  • currency_name: ISO-4177货币名称
  • local_currency_name: 本地化货币名称

示例

use Khsing\World\World;
$china = World::getByCode('cn');
$china->setLocale('zh-cn');
$china->name; // China
$china->local_name; // 中国
$china->full_name; // People's Republic of China
$china->local_full_name; // 中华人民共和国
$china->emoji; // 🇨🇳
$china->callingcode; // 86
$china->code; // CN
$china->code_alpha3; // CHN
$china->has_division; // true
$china->currency_code; // CNY
$china->currency_name; // Yuan Renminbi
$china->local_currency_name; // 人民币

本地化

目前,只支持英语(默认和回退)和中文简体 zh-cn。区域设置遵循Laravel项目中的config/app.php设置。

设置

  • composer require
composer require brianfp/world
  • 将服务提供者添加到config/app.php中,(仅限于Laravel 5.5之前)
'providers' => [
    // ...
    Khsing\World\WorldServiceProvider::class,
]
  • 发布和初始化
php artisan vendor:publish --force --provider="Khsing\World\WorldServiceProvider"
composer dump-autoload
php artisan world:init

使用

  • 获取所有大陆
use Khsing\World\World;

World::Continents()
  • 获取所有国家
use Khsing\World\World;

World::Countries()
  • 按代码获取国家/城市/地区
use Khsing\World\World;

World::getByCode('cn'); // iso-3166 alpha 2 code
World::getByCode('chn'); // iso-3166 alpha 3 code
World::getByCode('cn-11'); // Beijing
  • 获取属于某个大陆的国家
use Khsing\World\Models\Continent;

$asia = Continent::getByCode('AS');
$countries = $asia->countries()->get();
// or use children method
$countries = $asia->children();
  • 获取大陆或父级
$china = Country::getByCode('cn');
$asia = $china->parent();
  • 通过国家获取地区/州/省
$china = Country::getByCode('cn');
$provinces = $china->divisions()->get()
// or use children method
$provinces = $china->children();
  • 通过国家或地区获取城市
$china = Country::getByCode('cn');
// check has_division to determine next level is division or city.
$china->has_division; // true, otherwise is false
$regsions = $china->children();

贡献

如果您想为此库做出贡献,请提交问题或pr,请遵循以下步骤。

  1. 启动一个新的Laravel项目并安装此库。
  2. 安装 orangehill/iseed
  3. 通过SQL修改数据。
  4. 通过artisan iseed world_cities,world_cities_locale,world_continents,world_continents_locale,world_countries,world_countries_locale,world_divisions,world_divisions_locale生成种子
  5. delete()替换为truncate()cd database/seeders/ && sed -i 's/->delete()/->truncate()/g' World*.php
  6. 将种子文件复制到库中。
  7. 提交您的作品。 ;)

TODO

  • 更改种子数据的方式,例如从JSON加载数据?
  • 添加前端支持
  • 找到一种更新数据集的方法

数据源

感谢

关于

此包在MIT许可证下发布。如果您有任何问题或建议,请随时提交问题,或发送电子邮件给我 Guixing<khsing.cn(AT)gmail.com>。

祝您愉快。