chibifr/country-converter

A PHP package to convert a country ISO to country name and vice-versa.

1.0.0 2015-11-15 20:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:34 UTC


README

A PHP package to convert a country ISO to country name and vice-versa.

Installation

To install this package, make sure you have composer. Then, require it:

composer require chibifr/country-converter

Usage

You can now use it really easily! After you required the package with composer, start using it:

Without Exception management

<?php
// Require the composer's vendor autoload file
require './vendor/autoload.php';

use ChibiFR\CountryConverter\Converter;

// Create a new Converter object
$converter = new Converter();

// Get a country name from its ISO code
echo $converter->getCountryName('jp'); // will echo "Japan"

// Get a country ISO code from its name
echo $converter->getCountryCode('France'); // will echo "FR"

With Exception management

<?php
// Require the composer's vendor autoload file
require './vendor/autoload.php';

use ChibiFR\CountryConverter\Converter;

// Create a new Converter object
$converter = new Converter();

// Get a country name from its ISO code
try {
    echo $converter->getCountryName('jp'); // will echo "Japan"
} catch (InvalidCountryNameException $e) {
    die($e->getMessage());
}

// Get a country ISO code from its name
try {
    echo $converter->getCountryCode('France'); // will echo "FR"
} catch (InvalidCountryCodeException $e) {
    die($e->getMessage());
}