A Collection of ISO standards as PHP Enums

Packages

June 16th, 2022

A Collection of ISO standards as PHP Enums

The PrinsFrank/standards package is a collection of standards as PHP Enums, such as ISO3166, ISO4217, ISO639, and more. Specifically, this package includes support for common language ISO codes, country codes, and currencies.

For example, ISO 3166 deals with country code standards, such as two-letter country codes, three letter cords, and alpha-numeric codes:

// ISO3166_1_Alpha_2::Netherlands
$valueAlpha2 = ISO3166_1_Alpha_2::from('NL');
$valueAlpha2->value; // 'NL'
$valueAlpha2->name; // 'Netherlands'
 
// ISO3166_1_Alpha_3::Netherlands
$valueAlpha2->toISO3166_1_Alpha_3();
// ISO3166_1_Numeric::Netherlands
$valueAlpha2->toISO3166_1_Numeric();
 
// Alpha-3
// ISO3166_1_Alpha_3::Netherlands
$valueAlpha3 = ISO3166_1_Alpha_3::from('NLD');
$valueAlpha3->value; // 'NLD'
$valueAlpha3->name; // 'Netherlands'
 
// Numeric
// ISO3166_1_Numeric::Netherlands
ISO3166_1_Numeric::from('528');
ISO3166_1_Numeric::fromInt(528);
 
$valueNumeric->value; // '528'
$valueNumeric->name; // 'Netherlands'

This package also includes support for ISO 4217 currency codes:

// Alpha-3
// ISO4217_Alpha3::Euro
$valueAlpha3 = ISO4217_Alpha3::from('EUR');
$valueAlpha3->value; // 'EUR'
$valueAlpha3->name; // 'Euro'
 
// Numeric
// ISO4217_Numeric::Euro
$valueNumeric = ISO4217_Numeric::from('978');
$valueNumeric = ISO4217_Numeric::fromInt(978);
$valueNumeric->value; // '978'
$valueNumeric->name; // 'Euro'

Language support (ISO 639) is another helpful standard enum this package provides:

// ISO639_1_Alpha_2::Dutch_Flemish
$valueAlpha2 = ISO639_1_Alpha_2::from('nl');
$valueAlpha2->value; // 'nl'
$valueAlpha2->name; // 'Dutch_Flemish'

Enums are supported in PHP as of PHP 8.1. Laravel also includes Enum features such as casting Eloquent attributes to PHP "backed" enums and implicit enum route binding that you might want to check out if you're new to Enums.

You can learn about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.