1
<?php
2
declare(strict_types=1);
3

4
namespace Tariq86\CountryList;
5

6
/**
7
 * This file is part of Tariq86-CountryList
8
 * Reference : NoiseLabs-CountryBundle by Vítor Brandão <vitor@noiselabs.org>
9
 *
10
 * (c) 2020 Tariq86
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 *
16
 * @category    Tariq86
17
 * @package     CountryList
18
 * @copyright   (c) 2016-2020 Tariq86
19
 */
20

21
use Collator;
22
use RuntimeException;
23

24

25
/**
26
 * CountryList
27
 *
28
 * @author Tariq86
29
 */
30
class CountryList
31
{
32

33
    /**
34
     * Path to the directory containing countries data.
35
     * @var string
36
     */
37
    protected $dataDir;
38

39
    /**
40
     * Cached data.
41
     * @var array
42
     */
43
    protected $dataCache = [];
44

45
    /**
46
     * Constructor.
47
     *
48
     * @param string|null $dataDir Path to the directory containing countries data
49
     */
50 4
    public function __construct(?string $dataDir = null)
51
    {
52 4
        if (!$dataDir) {
53 4
            $dataDir = base_path('vendor/umpirsky/country-list/data');
54
        }
55

56 4
        if (!is_dir($dataDir)) {
57 4
            throw new RuntimeException(sprintf('Unable to locate the country data directory at "%s"', $dataDir));
58
        }
59

60 4
        $this->dataDir = realpath($dataDir);
61
    }
62

63
    /**
64
     * Get the country data directory.
65
     *
66
     * @return string
67
     */
68 4
    public function getDataDir(): string
69
    {
70 4
        return $this->dataDir;
71
    }
72

73
    /**
74
     * Returns one country.
75
     *
76
     * @param string $countryCode The 2-character ISO code for the country
77
     * @param string $locale The locale (default: en)
78
     * @return string
79
     * @throws CountryNotFoundException  If the country code doesn't match any country.
80
     */
81 4
    public function getOne(string $countryCode, string $locale = 'en'): string
82
    {
83 4
        $countryCode = mb_strtoupper($countryCode);
84 4
        $locales = $this->loadData($locale, 'php', false);
85

86 4
        if (!$this->has($countryCode, $locale)) {
87 4
            throw new CountryNotFoundException($countryCode);
88
        }
89

90 4
        return $locales[mb_strtoupper($countryCode)];
91
    }
92

93
    /**
94
     * Returns a list of countries.
95
     *
96
     * @param string $locale The locale (default: en)
97
     * @param string $format The format (default: php)
98
     * @param bool $sorted Sort the list? (default: true)
99
     * @return mixed         An array (list) with country or raw data
100
     */
101 4
    public function getList(string $locale = 'en', string $format = 'php', bool $sorted = true)
102
    {
103 4
        return $this->loadData($locale, $format, $sorted);
104
    }
105

106
    /**
107
     * @param string $locale The locale
108
     * @param array $data An array (list) with country data
109
     * @return CountryList   The instance of CountryList to enable fluent interface
110
     */
111 4
    public function setList(string $locale, array $data): CountryList
112
    {
113 4
        $this->dataCache[$locale] = $data;
114

115 4
        return $this;
116
    }
117

118
    /**
119
     * A lazy-loader that loads data from a PHP file if it is not stored in memory yet.
120
     *
121
     * @param string $locale The locale
122
     * @param string $format The format (default: php)
123
     * @param bool $sorted Should we sort the country list? (default: true)
124
     * @return mixed         An array (list) with country or raw data
125
     */
126 4
    protected function loadData(string $locale, string $format, bool $sorted = true)
127
    {
128 4
        $locale = str_replace('-', '_', $locale);
129

130 4
        if (!isset($this->dataCache[$locale][$format])) {
131
            // Customization - "source" does not matter anymore because umpirsky refactored his library.
132 4
            $file = sprintf('%s/%s/country.%s', $this->dataDir, $locale, $format);
133

134 4
            if (!is_file($file)) {
135 4
                throw new RuntimeException(sprintf('Unable to load the country data file "%s"', $file));
136
            }
137

138 4
            $this->dataCache[$locale][$format] = ($format === 'php') ? require $file : file_get_contents($file);
139
        }
140 4
        if ($sorted) {
141 4
            return $this->sortData($locale, $this->dataCache[$locale][$format]);
142
        }
143 4
        return $this->dataCache[$locale][$format];
144
    }
145

146
    /**
147
     * Sorts the data array for a given locale, using the locale translations.
148
     * It is UTF-8 aware if the Collator class is available (requires the intl
149
     * extension).
150
     *
151
     * @param string $locale The locale whose collation rules should be used.
152
     * @param mixed $data Array of strings or raw data.
153
     * @return mixed         If $data is an array, it will be sorted, otherwise raw data
154
     */
155 4
    protected function sortData(string $locale, $data)
156
    {
157 4
        if (is_array($data)) {
158 4
            $collator = new Collator($locale);
159 4
            $collator->asort($data);
160
        }
161 4
        return $data;
162
    }
163

164
    /**
165
     * Indicates whether or not a given $countryCode matches a country.
166
     *
167
     * @param string $countryCode A 2-letter country code
168
     * @param string $locale The locale (default: en)
169
     * @return bool                <code>true</code> if a match was found, <code>false</code> otherwise
170
     */
171 4
    public function has(string $countryCode, string $locale = 'en'): bool
172
    {
173 4
        $locales = $this->loadData($locale, 'php', false);
174

175 4
        return isset($locales[mb_strtoupper($countryCode)]);
176
    }
177
}

Read our documentation on viewing source code .

Loading