opheus2 / laravel-countries
获取国家和其货币的信息。
v1.0.1
2024-06-04 00:04 UTC
Requires
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.3|^1.4|^1.5|^1.6
- orchestra/testbench: ^3.8|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-04 00:45:11 UTC
README
此包让您轻松访问来自每个国家/地区的数据。支持Laravel 7以上版本。
原始 包 由 Patrick Samson 维护,现已不再活跃,因此我决定进行分支。
特性
-
通过国家的2字母、3字母或3位数字ISO3166代码获取国家。
-
通过地区或子地区获取国家列表。
-
通过货币获取国家列表。
-
获取国家列表作为简单的PHP数组,用于下拉列表。
-
国家
- 获取2字母ISO3166代码。
- 获取3字母ISO3166代码。
- 获取3位数字ISO3166代码。
- 获取官方名称。
- 获取通用名称。
- 获取货币对象。
- 获取货币对象集合。
-
货币
- 获取货币代码。
- 获取货币名称。
- 获取货币符号。
-
CountryCast
- 一个自定义的Eloquent类型转换,用于在数据库中存储Country对象。
-
即将推出
- 通过语言获取国家列表。
- 通过电话区号获取国家列表。
- 通过时区获取国家列表。
- 通过顶级域名获取国家列表。
- 通过大洲获取国家列表。
- 通过2字母ISO3166代码获取国家国旗。 (大小、类型、样式等)
- blade组件用于国家下拉列表
- blade组件用于国家国旗
- 获取国家的Google Maps URL
安装
通过Composer
composer require opheus2/laravel-countries
用法
您可以使用以下唯一的国籍代码之一搜索特定国家
$country = \Countries::getByAlpha2Code('CA'); // 2-letters country code from ISO3166 $country = \Countries::getByAlpha3Code('CAN'); // 3-letters country code from ISO3166 $country = \Countries::getByNumericCode(124); // 3-digits country code from ISO3166
您可以使用地区或货币检索国家数组
$countries = \Countries::getByCurrency('CAD'); /// A 3-letters currency code // Search by region $countries = \Countries::getByRegion(\Countries::$REGION_AFRICA); $countries = \Countries::getByRegion(\Countries::$REGION_AMERICAS); $countries = \Countries::getByRegion(\Countries::$REGION_ANTARCTICA); $countries = \Countries::getByRegion(\Countries::$REGION_ASIA); $countries = \Countries::getByRegion(\Countries::$REGION_EUROPE); $countries = \Countries::getByRegion(\Countries::$REGION_OCEANIA);
结果将返回为Country对象。这些对象具有以下辅助方法
$country = \Countries::getByAlpha3Code('CAN'); $country->getAlpha2Code(); // 'CA' $country->getAlpha3Code(); // 'CAN' $country->getNumericCode(); // 124 $country->getOfficialName(); // 'Canada' $country->getCommonName(); // 'Canada' $country->getCurrency(); // The first Currency object $country->getCurrencies(); // A collection of Currency objects $currency = $country->getCurrency(); $currency->getCode(); // 'CAD' $currency->getName(); // 'Canadian dollar' $currency->getSymbol(); // '$' $country->getAttributes(); // An array of all the raw attributes.
也可以生成简单的PHP数组,可用于生成HTML选择输入。
// All parameters are optionnal. These are the defaults. // $key The country attribute to use as key. (default: 'cca3', 3-letters country code from ISO3166) // $official True for the offical country name, False for the common name. (default: false) // $localization A 3-letter locale code to try to translate. Will default to English if it`s missing. (default: null) $countries = \Countries::getListForDropdown('cca3', false, 'fra'); //This will return the following [ 'CAN' => 'Canada', 'USA' => 'États-Unis', ... ]
宏
此包实现了Laravel Macroable
特性,允许在 Country
上使用宏和混入。
示例用法
use Orpheus\LaravelCountries\Country; Country::macro( 'getFlag', fn () => sprintf('https://www.countryflags.io/%s/flat/64.png', $this->getAlpha2Code()) ); $country = \Countries::getByAlpha3Code('CAN'); $flag = $country->getFlag(); // Output: https://www.countryflags.io/CA/flat/64.png
混入
除了宏,还支持混入。这允许将另一个类的其他方法合并到Country中。
定义混入类
class CustomCountry { public function getFlag(): string { return sprintf('https://www.countryflags.io/%s/flat/64.png', $this->getAlpha2Code()); } }
通过传递一个类的实例来注册混入
Country::mixin(new CustomCountry);
自定义类的方法将可用
$country = \Countries::getByAlpha3Code('CAN'); $flag = $country->getFlag();
致谢
- Orpheus 对分支的贡献。
- Patrick Samson 对初始包的贡献。
- Mohammed Le Doze 在此存储库中整理所有数据。
- 所有贡献者
许可
MIT许可证(MIT)。有关更多信息,请参阅许可文件。