markenwerk / iso3166-country-information
3.0.2
2021-01-18 17:41 UTC
Requires
- php: >=5.3
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: >=4.8.26
README
ISO3166 工具类,用于验证和列出国家代码,并为识别的国家获取详细信息。
安装
{
"require": {
"chroma-x/iso3166-country-information": "~3.0"
}
}
使用方法
自动加载和命名空间
require_once('path/to/vendor/autoload.php');
use ChromaX\Iso3166Country\Iso3166CountryInformation;
use ChromaX\Iso3166Country\Iso3166Country;
获取 ISO3166 中所有现有国家的信息
获取所有国家,以包含国家信息的哈希表数组形式
$arrayOfIso3166Countries = Iso3166CountryInformation::getCountryData();
结果
…
[RU] => Array
(
[iso3166_alpha2] => RU
[iso3166_alpha3] => RUS
[iso3166_numeric] => 643
[iso3166_2] => RU
[un] => RU
[tld] => ru
[name] => Russische Föderation
)
[SB] => Array
(
[iso3166_alpha2] => SB
[iso3166_alpha3] => SLB
[iso3166_numeric] => 090
[iso3166_2] => SB
[un] => SB
[tld] => sb
[name] => Salomonen
)
…
获取所有国家,以 Iso3166Country 对象数组形式
$arrayOfIso3166CountryObjects = Iso3166CountryInformation::getCountries();
结果
…
[CX] => Iso3166Country\Iso3166Country Object
(
[iso3166Alpha2CountryCode:Iso3166Country\Iso3166Country:private] => CX
[iso3166Alpha3CountryCode:Iso3166Country\Iso3166Country:private] => CXR
[iso3166NumericCountryCode:Iso3166Country\Iso3166Country:private] => 162
[iso3166_2CountryCode:Iso3166Country\Iso3166Country:private] => CX
[toplevelDomain:Iso3166Country\Iso3166Country:private] => cx
[unitedNationsCountryCode:Iso3166Country\Iso3166Country:private] => CX
[name:Iso3166Country\Iso3166Country:private] => Weihnachtsinsel
)
[EH] => Iso3166Country\Iso3166Country Object
(
[iso3166Alpha2CountryCode:Iso3166Country\Iso3166Country:private] => EH
[iso3166Alpha3CountryCode:Iso3166Country\Iso3166Country:private] => ESH
[iso3166NumericCountryCode:Iso3166Country\Iso3166Country:private] => 732
[iso3166_2CountryCode:Iso3166Country\Iso3166Country:private] => EH
[toplevelDomain:Iso3166Country\Iso3166Country:private] => eh
[unitedNationsCountryCode:Iso3166Country\Iso3166Country:private] => EH
[name:Iso3166Country\Iso3166Country:private] => Westsahara
)
…
获取所有国家,以包含 HTML 选项标签的字符串形式
// Argument 1 ('DE') ist the selected option,
// argument 2 (ISO3166_ALPHA2) property is the value,
// argument 2 (NAME) property is the label.
$optionsOfIso3166Countries = Iso3166CountryInformation::getSelectOptions(
'DE',
Iso3166CountryInformation::ISO3166_ALPHA2,
Iso3166CountryInformation::NAME
);
结果
<option value="AF">Afghanistan</option>
<option value="EG">Ägypten</option>
<option value="AX">Åland</option>
<option value="AL">Albanien</option>
<option value="DZ">Algerien</option>
<option value="AS">Amerikanisch-Samoa</option>
<option value="VI">Amerikanische Jungferninseln</option>
<option value="AD">Andorra</option>
…
<option value="DE" selected="selected">Deutchland</option>
…
通过 ISO3166 属性之一识别单个国家的信息
获取 ISO3166Alpha2 国家代码 'de'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByIso3166Alpha2('de');
获取 ISO3166Alpha3 国家代码 'deu'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByIso3166Alpha3('deu');
获取 ISO3166Numeric 国家代码 '276'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByIso3166Numeric(276);
获取 ISO3166-2 国家代码 'de'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByIso3166v2('de');
获取 ISO3166 顶级域名 'de'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByToplevelDomain('de');
获取 ISO3166 联合国标识符 'de'(德国)的信息
$iso3166Country = Iso3166CountryInformation::getByUnitedNationsId('de');
结果
$iso3166Country => Iso3166Country\Iso3166Country Object
(
[iso3166Alpha2CountryCode:Iso3166Country\Iso3166Country:private] => DE
[iso3166Alpha3CountryCode:Iso3166Country\Iso3166Country:private] => DEU
[iso3166NumericCountryCode:Iso3166Country\Iso3166Country:private] => 276
[iso3166_2CountryCode:Iso3166Country\Iso3166Country:private] => DE
[toplevelDomain:Iso3166Country\Iso3166Country:private] => de
[unitedNationsCountryCode:Iso3166Country\Iso3166Country:private] => DE
[name:Iso3166Country\Iso3166Country:private] => Deutschland
)
验证ISO3166属性是否有效(存在)
ISO3166Alpha2国家代码'de'是否存在
$countryExists = Iso3166CountryInformation::validateIso3166Alpha2('de');
ISO3166Alpha3国家代码'deu'是否存在
$countryExists = Iso3166CountryInformation::validateIso3166Alpha3('deu');
ISO3166Numeric国家代码'276'是否存在
$countryExists = Iso3166CountryInformation::validateIso3166Numeric(276);
ISO3166-2国家代码'de'是否存在
$countryExists = Iso3166CountryInformation::validateIso3166v2('de');
ISO3166顶级域名'de'是否存在
$countryExists = Iso3166CountryInformation::validateToplevelDomain('de');
ISO3166联合国标识'de'是否存在
$countryExists = Iso3166CountryInformation::validateUnitedNationsId('de');
所有验证方法返回一个布尔值。
Iso3166Country类的成员方法
通过国家代码创建对象
use ChromaX\Iso3166Country\Iso3166CountryInformation;
use ChromaX\Iso3166Country\Iso3166Country;
$country = new Iso3166Country();
$country->loadByIso3166Alpha2CountryCode('DE');
创建自定义国家信息对象
use Iso3166Country\Iso3166CountryInformation;
use Iso3166Country\Iso3166Country;
$utopia = array(
Iso3166CountryInformation::ISO3166_ALPHA2 => 'UO',
Iso3166CountryInformation::ISO3166_ALPHA3 => 'UTO',
Iso3166CountryInformation::ISO3166_NUMERIC => 42,
Iso3166CountryInformation::ISO3166_2 => 'UO',
Iso3166CountryInformation::UNITED_NATIONS_ID => 'uo',
Iso3166CountryInformation::TOP_LEVEL_DOMAIN => 'uo',
Iso3166CountryInformation::NAME => 'Utopia'
);
$country = new Iso3166Country();
$country->loadByIso3166CountryInformation($utopia);
属性获取器
$iso3166Alpha2 = $country->getIso3166Alpha2CountryCode();
$iso3166Alpha3 = $country->getIso3166Alpha3CountryCode();
$iso3166Numeric = $country->getIso3166NumericCountryCode();
$iso3166_2 = $country->getIso3166v2CountryCode();
$toplevelDomain = $country->getToplevelDomain();
$unitedNationsIdentifier = $country->getUnitedNationsId();
$name = $country->getName();
贡献
对我们的项目做出贡献总是非常受欢迎的。
但:请遵循在CONTRIBUTING.md文档中写下的贡献指南。
许可证
PHP ISO3166 Country Information遵循MIT许可证。