hollodotme/iso-country-codes

A value object collection representing ICO-3166-1 country codes

v1.0.0 2020-08-17 16:35 UTC

This package is not auto-updated.

Last update: 2024-11-21 06:14:00 UTC


README

Unit tests Static analysis

ISO-3166-1 country codes

This is a statically generated value object collection of ISO-3166-1 country codes with their following representations:

  • English short name
  • French short name
  • Alpha-2 code
  • Alpha-3 code
  • Numeric code

Source of the data is: https://www.iso.org/obp/ui/#search/code/

Requirements

  • PHP >= 7.4

Installation

The recommended way to install this library is using composer.

composer require "hollodotme/iso-country-codes"

Usage

From English short name

<?php declare(strict_types=1);

use hollodotme\ISO\CountryCodes\CountryCode;

$countryCode = CountryCode::fromEnglishShortName( 'New Caledonia' );

echo $countryCode->getEnglishShortName(), "\n";
echo $countryCode->getFrenchShortName(), "\n";
echo $countryCode->getAlpha2Code(), "\n";
echo $countryCode->getAlpha3Code(), "\n";
echo $countryCode->getNumericCode();

Prints:

New Caledonia
Nouvelle-Calédonie (la)
NC
NCL
540

From French short name

<?php declare(strict_types=1);

use hollodotme\ISO\CountryCodes\CountryCode;

$countryCode = CountryCode::fromFrenchShortName( 'Nouvelle-Calédonie (la)' );

echo $countryCode->getEnglishShortName(), "\n";
echo $countryCode->getFrenchShortName(), "\n";
echo $countryCode->getAlpha2Code(), "\n";
echo $countryCode->getAlpha3Code(), "\n";
echo $countryCode->getNumericCode();

Prints:

New Caledonia
Nouvelle-Calédonie (la)
NC
NCL
540

From alpha-2 code

<?php declare(strict_types=1);

use hollodotme\ISO\CountryCodes\CountryCode;

$countryCode = CountryCode::fromAlpha2Code( 'NC' );

echo $countryCode->getEnglishShortName(), "\n";
echo $countryCode->getFrenchShortName(), "\n";
echo $countryCode->getAlpha2Code(), "\n";
echo $countryCode->getAlpha3Code(), "\n";
echo $countryCode->getNumericCode();

Prints:

New Caledonia
Nouvelle-Calédonie (la)
NC
NCL
540

From alpha-3 code

<?php declare(strict_types=1);

use hollodotme\ISO\CountryCodes\CountryCode;

$countryCode = CountryCode::fromAlpha3Code( 'NCL' );

echo $countryCode->getEnglishShortName(), "\n";
echo $countryCode->getFrenchShortName(), "\n";
echo $countryCode->getAlpha2Code(), "\n";
echo $countryCode->getAlpha3Code(), "\n";
echo $countryCode->getNumericCode();

Prints:

New Caledonia
Nouvelle-Calédonie (la)
NC
NCL
540

From numeric code

<?php declare(strict_types=1);

use hollodotme\ISO\CountryCodes\CountryCode;

$countryCode = CountryCode::fromNumericCode( '540' );

echo $countryCode->getEnglishShortName(), "\n";
echo $countryCode->getFrenchShortName(), "\n";
echo $countryCode->getAlpha2Code(), "\n";
echo $countryCode->getAlpha3Code(), "\n";
echo $countryCode->getNumericCode();

Prints:

New Caledonia
Nouvelle-Calédonie (la)
NC
NCL
540