brianfp/lumen-world

提供国家、州和城市模型数据库。

v1.0 2020-07-09 18:54 UTC

This package is auto-updated.

Last update: 2024-09-17 00:11:39 UTC


README

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

概念

本包中有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。地区设置遵循 config/app.php 中的Lumen项目设置。

设置

  • composer require
composer require brianfp/lumen-world

用法

  • 获取所有大陆
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() 替换为 truncat()cd database/seeds/ && sed -i 's/->delete()/->truncat()/g' World*.php
  6. 将种子文件复制到库中。
  7. 提交您的作品。 ;)

待办事项

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

谢谢

关于

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

祝您愉快。