Completed
Pull Request — master (#119)
by Joshua
36:02 queued 19:39
created

Locale::countryCodeToLocale()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 8.8571
cc 5
eloc 12
nc 3
nop 2
crap 30
1
<?php
2
3
namespace libphonenumber\geocoding;
4
5
6
class Locale extends \Locale
7
{
8
    /**
9
     * Returns a locale from a country code that is provided.
10
     * @link http://stackoverflow.com/a/10375234/403165
11
     * @param string $country_code ISO 3166-2-alpha 2 country code
12
     * @param string $language_code ISO 639-1-alpha 2 language code
13
     * @returns string a locale, formatted like en_US, or null if not found
14
     */
15
    public static function countryCodeToLocale($country_code, $language_code = '')
16
    {
17
        $locale = 'en-' . $country_code;
18
        $locale_region = locale_get_region($locale);
19
        $locale_language = locale_get_primary_language($locale);
20
        $locale_array = array(
21
            'language' => $locale_language,
22
            'region' => $locale_region
23
        );
24
25
        if (strtoupper($country_code) == $locale_region && $language_code == '') {
26
            return locale_compose($locale_array);
27
        } elseif (strtoupper($country_code) == $locale_region && strtolower($language_code) == $locale_language) {
28
            return locale_compose($locale_array);
29
        }
30
31
        return null;
32
    }
33
34
}
35