Value Object representing a country using ISO-3166 specifications, with built-in support for IANA timezones.
composer require tiny-blocks/countryThe library exposes country codes according to ISO-3166 specifications. It is possible to create a representation of a country that groups the codes, its name, and all its IANA timezones.
A two-letter code that represents a country name, recommended as the general purpose code.
use TinyBlocks\Country\Alpha2Code;
$alpha2Code = Alpha2Code::UNITED_STATES_OF_AMERICA;
$alpha2Code->name; # UNITED_STATES_OF_AMERICA
$alpha2Code->value; # US
$alpha2Code->toAlpha3()->value; # USAA three-letter code that represents a country name, which is usually more closely related to the country name.
use TinyBlocks\Country\Alpha3Code;
$alpha3Code = Alpha3Code::UNITED_STATES_OF_AMERICA;
$alpha3Code->name; # UNITED_STATES_OF_AMERICA
$alpha3Code->value; # USA
$alpha3Code->toAlpha2()->value; # USA Country instance can be created using either an Alpha-2 or Alpha-3 code, along with an optional country name.
There are two main methods to create a Country object: from (which accepts objects) and fromString (which accepts
strings).
Each Country automatically carries all its IANA timezones.
You can create a Country instance using the from method by providing an Alpha2Code or Alpha3Code object.
Optionally, you can pass the name of the country. If no name is provided, the default is the English version of the
country name.
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::UNITED_STATES_OF_AMERICA);
$country->name; # United States of America
$country->alpha2->value; # US
$country->alpha3->value; # USAuse TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha3Code;
$country = Country::from(alphaCode: Alpha3Code::UNITED_STATES_OF_AMERICA);
$country->name; # United States of America
$country->alpha2->value; # US
$country->alpha3->value; # USAIf you want to specify a custom name:
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha3Code;
$country = Country::from(alphaCode: Alpha3Code::UNITED_STATES_OF_AMERICA, name: 'United States');
$country->name; # United States
$country->alpha2->value; # US
$country->alpha3->value; # USAAlternatively, you can create a Country instance using the fromString method, which accepts an Alpha-2 or
Alpha-3 code as a string.
use TinyBlocks\Country\Country;
$country = Country::fromString(alphaCode: 'US');
$country->name; # United States of America
$country->alpha2->value; # US
$country->alpha3->value; # USAYou can also pass a custom country name:
use TinyBlocks\Country\Country;
$country = Country::fromString(alphaCode: 'USA', name: 'United States');
$country->name; # United States
$country->alpha2->value; # US
$country->alpha3->value; # USAEvery Country includes an immutable CountryTimezones collection, built from the IANA timezone database (via PHP's
ICU integration).
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::BRAZIL);
$country->timezones->count(); # 4
$country->timezones->default(); # Timezone("America/Noronha")
$country->timezones->toStrings(); # ["America/Noronha", "America/Belem", "America/Sao_Paulo", ...]Returns all Timezone objects for the country:
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::BRAZIL);
$country->timezones->all(); # [Timezone("America/Noronha"), Timezone("America/Belem"), ...]Returns the primary timezone (first in the IANA list). Falls back to UTC for territories without an assigned timezone:
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::JAPAN);
$country->timezones->default(); # Timezone("Asia/Tokyo")
$country = Country::from(alphaCode: Alpha2Code::BOUVET_ISLAND);
$country->timezones->default(); # Timezone("UTC")Searches for a specific IANA identifier within the country's timezones. Returns UTC if not found.
use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::UNITED_STATES_OF_AMERICA);
$country->timezones->findByIdentifierOrUtc(iana: 'America/New_York'); # Timezone("America/New_York")
$country->timezones->findByIdentifierOrUtc(iana: 'Asia/Tokyo'); # Timezone("UTC")use TinyBlocks\Country\Country;
use TinyBlocks\Country\Alpha2Code;
$country = Country::from(alphaCode: Alpha2Code::JAPAN);
$country->timezones->contains(iana: 'Asia/Tokyo'); # true
$country->timezones->contains(iana: 'America/New_York'); # falseCountry is licensed under MIT.
Please follow the contributing guidelines to contribute to the project.