定制 jabarihunt/country-codes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jabarihunt/country-codes

Composer 安装命令:

composer require jabarihunt/country-codes

包简介

A simple class that returns country codes (or country name).

README 文档

README

This is a simple class used to get country codes and/or names. Normally this is something that would be stored and accessed from a database. However, this class will come in handy in situations where you...

  1. Are running your application someplace without access to a database
  2. Don't have proper permissions to add tables to a database
  3. Outside of a countries table, your application doesn't need a database

Requires PHP 7.0+ (due to type definitions)

INSTALLING

Via Composer

Run the following command in the same directory as your composer.json file:

composer require jabarihunt/country-codes

Via Github

  1. Clone this repository into a working directory: git clone git@github.com:jabarihunt/country-codes .

  2. Include the CountryCodes class in your project...

require('/path/to/CountryCodes.php')

...or if using an auto-loader...

 use jabarihunt/CountryCodes;

USAGE

Available Methods

There are two static methods publicly available in the class...

  • get( int $codeType, string $code, bool $useAPI = FALSE ) This function accepts a code type (availabe as class constants) and a corresponding value. The function returns an array containing the country name, 2 letter country code, 3 letter country code, and UN country code.

  • getAll( bool $useAPI = FALSE ) Returns a multidemintional array containing the country name, 2 letter country code, 3 letter country code, and UN country code for ALL countries.

NOTE: The countries data is hard coded into the class as an array. However, the optional parameter ($useAPI) will spawn an HTTP request to the REST Countries API to retrieve live data. It's rare that countries change, but it does happen!

Additionally, there are five publicly available constants that represent class data types:

public const CountryCodes::CODE_TYPE_COUNTRY_NAME
public const CountryCodes::CODE_TYPE_NATIVE_NAME
public const CountryCodes::CODE_TYPE_2_LETTER
public const CountryCodes::CODE_TYPE_3_LETTER
public const CountryCodes::CODE_TYPE_UN

Retrieving Data For A Single Country

Suppose we have a country name, Haiti for example, and we want the 3 letter country code. We will use the get() method by passing it the type of value (country name in this case) along with the value itself.

<?php

    $countryName     = 'Haiti';
    $countryCodeType = CountryCodes::CODE_TYPE_COUNTRY_NAME;
    $countryCodes    = CountryCodes::get($countryCodeType, $countryName);
    
    var_dump($countryCodes);

?>
/* OUTPUT */

array(5) {
    ["name"]=> string(5) "Haiti"
    ["nativeName"]=> string(6) "Haïti"
    ["2letter"]=> string(2) "HT"
    ["3letter"]=> string(3) "HTI"
    ["un"]=> string(3) "332"
}

As you can see, this method returns an array containing the country's official name, native name (in it's native language), 2 letter country code, 3 letter country code, and United Nations numeric code. This will be the case regardless if the source is the hard coded class data or from an HTTP request to the REST Countries API. The following would return the exact same output (note the use of the third optional parameter):

CountryCodes::get($countryCodeType, $countryName, TRUE)

NOTE: Acknowledged country names are exactly as in the REST Countries API. Most are straight forward as with "Haiti" in the example above. However, some aren't so obvious, such as "Venezuela (Bolivarian Republic of)" rather than just "Venezuela"!

The class can be used as a reverse lookup if you happened to have a country code and wish to receive a country name (or other data point from the class). The below example would return the exact same output as the one above:

<?php

    $unitedNationsCode = '332';
    $countryCodeType   = CountryCodes::CODE_TYPE_UN;
    $countryCodes      = CountryCodes::get($countryCodeType, $unitedNationsCode);

?>

Retrieving All Countries

There may be instances where you want to retrieve all country codes. The below example will return a multidimensional array of country codes with the same keys as in the example above.

<?php

    // RETURN HARD CODED CLASS VALUES
    $allCountryCodes = CountryCodes::getAll(); 
    
    /* OR */

    // RETURN DATA VIA HTTP REQUEST TO THE REST COUNTRIES API 
    $allCountryCodes = CountryCodes::getAll(TRUE);
    
?>

CONTRIBUTING

  1. Fork Repository
  2. Create a descriptive branch name
  3. Make edits to your branch
  4. Squash (rebase) your commits
  5. Create a pull request

LICENSE

This project is licensed under the MIT License - see the LICENSE.md file for details.

jabarihunt/country-codes 适用场景与选型建议

jabarihunt/country-codes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 01 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「country code」 「UN」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jabarihunt/country-codes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jabarihunt/country-codes 我们能提供哪些服务?
定制开发 / 二次开发

基于 jabarihunt/country-codes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-06