Return hash with simfiles and packs.
[rock.divinelegy.git] / Domain / Util.php
1 <?php
2
3 namespace Domain;
4
5 use Domain\Entities\StepMania\ISimfile;
6 use Domain\Entities\StepMania\IPack;
7
8 class Util
9 {
10 private static $_countryCodes =
11 array (
12 'AF' => 'Afghanistan',
13 'AX' => 'Åland Islands',
14 'AL' => 'Albania',
15 'DZ' => 'Algeria',
16 'AS' => 'American Samoa',
17 'AD' => 'Andorra',
18 'AO' => 'Angola',
19 'AI' => 'Anguilla',
20 'AQ' => 'Antarctica',
21 'AG' => 'Antigua and Barbuda',
22 'AR' => 'Argentina',
23 'AU' => 'Australia',
24 'AT' => 'Austria',
25 'AZ' => 'Azerbaijan',
26 'BS' => 'Bahamas',
27 'BH' => 'Bahrain',
28 'BD' => 'Bangladesh',
29 'BB' => 'Barbados',
30 'BY' => 'Belarus',
31 'BE' => 'Belgium',
32 'BZ' => 'Belize',
33 'BJ' => 'Benin',
34 'BM' => 'Bermuda',
35 'BT' => 'Bhutan',
36 'BO' => 'Bolivia',
37 'BA' => 'Bosnia and Herzegovina',
38 'BW' => 'Botswana',
39 'BV' => 'Bouvet Island',
40 'BR' => 'Brazil',
41 'IO' => 'British Indian Ocean Territory',
42 'BN' => 'Brunei Darussalam',
43 'BG' => 'Bulgaria',
44 'BF' => 'Burkina Faso',
45 'BI' => 'Burundi',
46 'KH' => 'Cambodia',
47 'CM' => 'Cameroon',
48 'CA' => 'Canada',
49 'CV' => 'Cape Verde',
50 'KY' => 'Cayman Islands',
51 'CF' => 'Central African Republic',
52 'TD' => 'Chad',
53 'CL' => 'Chile',
54 'CN' => 'China',
55 'CX' => 'Christmas Island',
56 'CC' => 'Cocos (Keeling) Islands',
57 'CO' => 'Colombia',
58 'KM' => 'Comoros',
59 'CG' => 'Congo',
60 'CD' => 'Zaire',
61 'CK' => 'Cook Islands',
62 'CR' => 'Costa Rica',
63 'CI' => 'Côte D\'Ivoire',
64 'HR' => 'Croatia',
65 'CU' => 'Cuba',
66 'CY' => 'Cyprus',
67 'CZ' => 'Czech Republic',
68 'DK' => 'Denmark',
69 'DJ' => 'Djibouti',
70 'DM' => 'Dominica',
71 'DO' => 'Dominican Republic',
72 'EC' => 'Ecuador',
73 'EG' => 'Egypt',
74 'SV' => 'El Salvador',
75 'GQ' => 'Equatorial Guinea',
76 'ER' => 'Eritrea',
77 'EE' => 'Estonia',
78 'ET' => 'Ethiopia',
79 'FK' => 'Falkland Islands (Malvinas)',
80 'FO' => 'Faroe Islands',
81 'FJ' => 'Fiji',
82 'FI' => 'Finland',
83 'FR' => 'France',
84 'GF' => 'French Guiana',
85 'PF' => 'French Polynesia',
86 'TF' => 'French Southern Territories',
87 'GA' => 'Gabon',
88 'GM' => 'Gambia',
89 'GE' => 'Georgia',
90 'DE' => 'Germany',
91 'GH' => 'Ghana',
92 'GI' => 'Gibraltar',
93 'GR' => 'Greece',
94 'GL' => 'Greenland',
95 'GD' => 'Grenada',
96 'GP' => 'Guadeloupe',
97 'GU' => 'Guam',
98 'GT' => 'Guatemala',
99 'GG' => 'Guernsey',
100 'GN' => 'Guinea',
101 'GW' => 'Guinea-Bissau',
102 'GY' => 'Guyana',
103 'HT' => 'Haiti',
104 'HM' => 'Heard Island and Mcdonald Islands',
105 'VA' => 'Vatican City State',
106 'HN' => 'Honduras',
107 'HK' => 'Hong Kong',
108 'HU' => 'Hungary',
109 'IS' => 'Iceland',
110 'IN' => 'India',
111 'ID' => 'Indonesia',
112 'IR' => 'Iran, Islamic Republic of',
113 'IQ' => 'Iraq',
114 'IE' => 'Ireland',
115 'IM' => 'Isle of Man',
116 'IL' => 'Israel',
117 'IT' => 'Italy',
118 'JM' => 'Jamaica',
119 'JP' => 'Japan',
120 'JE' => 'Jersey',
121 'JO' => 'Jordan',
122 'KZ' => 'Kazakhstan',
123 'KE' => 'KENYA',
124 'KI' => 'Kiribati',
125 'KP' => 'Korea, Democratic People\'s Republic of',
126 'KR' => 'Korea, Republic of',
127 'KW' => 'Kuwait',
128 'KG' => 'Kyrgyzstan',
129 'LA' => 'Lao People\'s Democratic Republic',
130 'LV' => 'Latvia',
131 'LB' => 'Lebanon',
132 'LS' => 'Lesotho',
133 'LR' => 'Liberia',
134 'LY' => 'Libyan Arab Jamahiriya',
135 'LI' => 'Liechtenstein',
136 'LT' => 'Lithuania',
137 'LU' => 'Luxembourg',
138 'MO' => 'Macao',
139 'MK' => 'Macedonia, the Former Yugoslav Republic of',
140 'MG' => 'Madagascar',
141 'MW' => 'Malawi',
142 'MY' => 'Malaysia',
143 'MV' => 'Maldives',
144 'ML' => 'Mali',
145 'MT' => 'Malta',
146 'MH' => 'Marshall Islands',
147 'MQ' => 'Martinique',
148 'MR' => 'Mauritania',
149 'MU' => 'Mauritius',
150 'YT' => 'Mayotte',
151 'MX' => 'Mexico',
152 'FM' => 'Micronesia, Federated States of',
153 'MD' => 'Moldova, Republic of',
154 'MC' => 'Monaco',
155 'MN' => 'Mongolia',
156 'ME' => 'Montenegro',
157 'MS' => 'Montserrat',
158 'MA' => 'Morocco',
159 'MZ' => 'Mozambique',
160 'MM' => 'Myanmar',
161 'NA' => 'Namibia',
162 'NR' => 'Nauru',
163 'NP' => 'Nepal',
164 'NL' => 'Netherlands',
165 'AN' => 'Netherlands Antilles',
166 'NC' => 'New Caledonia',
167 'NZ' => 'New Zealand',
168 'NI' => 'Nicaragua',
169 'NE' => 'Niger',
170 'NG' => 'Nigeria',
171 'NU' => 'Niue',
172 'NF' => 'Norfolk Island',
173 'MP' => 'Northern Mariana Islands',
174 'NO' => 'Norway',
175 'OM' => 'Oman',
176 'PK' => 'Pakistan',
177 'PW' => 'Palau',
178 'PS' => 'Palestinian Territory, Occupied',
179 'PA' => 'Panama',
180 'PG' => 'Papua New Guinea',
181 'PY' => 'Paraguay',
182 'PE' => 'Peru',
183 'PH' => 'Philippines',
184 'PN' => 'Pitcairn',
185 'PL' => 'Poland',
186 'PT' => 'Portugal',
187 'PR' => 'Puerto Rico',
188 'QA' => 'Qatar',
189 'RE' => 'Réunion',
190 'RO' => 'Romania',
191 'RU' => 'Russian Federation',
192 'RW' => 'Rwanda',
193 'SH' => 'Saint Helena',
194 'KN' => 'Saint Kitts and Nevis',
195 'LC' => 'Saint Lucia',
196 'PM' => 'Saint Pierre and Miquelon',
197 'VC' => 'Saint Vincent and the Grenadines',
198 'WS' => 'Samoa',
199 'SM' => 'San Marino',
200 'ST' => 'Sao Tome and Principe',
201 'SA' => 'Saudi Arabia',
202 'SN' => 'Senegal',
203 'RS' => 'Serbia',
204 'SC' => 'Seychelles',
205 'SL' => 'Sierra Leone',
206 'SG' => 'Singapore',
207 'SK' => 'Slovakia',
208 'SI' => 'Slovenia',
209 'SB' => 'Solomon Islands',
210 'SO' => 'Somalia',
211 'ZA' => 'South Africa',
212 'GS' => 'South Georgia and the South Sandwich Islands',
213 'ES' => 'Spain',
214 'LK' => 'Sri Lanka',
215 'SD' => 'Sudan',
216 'SR' => 'Suriname',
217 'SJ' => 'Svalbard and Jan Mayen',
218 'SZ' => 'Swaziland',
219 'SE' => 'Sweden',
220 'CH' => 'Switzerland',
221 'SY' => 'Syrian Arab Republic',
222 'TW' => 'Taiwan, Province of China',
223 'TJ' => 'Tajikistan',
224 'TZ' => 'Tanzania, United Republic of',
225 'TH' => 'Thailand',
226 'TL' => 'Timor-Leste',
227 'TG' => 'Togo',
228 'TK' => 'Tokelau',
229 'TO' => 'Tonga',
230 'TT' => 'Trinidad and Tobago',
231 'TN' => 'Tunisia',
232 'TR' => 'Turkey',
233 'TM' => 'Turkmenistan',
234 'TC' => 'Turks and Caicos Islands',
235 'TV' => 'Tuvalu',
236 'UG' => 'Uganda',
237 'UA' => 'Ukraine',
238 'AE' => 'United Arab Emirates',
239 'GB' => 'United Kingdom',
240 'US' => 'United States',
241 'UM' => 'United States Minor Outlying Islands',
242 'UY' => 'Uruguay',
243 'UZ' => 'Uzbekistan',
244 'VU' => 'Vanuatu',
245 'VE' => 'Venezuela',
246 'VN' => 'Viet Nam',
247 'VG' => 'Virgin Islands, British',
248 'VI' => 'Virgin Islands, U.S.',
249 'WF' => 'Wallis and Futuna',
250 'EH' => 'Western Sahara',
251 'YE' => 'Yemen',
252 'ZM' => 'Zambia',
253 'ZW' => 'Zimbabwe',
254 );
255
256 public static function countryNameToCode($name)
257 {
258 return array_search($name, self::$_countryCodes);
259 }
260
261 public static function countryNameFromLatLong($lat, $long)
262 {
263 $url = 'http://maps.google.com/maps/api/geocode/json?address=' . $lat .',' . $long .'&sensor=false';
264 $get = file_get_contents($url);
265 $geoData = json_decode($get);
266 if (json_last_error() !== JSON_ERROR_NONE) {
267 throw new \InvalidArgumentException('Invalid geocoding results');
268 }
269
270 if(isset($geoData->results[0])) {
271 foreach($geoData->results[0]->address_components as $addressComponent) {
272 if(in_array('country', $addressComponent->types)) {
273 return $addressComponent->long_name;
274 }
275 }
276 }
277 return null;
278 }
279
280 public static function bytesToHumanReadable($bytes, $dec = 2)
281 {
282 if(!is_int($bytes)) throw new \Exception('Bytes must be an int.' . var_dump($bytes));
283
284 //Shamelessly stolen from stackoverflow: http://stackoverflow.com/questions/15188033/human-readable-file-size
285 $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
286 $factor = floor((strlen($bytes) - 1) / 3);
287
288 return sprintf("%.{$dec}f", $bytes / pow(1000, $factor)) . @$size[$factor];
289 }
290
291 public static function packToArray(IPack $pack)
292 {
293 return array(
294 'title'=> $pack->getTitle(),
295 'contributors' => $pack->getContributors(),
296 'simfiles' => self::getPackSimfilesArray($pack),
297 'banner' => $pack->getBanner() ? 'files/banner/' . $pack->getBanner()->getHash() : 'files/banner/default',
298 'mirrors' => self::getPackMirrorsArray($pack),
299 'size' => $pack->getFile() ? self::bytesToHumanReadable($pack->getFile()->getSize()) : null,
300 'uploaded' => $pack->getFile() ? date('F jS, Y', $pack->getFile()->getUploadDate()) : null,
301 'hash' => $pack->getFile() ? $pack->getFile()->getHash() : null
302 );
303 }
304
305 public static function getPackMirrorsArray(IPack $pack)
306 {
307 $packMirrors = array();
308
309 if($pack->getFile())
310 {
311 $packMirrors[] = array('source' => 'DivinElegy', 'uri' => 'files/pack/' . $pack->getFile()->getHash());
312 }
313
314 if($pack->getFile()->getMirrors())
315 {
316 foreach($pack->getFile()->getMirrors() as $mirror)
317 {
318 $packMirrors[] = array('source' => $mirror->getSource(), 'uri' => $mirror->getUri());
319 }
320 }
321
322 return $packMirrors;
323 }
324
325 public static function getPackSimfilesArray(IPack $pack)
326 {
327 $packSimfiles = array();
328 foreach($pack->getSimfiles() as $simfile)
329 {
330 $packSimfiles[] = self::simfileToArray($simfile);
331 }
332
333 return $packSimfiles;
334 }
335
336 public static function simfileToArray(Entities\StepMania\ISimfile $simfile)
337 {
338 $singleSteps = array();
339 $doubleSteps = array();
340
341 foreach($simfile->getSteps() as $steps)
342 {
343 $stepDetails = array(
344 'artist' => $steps->getArtist()->getTag(),
345 'difficulty' => $steps->getDifficulty()->getITGName(),
346 'rating' => $steps->getRating()
347 );
348
349 if($steps->getMode()->getPrettyName() == 'Single')
350 {
351 $singleSteps[] = $stepDetails;
352 } else {
353 $doubleSteps[] = $stepDetails;
354 }
355 }
356
357 return array(
358 'title' => $simfile->getTitle(),
359 'artist' => $simfile->getArtist()->getName(),
360 'steps' => array(
361 'single' => $singleSteps,
362 'double' => $doubleSteps
363 ),
364 'bgChanges' => $simfile->hasBgChanges() ? 'Yes' : 'No',
365 'fgChanges' => $simfile->hasFgChanges() ? 'Yes' : 'No',
366 'bpmChanges' => $simfile->hasBPMChanges() ? 'Yes' : 'No',
367 'banner' => $simfile->getBanner() ? 'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default',
368 'download' => $simfile->getSimfile() ? 'files/simfile/' . $simfile->getSimfile()->getHash() : null,
369 'size' => $simfile->getSimfile() ? Util::bytesToHumanReadable($simfile->getSimfile()->getSize()) : null,
370 'uploaded' => $simfile->getSimfile() ? date('F jS, Y', $simfile->getSimfile()->getUploadDate()) : null,
371 'hash' => $simfile->getSimfile() ? $simfile->getSimfile()->getHash() : null
372 );
373 }
374 }