1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file contains functionality relating to the countries players are allowed to set in their profiles as their location |
4
|
|
|
* |
5
|
|
|
* @package BZiON\Models |
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A country |
11
|
|
|
* @package BZiON\Models |
12
|
|
|
*/ |
13
|
|
|
class Country extends Model |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name of the country |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The ISO code of the country |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $iso; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The name of the database table used for queries |
29
|
|
|
*/ |
30
|
|
|
const TABLE = "countries"; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
1 |
|
protected function assignResult($country) |
36
|
|
|
{ |
37
|
1 |
|
$this->name = $country['name']; |
38
|
1 |
|
$this->iso = $country['iso']; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the name of the country in the default language |
43
|
|
|
* |
44
|
|
|
* @return string The name of the country |
45
|
|
|
*/ |
46
|
1 |
|
public function getName() |
47
|
|
|
{ |
48
|
1 |
|
return $this->name; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the ISO code of a country |
53
|
|
|
* |
54
|
|
|
* @return string The ISO code of the country |
55
|
|
|
*/ |
56
|
1 |
|
public function getISO() |
57
|
|
|
{ |
58
|
1 |
|
return $this->iso; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the HTML to display a specific flag |
63
|
|
|
* |
64
|
|
|
* @return string HTML to generate a flag |
65
|
|
|
*/ |
66
|
1 |
|
public function getFlagLiteral() |
67
|
|
|
{ |
68
|
1 |
|
return '<div class="c-flag ' . $this->getFlagCssClass() . '" title="' . $this->getName() . '"></div>'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get all the countries in the database |
73
|
|
|
* |
74
|
|
|
* @return Country[] An array of country objects |
75
|
|
|
*/ |
76
|
|
|
public static function getCountries() |
77
|
|
|
{ |
78
|
|
|
return self::arrayIdToModel(self::fetchIds()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get an associative array with country ISO code as keys and country names |
83
|
|
|
* as values |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public static function getCountriesWithISO() |
88
|
|
|
{ |
89
|
|
|
$result = Database::getInstance()->query( |
90
|
|
|
'SELECT iso, name from ' . static::TABLE |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return array_column($result, 'name', 'iso'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the country's flag's CSS class |
98
|
|
|
* |
99
|
|
|
* @return string The URL to the country's flag |
100
|
|
|
*/ |
101
|
1 |
|
private function getFlagCssClass() |
102
|
|
|
{ |
103
|
1 |
|
return "c-flag--" . strtolower($this->getISO()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Given a country's ISO, get its ID |
108
|
|
|
* |
109
|
|
|
* @param string $iso The two-letter ISO code of the country |
110
|
|
|
* @return int The country's database ID |
111
|
|
|
*/ |
112
|
|
|
public static function getIdFromISO($iso) |
113
|
|
|
{ |
114
|
|
|
return self::fetchIdFrom($iso, 'iso'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get a query builder for countries |
119
|
|
|
* |
120
|
|
|
* @return QueryBuilder |
121
|
|
|
*/ |
122
|
|
|
public static function getQueryBuilder() |
123
|
|
|
{ |
124
|
|
|
return new QueryBuilder('Country', array( |
125
|
|
|
'columns' => array( |
126
|
|
|
'name' => 'name', |
127
|
|
|
), |
128
|
|
|
'name' => 'name', |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|