tiny-blocks/country
Composer 安装命令:
composer require tiny-blocks/country
包简介
Provides ISO 3166-1 country and ISO 3166-2 subdivision value objects for PHP, with Alpha-2, Alpha-3, numeric, and IANA timezone resolution.
关键字:
README 文档
README
Overview
Provides ISO 3166-1 country and
ISO 3166-2 subdivision value objects for PHP. A Country is a lazy
handle over an Alpha-2 code that resolves its name, its Alpha-3 and numeric codes, its
IANA timezones, and its subdivisions on demand. A Subdivision is the same kind of handle one
level down, resolving its name, its category, and its owning country from the ISO 3166-2 code.
Construction works from any of the three code variants (Alpha-2, Alpha-3, or numeric), with automatic conversion between them. The data is generated from the ISO 3166 standard. Names are the official ISO English short names, so the values are stable and citable rather than derived. The library builds on the tiny-blocks value-object contract for immutability and structural equality, on tiny-blocks/time for timezones, and on tiny-blocks/collection for the country and subdivision collections.
Installation
composer require tiny-blocks/country
How to use
Country codes
Each country has three ISO 3166-1 representations: a two-letter Alpha2Code, a three-letter Alpha3Code, and a
three-digit NumericCode. Every code converts to the other two and to its string value.
<?php declare(strict_types=1); use TinyBlocks\Country\Alpha2Code; $alpha2 = Alpha2Code::BRAZIL; $alpha2->value; # BR $alpha2->toAlpha3()->value; # BRA $alpha2->toNumeric()->value; # 076 $alpha2->toString(); # BR
The numeric code keeps the ISO leading zeros as a string and exposes them as an integer through toInteger.
<?php declare(strict_types=1); use TinyBlocks\Country\NumericCode; $numeric = NumericCode::BRAZIL; $numeric->value; # 076 $numeric->toInteger(); # 76 $numeric->toAlpha2()->value; # BR $numeric->toAlpha3()->value; # BRA
Country
A Country can be created from any code object with from, or from a string with fromString. The name, both alpha
codes, the numeric code, the timezones, and the subdivisions are read through methods.
Creating from objects
<?php declare(strict_types=1); use TinyBlocks\Country\Alpha2Code; use TinyBlocks\Country\Country; $country = Country::from(code: Alpha2Code::BRAZIL); $country->name(); # Brazil $country->alpha2()->value; # BR $country->alpha3()->value; # BRA $country->numeric()->value; # 076
The same country is produced from its Alpha-3 or numeric code, since from canonicalizes to the Alpha-2 code.
<?php declare(strict_types=1); use TinyBlocks\Country\Country; use TinyBlocks\Country\NumericCode; $country = Country::from(code: NumericCode::UNITED_STATES_OF_AMERICA); $country->name(); # United States $country->alpha2()->value; # US $country->alpha3()->value; # USA
Creating from string
fromString accepts an Alpha-2, Alpha-3, or numeric code as a string. It throws InvalidCountryCode when the string
matches no known code.
<?php declare(strict_types=1); use TinyBlocks\Country\Country; Country::fromString(code: 'BR')->name(); # Brazil Country::fromString(code: 'USA')->name(); # United States Country::fromString(code: '076')->name(); # Brazil
Safe creation
Use tryFromString to validate external input without handling exceptions. It returns null when the code matches no
known Alpha-2, Alpha-3, or numeric code.
<?php declare(strict_types=1); use TinyBlocks\Country\Country; Country::tryFromString(code: 'BR')?->alpha2()->value; # BR Country::tryFromString(code: 'XYZ'); # null
Subdivisions
Each Country exposes its ISO 3166-2 subdivisions as a Subdivisions collection, which may be empty for territories
that have none. A Subdivision can also be created directly from its code with fromString or tryFromString.
<?php declare(strict_types=1); use TinyBlocks\Country\Country; use TinyBlocks\Country\Subdivision; $subdivision = Subdivision::fromString(code: 'BR-SP'); $subdivision->code(); # BR-SP $subdivision->name(); # São Paulo $subdivision->category()->value; # State $subdivision->category()->isState(); # true $subdivision->country()->name(); # Brazil $subdivision->belongsTo(country: Country::fromString(code: 'BR')); # true
<?php declare(strict_types=1); use TinyBlocks\Country\Alpha2Code; use TinyBlocks\Country\Country; $subdivisions = Country::from(code: Alpha2Code::BRAZIL)->subdivisions(); $subdivisions->count(); # 27 $subdivisions->isEmpty(); # false
All countries
Countries::all returns every ISO 3166-1 country as a collection of Country handles.
<?php declare(strict_types=1); use TinyBlocks\Country\Countries; $countries = Countries::all(); $countries->count(); # 249
Timezones
Every Country exposes a CountryTimezones collection, built from the IANA timezone database through PHP. The first
identifier is the default, and it falls back to UTC for territories without an assigned timezone.
<?php declare(strict_types=1); use TinyBlocks\Country\Alpha2Code; use TinyBlocks\Country\Country; $timezones = Country::from(code: Alpha2Code::JAPAN)->timezones(); $timezones->count(); # 1 $timezones->default()->value; # Asia/Tokyo $timezones->contains(iana: 'Asia/Tokyo'); # true $timezones->contains(iana: 'America/New_York'); # false $timezones->findByIdentifierOrUtc(iana: 'X')->value; # UTC
License
Country is licensed under MIT.
Contributing
Please follow the contributing guidelines to contribute to the project.
tiny-blocks/country 适用场景与选型建议
tiny-blocks/country 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.42k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「timezone」 「country」 「numeric」 「iana」 「alpha-2」 「alpha-3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tiny-blocks/country 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tiny-blocks/country 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tiny-blocks/country 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sun Country is the package that helps you to get the country name & dialing code by the country ISO 3166-1 Alpha-2 code.
List of all countries with names and ISO 3166-1 codes in all languages and data formats for Laravel
Datetime helpers for timezone handling
Country select field type.
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
Official PHP SDK for the IPGeolocation.io IP Location API with single and bulk lookup support.
统计信息
- 总下载量: 22.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-26