Download the PHP package jabarihunt/country-codes without Composer
On this page you can find all versions of the php package jabarihunt/country-codes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jabarihunt/country-codes
More information about jabarihunt/country-codes
Files in jabarihunt/country-codes
Package country-codes
Short Description A simple class that returns country codes (or country name).
License MIT
Homepage https://github.com/jabarihunt/country-codes
Informations about the package country-codes
Country Code Class
This is a simple class used to get country codes and/or names. Normally this is something that would be stored and accessed from a database. However, this class will come in handy in situations where you...
- Are running your application someplace without access to a database
- Don't have proper permissions to add tables to a database
- Outside of a countries table, your application doesn't need a database
Requires PHP 7.0+ (due to type definitions)
INSTALLING
Via Composer
Run the following command in the same directory as your composer.json file:
composer require jabarihunt/country-codes
Via Github
-
Clone this repository into a working directory:
git clone [email protected]:jabarihunt/country-codes .
- Include the CountryCodes class in your project...
...or if using an auto-loader...
USAGE
Available Methods
There are two static methods publicly available in the class...
-
get(
int $codeType
,string $code
,bool $useAPI = FALSE
) This function accepts a code type (availabe as class constants) and a corresponding value. The function returns an array containing the country name, 2 letter country code, 3 letter country code, and UN country code. - getAll(
bool $useAPI = FALSE
) Returns a multidemintional array containing the country name, 2 letter country code, 3 letter country code, and UN country code for ALL countries.
NOTE: The countries data is hard coded into the class as an array. However, the optional parameter (
$useAPI
) will spawn an HTTP request to the REST Countries API to retrieve live data. It's rare that countries change, but it does happen!
Additionally, there are five publicly available constants that represent class data types:
Retrieving Data For A Single Country
Suppose we have a country name, Haiti for example, and we want the 3 letter country code. We will use the get()
method by passing it the type of value (country name in this case) along with the value itself.
As you can see, this method returns an array containing the country's official name, native name (in it's native language), 2 letter country code, 3 letter country code, and United Nations numeric code. This will be the case regardless if the source is the hard coded class data or from an HTTP request to the REST Countries API. The following would return the exact same output (note the use of the third optional parameter):
CountryCodes::get($countryCodeType, $countryName, TRUE)
NOTE: Acknowledged country names are exactly as in the REST Countries API. Most are straight forward as with "Haiti" in the example above. However, some aren't so obvious, such as "Venezuela (Bolivarian Republic of)" rather than just "Venezuela"!
The class can be used as a reverse lookup if you happened to have a country code and wish to receive a country name (or other data point from the class). The below example would return the exact same output as the one above:
Retrieving All Countries
There may be instances where you want to retrieve all country codes. The below example will return a multidimensional array of country codes with the same keys as in the example above.
CONTRIBUTING
- Fork Repository
- Create a descriptive branch name
- Make edits to your branch
- Squash (rebase) your commits
- Create a pull request
LICENSE
This project is licensed under the MIT License - see the LICENSE.md file for details.