1
+ <?php
2
+ namespace App \View \Helper ;
3
+
4
+ use Cake \Utility \Inflector ;
5
+ use Cake \View \Helper ;
6
+
7
+ // This helper helps determining the brightness of a colour (initially only used for the tagging) in order to decide
8
+ // what text colour to use against the background (black or white)
9
+ class FlagHelper extends Helper {
10
+
11
+ public $ helpers = [
12
+ 'Bootstrap ' ,
13
+ ];
14
+
15
+ /**
16
+ * @param string $countryCode ISO 3166-1 alpha-2 two-letter country code
17
+ * @param string $countryName Full country name for title
18
+ * @return string
19
+ */
20
+ public function countryFlag ($ countryCode , $ countryName = null , $ small = false )
21
+ {
22
+ if (strlen ($ countryCode ) !== 2 ) {
23
+ return '' ;
24
+ }
25
+
26
+ $ output = [];
27
+ foreach (str_split (strtolower ($ countryCode )) as $ letter ) {
28
+ $ letterCode = ord ($ letter );
29
+ if ($ letterCode < 97 || $ letterCode > 122 ) {
30
+ return '' ; // invalid letter
31
+ }
32
+ $ output [] = "1f1 " . dechex (0xe6 + ($ letterCode - 97 ));
33
+ }
34
+
35
+ $ countryNamePretty = Inflector::humanize ($ countryName ? h ($ countryName ) : $ countryCode );
36
+ $ baseurl = $ this ->getView ()->get ('baseurl ' );
37
+ $ title = __ ('Flag of %s ' , $ countryNamePretty );
38
+ $ html = '<img src=" ' . $ baseurl . '/img/flags/ ' . implode ('- ' , $ output ) . '.svg" title=" ' . $ title .'" alt=" ' . $ title . '" aria-label=" ' . $ title . '" style="height: 18px" /> ' ;
39
+ if (!$ small ) {
40
+ $ html = $ this ->Bootstrap ->node ('span ' , [
41
+ 'class ' => 'd-flex align-items-center '
42
+ ], $ html . ' ' . $ countryNamePretty );
43
+ }
44
+ return $ html ;
45
+ }
46
+
47
+ public function flag ($ countryName , $ small = false ) {
48
+ $ countryNameLow = strtolower ($ countryName );
49
+ if (!empty (self ::countries[$ countryNameLow ])) {
50
+ $ countryCode = self ::countries[$ countryNameLow ];
51
+ return $ this ->countryFlag ($ countryCode , $ countryName , $ small );
52
+ }
53
+ return '' ;
54
+ }
55
+
56
+ private const countries = [
57
+ "afghanistan " => "AF " ,
58
+ "albania " => "AL " ,
59
+ "algeria " => "DZ " ,
60
+ "andorra " => "AD " ,
61
+ "angola " => "AO " ,
62
+ "antigua and barbuda " => "AG " ,
63
+ "argentina " => "AR " ,
64
+ "armenia " => "AM " ,
65
+ "australia " => "AU " ,
66
+ "austria " => "AT " ,
67
+ "azerbaijan " => "AZ " ,
68
+ "bahamas " => "BS " ,
69
+ "bahrain " => "BH " ,
70
+ "bangladesh " => "BD " ,
71
+ "barbados " => "BB " ,
72
+ "belarus " => "BY " ,
73
+ "belgium " => "BE " ,
74
+ "belize " => "BZ " ,
75
+ "benin " => "BJ " ,
76
+ "bhutan " => "BT " ,
77
+ "bolivia " => "BO " ,
78
+ "bosnia and herzegovina " => "BA " ,
79
+ "botswana " => "BW " ,
80
+ "brazil " => "BR " ,
81
+ "brunei " => "BN " ,
82
+ "bulgaria " => "BG " ,
83
+ "burkina faso " => "BF " ,
84
+ "burundi " => "BI " ,
85
+ "cabo verde " => "CV " ,
86
+ "cambodia " => "KH " ,
87
+ "cameroon " => "CM " ,
88
+ "canada " => "CA " ,
89
+ "central african republic " => "CF " ,
90
+ "chad " => "TD " ,
91
+ "chile " => "CL " ,
92
+ "china " => "CN " ,
93
+ "colombia " => "CO " ,
94
+ "comoros " => "KM " ,
95
+ "congo (brazzaville) " => "CG " ,
96
+ "congo (kinshasa) " => "CD " ,
97
+ "costa rica " => "CR " ,
98
+ "croatia " => "HR " ,
99
+ "cuba " => "CU " ,
100
+ "cyprus " => "CY " ,
101
+ "czechia " => "CZ " ,
102
+ "denmark " => "DK " ,
103
+ "djibouti " => "DJ " ,
104
+ "dominica " => "DM " ,
105
+ "dominican republic " => "DO " ,
106
+ "ecuador " => "EC " ,
107
+ "egypt " => "EG " ,
108
+ "el salvador " => "SV " ,
109
+ "equatorial guinea " => "GQ " ,
110
+ "eritrea " => "ER " ,
111
+ "estonia " => "EE " ,
112
+ "eswatini " => "SZ " ,
113
+ "ethiopia " => "ET " ,
114
+ "fiji " => "FJ " ,
115
+ "finland " => "FI " ,
116
+ "france " => "FR " ,
117
+ "gabon " => "GA " ,
118
+ "gambia " => "GM " ,
119
+ "georgia " => "GE " ,
120
+ "germany " => "DE " ,
121
+ "ghana " => "GH " ,
122
+ "greece " => "GR " ,
123
+ "grenada " => "GD " ,
124
+ "guatemala " => "GT " ,
125
+ "guinea " => "GN " ,
126
+ "guinea-bissau " => "GW " ,
127
+ "guyana " => "GY " ,
128
+ "haiti " => "HT " ,
129
+ "honduras " => "HN " ,
130
+ "hungary " => "HU " ,
131
+ "iceland " => "IS " ,
132
+ "india " => "IN " ,
133
+ "indonesia " => "ID " ,
134
+ "iran " => "IR " ,
135
+ "iraq " => "IQ " ,
136
+ "ireland " => "IE " ,
137
+ "israel " => "IL " ,
138
+ "italy " => "IT " ,
139
+ "jamaica " => "JM " ,
140
+ "japan " => "JP " ,
141
+ "jordan " => "JO " ,
142
+ "kazakhstan " => "KZ " ,
143
+ "kenya " => "KE " ,
144
+ "kiribati " => "KI " ,
145
+ "korea (north) " => "KP " ,
146
+ "korea (south) " => "KR " ,
147
+ "kuwait " => "KW " ,
148
+ "kyrgyzstan " => "KG " ,
149
+ "laos " => "LA " ,
150
+ "latvia " => "LV " ,
151
+ "lebanon " => "LB " ,
152
+ "lesotho " => "LS " ,
153
+ "liberia " => "LR " ,
154
+ "libya " => "LY " ,
155
+ "liechtenstein " => "LI " ,
156
+ "lithuania " => "LT " ,
157
+ "luxembourg " => "LU " ,
158
+ "madagascar " => "MG " ,
159
+ "malawi " => "MW " ,
160
+ "malaysia " => "MY " ,
161
+ "maldives " => "MV " ,
162
+ "mali " => "ML " ,
163
+ "malta " => "MT " ,
164
+ "marshall islands " => "MH " ,
165
+ "mauritania " => "MR " ,
166
+ "mauritius " => "MU " ,
167
+ "mexico " => "MX " ,
168
+ "micronesia " => "FM " ,
169
+ "moldova " => "MD " ,
170
+ "monaco " => "MC " ,
171
+ "mongolia " => "MN " ,
172
+ "montenegro " => "ME " ,
173
+ "morocco " => "MA " ,
174
+ "mozambique " => "MZ " ,
175
+ "myanmar " => "MM " ,
176
+ "namibia " => "NA " ,
177
+ "nauru " => "NR " ,
178
+ "nepal " => "NP " ,
179
+ "netherlands " => "NL " ,
180
+ "new zealand " => "NZ " ,
181
+ "nicaragua " => "NI " ,
182
+ "niger " => "NE " ,
183
+ "nigeria " => "NG " ,
184
+ "north macedonia " => "MK " ,
185
+ "norway " => "NO " ,
186
+ "oman " => "OM " ,
187
+ "pakistan " => "PK " ,
188
+ "palau " => "PW " ,
189
+ "panama " => "PA " ,
190
+ "papua new guinea " => "PG " ,
191
+ "paraguay " => "PY " ,
192
+ "peru " => "PE " ,
193
+ "philippines " => "PH " ,
194
+ "poland " => "PL " ,
195
+ "portugal " => "PT " ,
196
+ "qatar " => "QA " ,
197
+ "romania " => "RO " ,
198
+ "russia " => "RU " ,
199
+ "rwanda " => "RW " ,
200
+ "saint kitts and nevis " => "KN " ,
201
+ "saint lucia " => "LC " ,
202
+ "saint vincent and the grenadines " => "VC " ,
203
+ "samoa " => "WS " ,
204
+ "san marino " => "SM " ,
205
+ "sao tome and principe " => "ST " ,
206
+ "saudi arabia " => "SA " ,
207
+ "senegal " => "SN " ,
208
+ "serbia " => "RS " ,
209
+ "seychelles " => "SC " ,
210
+ "sierra leone " => "SL " ,
211
+ "singapore " => "SG " ,
212
+ "slovakia " => "SK " ,
213
+ "slovenia " => "SI " ,
214
+ "solomon islands " => "SB " ,
215
+ "somalia " => "SO " ,
216
+ "south africa " => "ZA " ,
217
+ "south sudan " => "SS " ,
218
+ "spain " => "ES " ,
219
+ "sri lanka " => "LK " ,
220
+ "sudan " => "SD " ,
221
+ "suriname " => "SR " ,
222
+ "sweden " => "SE " ,
223
+ "switzerland " => "CH " ,
224
+ "syria " => "SY " ,
225
+ "taiwan " => "TW " ,
226
+ "tajikistan " => "TJ " ,
227
+ "tanzania " => "TZ " ,
228
+ "thailand " => "TH " ,
229
+ "timor-leste " => "TL " ,
230
+ "togo " => "TG " ,
231
+ "tonga " => "TO " ,
232
+ "trinidad and tobago " => "TT " ,
233
+ "tunisia " => "TN " ,
234
+ "turkey " => "TR " ,
235
+ "turkmenistan " => "TM " ,
236
+ "tuvalu " => "TV " ,
237
+ "uganda " => "UG " ,
238
+ "ukraine " => "UA " ,
239
+ "united arab emirates " => "AE " ,
240
+ "united kingdom " => "GB " ,
241
+ "united states " => "US " ,
242
+ "uruguay " => "UY " ,
243
+ "uzbekistan " => "UZ " ,
244
+ "vanuatu " => "VU " ,
245
+ "vatican city " => "VA " ,
246
+ "venezuela " => "VE " ,
247
+ "vietnam " => "VN " ,
248
+ "yemen " => "YE " ,
249
+ "zambia " => "ZM " ,
250
+ "zimbabwe " => "ZW "
251
+ ];
252
+
253
+ }
0 commit comments