countlang/countlang

PHP library for the Unicode CLDR project

v1.0.0 2017-07-13 16:32 UTC

This package is not auto-updated.

Last update: 2024-03-17 00:13:46 UTC


README

Release Build Status Code Coverage License

CountLang is a PHP library for the Unicode CLDR project.

Summary

The library provides data for any country, language, currency, region, sub-region and relationship between them.

Country is the base entity of CountLang. Language, currency, region and sub-region are independent entities, but they are all connected through the country.

Connection between entities is provided by a map of relations that binds the country with other entities.

Data is available for all countries that existed since 1974 (year of first issue of ISO 3166).

Doctrine cache is used to improve data parsing and mapping.

Installing

The easiest way to install this package is with Composer using the following command:

$ composer require countlang/countlang

Examples

Include composer loader and use library interface:

// include composer autoloader if you haven't done it yet
require __DIR__ . '/vendor/autoload.php';

// include library interface
use CountLang\CountLang;

// initialize CountLand interface
$countLang = new CountLang();

Country

// get a collection with all countries 
$allCountries = $countlang->getCountries();
// get a list of all official country names 
$allCountryNames = $allCountries->select('officialName');

// get the country entity of Denmark
$denmark = $countlang->getCountry('Denmark');

// get a collection of countries that share common border with Denmark
$denmarkNeigbors = $denmark->getBordersCollection();

// get a collection with all languages of Denmark
$denmarkLanguages = $denmark->getLanguagesCollection();
// get usage percentage of Danish language in Denmark
$usagePercentageOfDanishLanguageInDenmark = $denmarkLanguages->findEntity('Danish')->getUsagePercentage();

// get first currency entity from collection of Denmark's currencies
$denmarkCurrency = $denmark->getCurrenciesCollection()->getEntity();

// get name of Denmark's sub-region
$denmarkSubRegion = $denmark->getSubRegionEntity()->getName();

// get code of region Denmark is belong to
$denmarkRegion = $denmark->getRegionEntity()->getCode();

Language

// get a collection of all languages 
$allLanguages = $countlang->getLanguages();
// get a list of alpha 3T codes (according to ISO 639) for all languages 
$allLanguagesCodes = $allLanguages->select('alpha3TCode');

// get the language entity of Danish
$danishLanguage = $countlang->getLanguage('Danish');

// get a collection with all Danish-speaking countries entities
$danishLanguageCountries = $danishLanguage->getCountriesCollection();
// get a collection with all Danish-speaking countries 
// that have a population more than half a million people
$mostPopulatedDanishLanguageCountries = $danishLanguageCountries->filter('population', 500000, 'gt');
// get a collection with all Danish-speaking countries 
// that have a population less than 100 thousand people
// and have access to the world's ocean
$filteredDanishLanguageCountries = $danishLanguageCountries->multiFilter([
    ['population', 100000, CountLang\Filter\Filter::OPERATOR_LT],
    ['isLandLocked', false],
]);

Currency

// get a collection of all currencies 
$allCurrencies = $countlang->getCurrencies();
// get an associative array with official names and symbols of all countries
$allCurrenciesNamesAndSymbols = $allCurrencies->select(['officialName', 'symbol']);

// get the currency entity of Danish Krone
$danishKrona = $countlang->getCurrency('Danish Krone');

// get a collection of all countries that use Danish Krone
$danishKronaCountries = $danishKrona->getCountriesCollection();

Region

// get a collection of all regions 
$allRegions = $countlang->getRegions();
// print the collection (in JSON format)
// Note: when used as a string, collection or entity will be converted to JSON
echo $allRegions;

// get the region entity of Europe
$europe = $countlang->getRegion('Europe');

// get a collection of European countries
$europeanCountries = $europe->getCountriesCollection();

// get a collection of Europe sub-regions
$subRegions = $europe->getSubRegionsCollection();

SubRegion

// get a collection of all sub-regions 
$allSubRegions = $countlang->getSubRegions();
// get an associative array with complete data for all sub-regions 
$allSubRegionsData = $allSubRegions->select();

// get the region entity of Northern Europe
$northernEurope = $countlang->getSubRegion('Northern Europe');

// get the region entity of Europe
$europe = $northernEurope->getRegionEntity();

// get the number of countries in the Northern Europe sub-region
$northernEuropeanCountriesAmount = $northernEurope->getCountriesCollection()->count();

Sources

The main source of data is the most recent release of Unicode CLDR. Additionally used ISO 3166, ISO 639, ISO 4217, World Bank Open Data, The World Factbook, Wikipedia, WTNG.

License

This library is available under the MIT license.