/
CountryList.php
82 lines (69 loc) · 2.7 KB
/
CountryList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Concrete\Core\Localization\Service;
use Events;
use Localization;
class CountryList
{
protected $countries = [];
public function __construct()
{
$this->loadCountries();
}
protected function loadCountries()
{
$countries = \Punic\Territory::getCountries();
unset(
// Fake countries
$countries['IM'], // Isle of Man (it's a British Crown Dependency)
$countries['JE'] // Jersey (it's a British Crown Dependency)
);
$event = new \Symfony\Component\EventDispatcher\GenericEvent();
$event->setArgument('countries', $countries);
$event = Events::dispatch('on_get_countries_list', $event);
$countries = $event->getArgument('countries');
$this->countries[Localization::activeLocale()] = $countries;
}
/** Returns an array of countries with their short name as the key and their full name as the value
* @return array Keys are the country codes, values are the county names
*/
public function getCountries()
{
if (!array_key_exists(Localization::activeLocale(), $this->countries)) {
$this->loadCountries();
}
return $this->countries[Localization::activeLocale()];
}
/**
* Gets a country name given its code.
*
* @param string $code The country code
*
* @return string|null returns NULL if $code is not valid
*/
public function getCountryName($code)
{
$countries = $this->getCountries(true);
return isset($countries[$code]) ? $countries[$code] : null;
}
/**
* Return a list of territory codes where a specific language is spoken, sorted by the total number of people speaking that language.
*
* @param string $languageCode The language code (eg. 'en')
* @param string $languageStatuses The allowed statuses of the languages, whose codes are 'o' (official), 'r' (official regional), 'f' (de facto official), 'm' (official minority), 'u' (unofficial or unknown)
*
* @return array Returns a list of country codes
*/
public function getCountriesForLanguage($languageCode, $languageStatuses = 'orfm')
{
$territories = [];
foreach (\Punic\Territory::getTerritoriesForLanguage($languageCode) as $territory) {
$territoryLanguages = \Punic\Territory::getLanguages($territory, $languageStatuses, true);
if (in_array($languageCode, $territoryLanguages)) {
$territories[] = $territory;
}
}
$validCountryCodes = array_keys($this->getCountries());
$result = array_intersect($territories, $validCountryCodes);
return array_values($result);
}
}