CountriesList   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 1
A getNameFromCode() 0 7 2
A getCodeFromName() 0 7 2
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