Full Code of Monarobase/country-list for AI

master a432583fe65b cached
10 files
13.4 KB
3.8k tokens
24 symbols
1 requests
Download .txt
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
================================================
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
>
    <testsuites>
        <testsuite name="Package Test Suite">
            <directory suffix=".php">./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

================================================
FILE: src/CountryList.php
================================================
<?php
declare(strict_types=1);

namespace Monarobase\CountryList;

/**
 * This file is part of Monarobase-CountryList
 * Reference : NoiseLabs-CountryBundle by Vítor Brandão <vitor@noiselabs.org>
 *
 * (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 <jonathan@monarobase.net>
 * @link        https://monarobase.net
 */

/**
 * CountryList
 *
 * @author Monarobase <jonathan@monarobase.net>
 */
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                <code>true</code> if a match was found, <code>false</code> 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
================================================
<?php
declare(strict_types=1);

namespace Monarobase\CountryList;

/**
 * This file is part of Monarobase-CountryList
 *
 * (c) 2013 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 Monarobase <jonathan@monarobase.net>
 * @link        http://monarobase.net
 */

use Illuminate\Support\Facades\Facade;

/**
 * CountryListFacade
 *
 * @author Monarobase <jonathan@monarobase.net>
 *
 * @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
================================================
<?php
declare(strict_types=1);

namespace Monarobase\CountryList;

/**
 * This file is part of Monarobase-CountryList
 *
 * (c) 2013 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 Monarobase <jonathan@monarobase.net>
 * @link        http://monarobase.net
 */

use Illuminate\Support\ServiceProvider;

/**
 * CountryListServiceProvider
 *
 * @author Monarobase <jonathan@monarobase.net>
 */
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
================================================
<?php
declare(strict_types=1);

namespace Monarobase\CountryList;

/**
 * CountryNotFoundException
 *
 * @author Yohann Bianchi<yohann.b@lahautesociete.com>
 * @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
================================================
<?php
declare(strict_types=1);

namespace Monarobase\CountryList\Tests;

use Monarobase\CountryList\CountryList;
use Monarobase\CountryList\CountryNotFoundException;
use PHPUnit\Framework\TestCase;

class CountryListTest extends TestCase
{
    /**
     * @var CountryList
     */
    private $countryList;

    protected function setUp(): void
    {
        $this->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'));
    }
}
Download .txt
gitextract_u7jzeizu/

├── .gitignore
├── .travis.yml
├── README.md
├── composer.json
├── phpunit.xml
├── src/
│   ├── CountryList.php
│   ├── CountryListFacade.php
│   ├── CountryListServiceProvider.php
│   └── CountryNotFoundException.php
└── tests/
    └── CountryListTest.php
Download .txt
SYMBOL INDEX (24 symbols across 5 files)

FILE: src/CountryList.php
  class CountryList (line 27) | class CountryList
    method __construct (line 46) | public function __construct(?string $dataDir = null)
    method getDataDir (line 64) | public function getDataDir(): string
    method getOne (line 77) | public function getOne(string $countryCode, string $locale = 'en'): st...
    method getList (line 96) | public function getList(string $locale = 'en', string $format = 'php')
    method setList (line 106) | public function setList(string $locale, array $data): CountryList
    method loadData (line 120) | protected function loadData(string $locale, string $format)
    method sortData (line 147) | protected function sortData(string $locale, $data)
    method has (line 168) | public function has(string $countryCode, string $locale = 'en'): bool

FILE: src/CountryListFacade.php
  class CountryListFacade (line 34) | class CountryListFacade extends Facade
    method getFacadeAccessor (line 41) | protected static function getFacadeAccessor()

FILE: src/CountryListServiceProvider.php
  class CountryListServiceProvider (line 28) | class CountryListServiceProvider extends ServiceProvider
    method register (line 42) | public function register()
    method provides (line 54) | public function provides()

FILE: src/CountryNotFoundException.php
  class CountryNotFoundException (line 12) | class CountryNotFoundException extends \Exception
    method __construct (line 19) | public function __construct($countryCode)

FILE: tests/CountryListTest.php
  class CountryListTest (line 10) | class CountryListTest extends TestCase
    method setUp (line 17) | protected function setUp(): void
    method tearDown (line 22) | protected function tearDown(): void
    method getDataDirTest (line 31) | public function getDataDirTest(): void
    method getOneTest (line 40) | public function getOneTest(): void
    method getListPHPTest (line 58) | public function getListPHPTest(): void
    method getListJSONTest (line 84) | public function getListJSONTest(): void
    method hasTest (line 99) | public function hasTest(): void
Condensed preview — 10 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (15K chars).
[
  {
    "path": ".gitignore",
    "chars": 51,
    "preview": "/vendor\ncomposer.phar\ncomposer.lock\n.DS_Store\n.idea"
  },
  {
    "path": ".travis.yml",
    "chars": 163,
    "preview": "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 c"
  },
  {
    "path": "README.md",
    "chars": 976,
    "preview": "# Country List\n\nCountry List is a package for Laravel which lists all countries with names and ISO 3166-1 codes in all l"
  },
  {
    "path": "composer.json",
    "chars": 1213,
    "preview": "{\n    \"name\": \"monarobase/country-list\",\n    \"description\": \"List of all countries with names and ISO 3166-1 codes in al"
  },
  {
    "path": "phpunit.xml",
    "chars": 540,
    "preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<phpunit backupGlobals=\"false\"\n         backupStaticAttributes=\"false\"\n         b"
  },
  {
    "path": "src/CountryList.php",
    "chars": 5179,
    "preview": "<?php\ndeclare(strict_types=1);\n\nnamespace Monarobase\\CountryList;\n\n/**\n * This file is part of Monarobase-CountryList\n *"
  },
  {
    "path": "src/CountryListFacade.php",
    "chars": 1128,
    "preview": "<?php\ndeclare(strict_types=1);\n\nnamespace Monarobase\\CountryList;\n\n/**\n * This file is part of Monarobase-CountryList\n *"
  },
  {
    "path": "src/CountryListServiceProvider.php",
    "chars": 1212,
    "preview": "<?php\ndeclare(strict_types=1);\n\nnamespace Monarobase\\CountryList;\n\n/**\n * This file is part of Monarobase-CountryList\n *"
  },
  {
    "path": "src/CountryNotFoundException.php",
    "chars": 518,
    "preview": "<?php\ndeclare(strict_types=1);\n\nnamespace Monarobase\\CountryList;\n\n/**\n * CountryNotFoundException\n *\n * @author Yohann "
  },
  {
    "path": "tests/CountryListTest.php",
    "chars": 2767,
    "preview": "<?php\ndeclare(strict_types=1);\n\nnamespace Monarobase\\CountryList\\Tests;\n\nuse Monarobase\\CountryList\\CountryList;\nuse Mon"
  }
]

About this extraction

This page contains the full source code of the Monarobase/country-list GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 10 files (13.4 KB), approximately 3.8k tokens, and a symbol index with 24 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!