php-components/isocodes

ISO代码包的PHP实现

v1.0.0-beta.2 2016-10-25 15:23 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:01:17 UTC


README

Build Status

基于开源项目 iso-codes 的PHP ISO代码。

本项目包含

  • ISO-15924
  • ISO-3166-1
  • ISO-3166-2
  • ISO-3166-3
  • ISO-4217
  • ISO-639-2
  • ISO-639-3
  • ISO-639-5

...以及通过 Translation Project 管理的适当翻译。

安装

安装此库最简单、推荐的方式是通过 composer

composer require php-components/isocodes

适配器

适配器用于加载ISO数据。目前我们支持以下适配器

  • Json
  • Pdo

然而,已经实现了接口,您可以编写自己的自定义适配器。如果您编写自己的适配器,请注意,它必须将翻译对象注入到对象中。

get($code) 将确定提供的参数是哪种类型的代码,并将搜索该类型的代码。允许的类型是Alpha-2、Alpha-3、Alpha-4和数字代码(如果请求的ISO中可用)。

一些ISO文件,如ISO-639,有额外的代码,符合Alpha-2、Alpha-3、Alpha-4或数字代码的模式。对于这些特殊情况,为其适配器添加了额外的功能。

JSON适配器

此适配器使用 iso-codes JSON文件提供ISO数据。数据作为数组存储在适配器接口中。

用法示例

use ISOCodes\ISO3166_1\Adapter\Json as ISO3166_1Adapter;

$adapter = new ISO3166_1Adapter();
// Get country with Alpha-2 code 'es' (Spain)
$country = $adapter->get('es');

if (null !== $country) {
    // Get the country name in Spanish ('es')
    echo $country->getName('es');
} else {
    echo 'Country not found!';
}

这将返回 España

PDO适配器

此适配器使用PDO从数据库后端检索ISO数据。默认情况下,它将使用包含的SQLite数据库,但您可以在适配器的构造函数中指定另一个PDO。构造函数定义如下

public function __construct(PDO $pdo = null)

$pdo 将是您的PDO对象,或者您可以将它留为null以加载默认SQLite数据库。

用法示例

use ISOCodes\ISO3166_1\Adapter\Pdo as ISO3166_1Adapter;

$adapter = new ISO3166_1Adapter();
// Get country with Alpha-2 code 'es' (Spain)
$country = $adapter->get('es');

if (null !== $country) {
    // Get the country name in Spanish ('es')
    echo $country->getName('es');
} else {
    echo 'Country not found!';
}

这将返回 España