PHPackages                             bradietilley/country-enums - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bradietilley/country-enums

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bradietilley/country-enums
==========================

All (or at least most) countries and their regions formatted as PHP enums

1.5.0(3y ago)518.2k↑67.6%1MITPHPPHP ^8.0

Since Sep 6Pushed 3y ago1 watchersCompare

[ Source](https://github.com/bradietilley/country-enums)[ Packagist](https://packagist.org/packages/bradietilley/country-enums)[ RSS](/packages/bradietilley-country-enums/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (4)Versions (9)Used By (0)

PHP Country (and Region) Enums
==============================

[](#php-country-and-region-enums)

[![Static Analysis](https://github.com/bradietilley/country-enums/actions/workflows/static.yml/badge.svg)](https://github.com/bradietilley/country-enums/actions/workflows/static.yml/badge.svg)[![Tests](https://github.com/bradietilley/country-enums/actions/workflows/tests.yml/badge.svg)](https://github.com/bradietilley/country-enums/actions/workflows/tests.yml/badge.svg)

All (or at least most) countries and their regions formatted as PHP enums, framework agnostic (of course).

Includes automatic relationships between Countries and Regions, and comes out of the box with svg &amp; png flags. See Authors section for due credit.

Includes a small handful of Laravel helper methods which are made available if this is installed on a Laravel app.

Requirements
------------

[](#requirements)

PHP 8.1+

Installation
------------

[](#installation)

Install it via composer:

```
composer require bradietilley/country-enums

```

Usage
-----

[](#usage)

#### Country Enum

[](#country-enum)

All countries are defined by their two-letter codes (*ISO 3166-1 alpha-2?*).

```
$country = Country::US;
$country = Country::from('US');
```

Method/AccessorDescriptionExampleType$country-&gt;valueGet the two-letter code"US"string$country-&gt;label()Get the human-readable label"United States"string$country-&gt;code()Get the English snake\_case code name"united\_states"string$country-&gt;regions()Get all regions in the country\[ Region::US\_AL, ..., Region::US\_WY \]array&lt;Region&gt;$country-&gt;collectRegions()Get all regions in the country as a collection (requires Laravel if used)Collection::make(\[ Region::US\_AL, ..., Region::US\_WY \])\\Illuminate\\Support\\Collection&lt;Region&gt;$country-&gt;regionsValues()Get all region values (e.g. 'AU\_NSW') in the country\[ 'US\_AL', ..., 'US\_WY' \]array&lt;string&gt;$country-&gt;collectRegionsValues()Get all region values (e.g. 'AU\_NSW') in the country as a collection (requires Laravel if used)Collection:make(\[ 'US\_AL', ..., 'US\_WY' \])\\Illuminate\\Support\\Collection&lt;string&gt;$country-&gt;toArray()Compile the Country enum to array\[ 'value' =&gt; 'US', 'label' =&gt; 'United States', 'code' =&gt; 'united\_states', 'regions' =&gt; \[ \[...\], ... \[...\] \] \]array$country-&gt;toJson($options = 0)Compile the Country enum to a JSON stringjson\_encode($country-&gt;toArray(), $options = 0)string$country-&gt;svgFlag()Get the *path* to the SVG Flag/var/www/.../country-enums/flags/svg/us.svgstring|null$country-&gt;svgFlagContent()Get the *content* of the SVG Flag (XML)[svg](https://raw.githubusercontent.com/bradietilley/country-enums/master/flags/svg/au.svg)string|null$country-&gt;pngFlag(int $size = 100)Get the *path* to the PNG Flag for the given size (100, 250, 1000)/var/www/.../country-enums/flags/png250px/us.pngstring|null$country-&gt;pngFlagContent(int $size = 100)Get the *content* of the PNG Flag for the given size (100, 250, 1000)[png](https://raw.githubusercontent.com/bradietilley/country-enums/master/flags/png100px/au.png)string|nullCountry::random()Get a random countryCountry::NZCountryCountry::getValues()Get a list of available "values"\[ "AF", ..., "ZW" \]array&lt;string&gt;Country::collectValues()Get a list of available "values" as a collection (requires Laravel if used)Collection::make(\[ "AF", ..., "ZW" \])\\Illuminate\\Support\\Collection&lt;string&gt;Country::getOptions()Get a list of available options (key val pairs)\[ "AF" =&gt; "Afghanistan", ..., "ZW" =&gt; "Zimbabwe" \]array&lt;string,string&gt;Country::collectOptions()Get a list of available options (key val pairs) as a collection (requires Laravel if used)Collection::make(\[ "AF" =&gt; "Afghanistan", ..., "ZW" =&gt; "Zimbabwe" \])\\Illuminate\\Support\\Collection&lt;string,string&gt;Country::cases()Core-PHP function to get all enum cases\[ Country::AF, ..., Country::ZW \]array&lt;Country&gt;Country::collect()Get all Country enums in a Collection class (requires Laravel if used)Collection::make(\[ Country::AF, ..., Country::ZW \])\\Illuminate\\Support\\Collection&lt;Country&gt;Country::from(string $value (e.g. 'NZ'))Core-PHP function to convert value to enumCountry::NZ (or throws exception if invalid)CountryCountry::tryFrom(string $value (e.g. 'NZ'))Core-PHP function to convert value to enum (/null)Country::NZ (or null if invalid)Country|nullCountry::parse(stringCountrynull $country (e.g. 'AU'))Parse the given string, Country or null value to Country enumCountry::tryParse(stringCountrynull $country (e.g. 'AU'))Parse the given string, Country or null value to Country enumCountry::inRule()Get an 'In' ruleset that asserts the validated value is a backing value of Country, e.g. 'AU' (requires Laravel if used)new In(\[ 'AF', ..., 'ZW' \])\\Illuminate\\Validation\\Rules\\InCountry::enumRule()Get an 'Enum' ruleset that asserts the validated value is a backing value of Country, e.g. 'AU' (requires Laravel if used)new Enum(Country::class)\\Illuminate\\Validation\\Rules\\EnumWhen a country is compiled to array, all of its regions are casted to array as well and are made available in the 'regions' array.

#### Region Enum

[](#region-enum)

All regions are defined by their country's two-letter code followed by an underscore then a variable length code for the region itself.

```
$region = Region::US_CA;
$region = Country::from('US_CA');
```

Method/AccessorDescriptionExampleType$region-&gt;valueGet the code"US\_CA"string$region-&gt;label()Get the human-readable label"California"string$region-&gt;code()Get the English snake\_case code name"california"string$region-&gt;country()Get the country for this regionCountry::USCountry$region-&gt;countryCode()Get the country code for this region"US"string$region-&gt;regionCode()Get the region code for this region"CA"string$region-&gt;toArray()Compile the Region enum to array\[ 'value' =&gt; 'US\_CA', 'label' =&gt; 'California', 'code' =&gt; 'california', 'country' =&gt; 'US', 'region' =&gt; 'CA' \]array$region-&gt;toJson($options = 0)Compile the Region enum to a JSON stringjson\_encode($region-&gt;toArray(), $options = 0)stringRegion::for(Country|string $country)Get a list of regions (in the given $country if provided)\[ Region::US\_AL, ..., Region::US\_WY \]array&lt;Region&gt;Region::collectFor(Country|string $country)Get a list of regions (in the given $country if provided) as a collection (requires Laravel if used)Collection::make(Region::for($country))\\Illuminate\\Support\\Collection&lt;Region&gt;Region::random(Country|string|null $country)Get a random region (in the given $country if provided)Region::US\_TXRegionRegion::getValues(Country|string|null $country)Get a list of available "values"\[ "AL", ..., "WY" \]array&lt;string&gt;Region::collectValues(Country|string|null $country)Get a list of available "values" as a collection (requires Laravel if used)Collection::make(\[ "AL", ..., "WY" \])\\Illuminate\\Support\\Collection&lt;string&gt;Region::getOptions(Country|string|null $country)Get a list of available options (key val pairs)\[ "AL" =&gt; "Alabama", ..., "WY" =&gt; "Wyoming" \]array&lt;string,string&gt;Region::collectOptions(Country|string|null $country)Get a list of available options (key val pairs) as a collection (requires Laravel if used)Collection::make(\[ "AL" =&gt; "Alabama", ..., "WY" =&gt; "Wyoming" \])\\Illuminate\\Support\\Collection&lt;string,string&gt;Region::cases()Core-PHP function to get all enum cases\[ Region::AF\_BDS, ..., Region::ZW\_MI \]array&lt;Region&gt;Region::collect()Get all Region enums in a Collection class (requires Laravel if used)Collection::make(\[ Region::AF\_BDS, ..., Region::ZW\_MI \])\\Illuminate\\Support\\Collection&lt;Region&gt;Region::from(string $val (e.g. 'NZ\_AUK'))Core-PHP function to convert value to enumRegion::NZ\_AUK (or throws exception if invalid)RegionRegion::tryFrom(string $val (e.g. 'NZ\_AUK'))Core-PHP function to convert value to enum (/null)Region::NZ\_AUK (or null if invalid)Region|nullRegion::parse(string|Region|null $region (e.g. 'AU\_NSW'))Parse the given string, Region or null value to Region enumRegion::AU\_NSW (or throws exception if invalid)RegionRegion::tryParse(string|Region|null $region (e.g. 'AU\_NSW'))Parse the given string, Region or null value to Region enumRegion::AU\_NSW (or null if invalid)Region|nullRegion::inRule(string|Country|null $country = null)Get an 'In' ruleset that asserts the validated value is a backing value of Region, e.g. 'AU\_NSW', optionally providing a country to limit the scope of valid regions (requires Laravel if used)new In(\[ 'AF\_BDS', ..., 'ZW\_MI' \])\\Illuminate\\Validation\\Rules\\InRegion::enumRule()Get an 'Enum' ruleset that asserts the validated value is a backing value of Region, e.g. 'AU\_NSW' (requires Laravel if used)new Enum(Region::class)\\Illuminate\\Validation\\Rules\\EnumWhen a region is compiled to array, its region and country codes are made available as separate key-value pairs alongside the globally-unique "value" (see above).

Author(s)
---------

[](#authors)

PHP Enum classes generated by Bradie Tilley.

Specials Thanks
---------------

[](#specials-thanks)

Thanks to all the people who have contributed to the following open source repositories

- Country/Region Data:
- SVG/PNG Flags:

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~36 days

Recently: every ~63 days

Total

8

Last Release

1098d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f93c7d77d882d4106664cca209c6dccc0e531f307f56dc2247937358d193ef0b?d=identicon)[bradietilley](/maintainers/bradietilley)

---

Top Contributors

[![bradietilley](https://avatars.githubusercontent.com/u/44430471?v=4)](https://github.com/bradietilley "bradietilley (35 commits)")

###  Code Quality

TestsPest

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/bradietilley-country-enums/health.svg)

```
[![Health](https://phpackages.com/badges/bradietilley-country-enums/health.svg)](https://phpackages.com/packages/bradietilley-country-enums)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
