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