CountriesList::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace LeKoala\GeoTools;
4
5
use SilverStripe\i18n\Data\Intl\IntlLocales;
6
7
/**
8
 * @author Koala
9
 */
10
class CountriesList
11
{
12
    /**
13
     * Get the country list, using IntlLocales
14
     *
15
     * Keys are set to uppercase to match ISO standards
16
     *
17
     * @return array<string,string>
18
     */
19
    public static function get()
20
    {
21
        $intl = new IntlLocales;
22
        $countries = $intl->getCountries();
23
        $countries = array_change_key_case($countries, CASE_UPPER);
24
        return $countries;
25
    }
26
27
    /**
28
     * @param string $code
29
     * @return string
30
     */
31
    public static function getNameFromCode($code)
32
    {
33
        $list = self::get();
34
        if (isset($list[$code])) {
35
            return $list[$code];
36
        }
37
        return $code;
38
    }
39
40
    /**
41
     * @param string $name
42
     * @return string
43
     */
44
    public static function getCodeFromName($name)
45
    {
46
        $list = array_flip(self::get());
47
        if (isset($list[$name])) {
48
            return $list[$name];
49
        }
50
        return strtoupper(substr($name, 0, 2));
51
    }
52
}
53