lykegenes/laravel-countries

获取国家信息。

2.0.1 2022-05-06 17:53 UTC

This package is auto-updated.

Last update: 2024-09-09 18:54:03 UTC


README

Latest Version on Packagist Software License Build Status Total Downloads

本软件包可让您轻松访问来自每个国家/地区的数据。

安装

通过Composer

composer require lykegenes/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->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',
    ...
]

鸣谢

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件