Repository: Monarobase/country-list
Branch: master
Commit: a432583fe65b
Files: 10
Total size: 13.4 KB
Directory structure:
gitextract_u7jzeizu/
├── .gitignore
├── .travis.yml
├── README.md
├── composer.json
├── phpunit.xml
├── src/
│ ├── CountryList.php
│ ├── CountryListFacade.php
│ ├── CountryListServiceProvider.php
│ └── CountryNotFoundException.php
└── tests/
└── CountryListTest.php
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
/vendor
composer.phar
composer.lock
.DS_Store
.idea
================================================
FILE: .travis.yml
================================================
language: php
php:
- 7.0
- 7.1
- 7.2
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
script: phpunit
================================================
FILE: README.md
================================================
# Country List
Country List is a package for Laravel which lists all countries with names and ISO 3166-1 codes in all languages and data formats.
## Installation
Run `composer require monarobase/country-list`.
Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
If you don't use auto-discovery, add the ServiceProvider to the `providers` array in `config/app.php`
```php
Monarobase\CountryList\CountryListServiceProvider::class,
```
If needed, add the following alias as well.
```php
'Countries' => Monarobase\CountryList\CountryListFacade::class,
```
## Usage
- Locale (en, en_US, fr, fr_CA...)
- Format (csv, flags.html, html, json, mysql.sql, php, postgresql.sql, sqlite.sql, sqlserver.sql, txt, xml, yaml)
Get all countries
```php
Route::get('/', function()
{
return Countries::getList('en', 'json');
});
```
Get one country
```php
Route::get('/', function()
{
return Countries::getOne('RU', 'en');
});
```
================================================
FILE: composer.json
================================================
{
"name": "monarobase/country-list",
"description": "List of all countries with names and ISO 3166-1 codes in all languages and data formats for Laravel",
"keywords": [
"laravel",
"countries"
],
"license": "MIT",
"authors": [
{
"name": "Jonathan Thuau",
"email": "jonathan@monarobase.net",
"homepage": "https://monarobase.net",
"role": "Developer"
}
],
"require": {
"php": ">=5.6.0",
"illuminate/support": "^5.8|^6|^7|^8|^9|^10|^11|^12|^13.0",
"umpirsky/country-list": "2.0.*"
},
"require-dev": {
"phpunit/phpunit": "^8.3"
},
"autoload": {
"psr-4": {
"Monarobase\\CountryList\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Monarobase\\CountryList\\Tests\\": "tests/"
}
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"Monarobase\\CountryList\\CountryListServiceProvider"
],
"aliases": {
"Countries": "Monarobase\\CountryList\\CountryListFacade"
}
}
}
}
================================================
FILE: phpunit.xml
================================================
./tests/
================================================
FILE: src/CountryList.php
================================================
*
* (c) 2013-2015 Monarobase
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*
* @category Monarobase
* @package CountryList
* @copyright (c) 2013-2015 Monarobase
* @link https://monarobase.net
*/
/**
* CountryList
*
* @author Monarobase
*/
class CountryList
{
/**
* Path to the directory containing countries data.
* @var string
*/
protected $dataDir;
/**
* Cached data.
* @var array
*/
protected $dataCache = [];
/**
* Constructor.
*
* @param string|null $dataDir Path to the directory containing countries data
*/
public function __construct(?string $dataDir = null)
{
if (!isset($dataDir)) {
$dataDir = base_path('vendor/umpirsky/country-list/data');
}
if (!is_dir($dataDir)) {
throw new \RuntimeException(sprintf('Unable to locate the country data directory at "%s"', $dataDir));
}
$this->dataDir = realpath($dataDir);
}
/**
* Get the country data directory.
*
* @return string
*/
public function getDataDir(): string
{
return $this->dataDir;
}
/**
* Returns one country.
*
* @param string $countryCode The country
* @param string $locale The locale (default: en)
* @return string
* @throws CountryNotFoundException If the country code doesn't match any country.
*/
public function getOne(string $countryCode, string $locale = 'en'): string
{
$countryCode = mb_strtoupper($countryCode);
$locales = $this->loadData($locale, 'php');
if (!$this->has($countryCode, $locale)) {
throw new CountryNotFoundException($countryCode);
}
return $locales[mb_strtoupper($countryCode)];
}
/**
* Returns a list of countries.
*
* @param string $locale The locale (default: en)
* @param string $format The format (default: php)
* @return mixed An array (list) with country or raw data
*/
public function getList(string $locale = 'en', string $format = 'php')
{
return $this->loadData($locale, $format);
}
/**
* @param string $locale The locale
* @param array $data An array (list) with country data
* @return CountryList The instance of CountryList to enable fluent interface
*/
public function setList(string $locale, array $data): CountryList
{
$this->dataCache[$locale] = $data;
return $this;
}
/**
* A lazy-loader that loads data from a PHP file if it is not stored in memory yet.
*
* @param string $locale The locale
* @param string $format The format (default: php)
* @return mixed An array (list) with country or raw data
*/
protected function loadData(string $locale, string $format)
{
$locale = str_replace('-', '_', $locale);
if (!isset($this->dataCache[$locale][$format])) {
// Customization - "source" does not matter anymore because umpirsky refactored his library.
$file = sprintf('%s/%s/country.%s', $this->dataDir, $locale, $format);
if (!is_file($file)) {
throw new \RuntimeException(sprintf('Unable to load the country data file "%s"', $file));
}
$this->dataCache[$locale][$format] = ($format === 'php') ? require $file : file_get_contents($file);
}
return $this->sortData($locale, $this->dataCache[$locale][$format]);
}
/**
* Sorts the data array for a given locale, using the locale translations.
* It is UTF-8 aware if the Collator class is available (requires the intl
* extension).
*
* @param string $locale The locale whose collation rules should be used.
* @param mixed $data Array of strings or raw data.
* @return mixed If $data is an array, it will be sorted, otherwise raw data
*/
protected function sortData(string $locale, $data)
{
if (is_array($data)) {
if (class_exists('Collator')) {
$collator = new \Collator($locale);
$collator->asort($data);
} else {
asort($data);
}
}
return $data;
}
/**
* Indicates whether or not a given $countryCode matches a country.
*
* @param string $countryCode A 2-letter country code
* @param string $locale The locale (default: en)
* @return bool true if a match was found, false otherwise
*/
public function has(string $countryCode, string $locale = 'en'): bool
{
$locales = $this->loadData($locale, 'php');
return isset($locales[mb_strtoupper($countryCode)]);
}
}
================================================
FILE: src/CountryListFacade.php
================================================
* @link http://monarobase.net
*/
use Illuminate\Support\Facades\Facade;
/**
* CountryListFacade
*
* @author Monarobase
*
* @method static string getDataDir()
* @method static string getOne(string $countryCode, string $locale = 'en')
* @method static array getList(string $locale = 'en', string $format = 'php')
* @method static CountryList setList(string $locale, array $data)
* @method static bool has(string $countryCode, string $locale = 'en')
*/
class CountryListFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return CountryList::class;
}
}
================================================
FILE: src/CountryListServiceProvider.php
================================================
* @link http://monarobase.net
*/
use Illuminate\Support\ServiceProvider;
/**
* CountryListServiceProvider
*
* @author Monarobase
*/
class CountryListServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('countrylist', function ($app) {
return new CountryList(base_path('vendor/umpirsky/country-list/data'));
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['countrylist'];
}
}
================================================
FILE: src/CountryNotFoundException.php
================================================
* @copyright 2015 La Haute Société - http://www.lahautesociete.com/
*/
class CountryNotFoundException extends \Exception
{
/**
* Constructor.
*
* @param string $countryCode A 2-letter country code
*/
public function __construct($countryCode)
{
parent::__construct('Country "' . $countryCode . '" not found.');
}
}
================================================
FILE: tests/CountryListTest.php
================================================
countryList = new CountryList('vendor/umpirsky/country-list/data');
}
protected function tearDown(): void
{
unset($this->countryList);
$this->countryList = null;
}
/**
* @test
*/
public function getDataDirTest(): void
{
$this->assertEquals(realpath('vendor/umpirsky/country-list/data'), $this->countryList->getDataDir());
}
/**
* @test
* @throws \Monarobase\CountryList\CountryNotFoundException
*/
public function getOneTest(): void
{
$this->countryList->setList('xx', [
'php' => [
'C' => 'Country C',
'B' => 'Country B',
'A' => 'Country A',
]
]);
$this->assertEquals('Country B', $this->countryList->getOne('B', 'xx'));
$this->expectException(CountryNotFoundException::class);
$this->countryList->getOne('D', 'xx');
}
/**
* @test
*/
public function getListPHPTest(): void
{
$this->countryList->setList('xx', [
'php' => [
'C' => 'Country C',
'B' => 'Country B',
'A' => 'Country A',
]
]);
$this->assertEquals(array_keys([
'A' => 'Country A',
'B' => 'Country B',
'C' => 'Country C',
]), array_keys($this->countryList->getList('xx')));
$this->assertNotEquals(array_keys([
'C' => 'Country C',
'A' => 'Country A',
'B' => 'Country B',
]), array_keys($this->countryList->getList('xx')));
}
/**
* @test
*/
public function getListJSONTest(): void
{
$this->countryList->setList('xx', [
'json' => '{"A":"Country A","B":"Country B","C":"Country C"}'
]);
$this->assertEquals(
'{"A":"Country A","B":"Country B","C":"Country C"}',
$this->countryList->getList('xx', 'json')
);
}
/**
* @test
*/
public function hasTest(): void
{
$this->countryList->setList('xx', [
'php' => [
'A' => 'Country A',
'B' => 'Country B',
'C' => 'Country C',
]
]);
$this->assertTrue($this->countryList->has('A', 'xx'));
$this->assertFalse($this->countryList->has('D', 'xx'));
}
}