Skip to content

Commit 54279b0

Browse files
committed
Ensure all known section keys are defined with a default value as is
expected from the old platform. As we want to ensure close compatibility for existing implementations, we want to ensure all data array keys are properly set since not everyone will check those in advance of accessing them. (cherry picked from commit 60eb40c)
1 parent 30a5148 commit 54279b0

File tree

2 files changed

+154
-74
lines changed

2 files changed

+154
-74
lines changed

src/Vatsimphp/Parser/DataV3CompatParser.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class DataV3CompatParser extends DataParser
105105
'visualrange' => 'visual_range',
106106
'atis_message' => 'text_atis',
107107
'time_logon' => 'logon_time',
108+
'time_last_atis_received' => 'last_updated',
108109
];
109110

110111
/**
@@ -166,7 +167,7 @@ protected function parseSections()
166167

167168
if (isset($this->json['pilots']) && is_array($this->json['pilots'])) {
168169
foreach ($this->json['pilots'] as $in) {
169-
$pilot = $this->convertToLegacy($in, $this->pilotMap);
170+
$pilot = $this->convertToLegacy($in, $this->pilotMap, $this->getSectionDefault("clients"));
170171
if (isset($in['flight_plan']) && is_array($in['flight_plan'])) {
171172
$pilot = $this->convertToLegacy($in['flight_plan'], $this->flightPlanMap, $pilot);
172173
}
@@ -177,7 +178,7 @@ protected function parseSections()
177178

178179
if (isset($this->json['controllers']) && is_array($this->json['controllers'])) {
179180
foreach ($this->json['controllers'] as $in) {
180-
$controller = $this->convertToLegacy($in, $this->controllerMap);
181+
$controller = $this->convertToLegacy($in, $this->controllerMap, $this->getSectionDefault("clients"));
181182
$controller[VatsimData::HEADER_CLIENT_TYPE] = VatsimData::CLIENT_TYPE_ATC;
182183
$clients[] = $controller;
183184
}
@@ -193,7 +194,7 @@ protected function parseSections()
193194

194195
if (isset($this->json['prefiles']) && is_array($this->json['prefiles'])) {
195196
foreach ($this->json['prefiles'] as $in) {
196-
$prefile = $this->convertToLegacy($in, $this->prefileMap);
197+
$prefile = $this->convertToLegacy($in, $this->prefileMap, $this->getSectionDefault("prefile"));
197198
if (isset($in['flight_plan']) && is_array($in['flight_plan'])) {
198199
$prefile = $this->convertToLegacy($in['flight_plan'], $this->flightPlanMap, $prefile);
199200
}
@@ -210,7 +211,7 @@ protected function parseSections()
210211

211212
if (isset($this->json['servers']) && is_array($this->json['servers'])) {
212213
foreach ($this->json['servers'] as $in) {
213-
$servers[] = $this->convertToLegacy($in, $this->serverMap);
214+
$servers[] = $this->convertToLegacy($in, $this->serverMap, $this->getSectionDefault("servers"));
214215
}
215216
}
216217

@@ -299,4 +300,23 @@ protected function convertISO8601ToLegacy($str)
299300
$dt->setTimezone(new \DateTimeZone('UTC'));
300301
return $dt->format("YmdHis");
301302
}
303+
304+
/**
305+
* Return section default skeleton/values.
306+
*
307+
* @param string $section The section name as defined in $this->sectionHeaders
308+
*
309+
* @return array
310+
*/
311+
protected function getSectionDefault($section) {
312+
$default = [];
313+
if (isset($this->sectionsHeaders[$section])) {
314+
foreach ($this->sectionsHeaders[$section] as $header) {
315+
// Setting empty string to avoid having per field defaults which is
316+
// is out of scope of the compatibility.
317+
$default[$header] = "";
318+
}
319+
}
320+
return $default;
321+
}
302322
}

tests/Vatsimphp/Parser/DataV3CompatParserTest.php

+130-70
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ public function providerTestParseData()
114114
},
115115
"logon_time":"2021-01-01T20:10:13.1294438Z",
116116
"last_updated":"2021-01-02T00:17:15.664797Z"
117+
},
118+
{
119+
"cid": 99887766,
120+
"name": "John Doe II",
121+
"callsign": "SWA3436",
122+
"server": "USA-WEST",
123+
"pilot_rating": 1,
124+
"latitude": 35.57682,
125+
"longitude": -115.04695,
126+
"altitude": 29273,
127+
"groundspeed": 467,
128+
"transponder": "3262",
129+
"heading": 253,
130+
"qnh_i_hg": 30.18,
131+
"qnh_mb": 1022,
132+
"logon_time":"2021-01-01T20:10:13.1294438Z",
133+
"last_updated":"2021-01-02T00:17:15.664797Z"
117134
}
118135
],
119136
"controllers": [
@@ -174,7 +191,7 @@ public function providerTestParseData()
174191
'cid' => '1234567',
175192
'realname' => 'John Doe',
176193
'clienttype' => 'PILOT',
177-
//'frequency' => '',
194+
'frequency' => '',
178195
'latitude' => '34.57682',
179196
'longitude' => '-116.04695',
180197
'altitude' => '29273',
@@ -185,28 +202,71 @@ public function providerTestParseData()
185202
'planned_altitude' => '50000',
186203
'planned_destairport' => 'KLAX',
187204
'server' => 'USA-WEST',
188-
//'protrevision' => '100',
205+
'protrevision' => '', // 100
189206
'rating' => '1',
190207
'transponder' => '3262',
191-
//'facilitytype' => '0',
192-
//'visualrange' => '0',
193-
//'planned_revision' => '4',
208+
'facilitytype' => '', // 0
209+
'visualrange' => '', // 0
210+
'planned_revision' => '', // 4
194211
'planned_flighttype' => 'I',
195212
'planned_deptime' => '0',
196-
//'planned_actdeptime' => '0',
213+
'planned_actdeptime' => '', // 0
197214
'planned_hrsenroute' => '0',
198-
//'planned_minenroute' => '0',
215+
'planned_minenroute' => '', // 0
199216
'planned_hrsfuel' => '0',
200-
//'planned_minfuel' => '0',
217+
'planned_minfuel' => '', // 0
201218
'planned_altairport' => 'KONT',
202219
'planned_remarks' => 'A/C Type Concorde. RNAV and RVSM equipped. /v/',
203220
'planned_route' => '+PEKUE PIPPN ROTTN PWE J64 TBC JASSE Q90 DNERO ANJLL4',
204-
//'planned_depairport_lat' => '0',
205-
//'planned_depairport_lon' => '0',
206-
//'planned_destairport_lat' => '0',
207-
//'planned_destairport_lon' => '0',
208-
//'atis_message' => '',
209-
//'time_last_atis_received' => '20210101201013',
221+
'planned_depairport_lat' => '', // 0
222+
'planned_depairport_lon' => '', // 0
223+
'planned_destairport_lat' => '', // 0
224+
'planned_destairport_lon' => '', // 0
225+
'atis_message' => '',
226+
'time_last_atis_received' => '', // 20210101201013
227+
'time_logon' => '20210101201013',
228+
'heading' => '253',
229+
'QNH_iHg' => '30.18',
230+
'QNH_Mb' => '1022',
231+
],
232+
[
233+
'callsign' => 'SWA3436',
234+
'cid' => '99887766',
235+
'realname' => 'John Doe II',
236+
'clienttype' => 'PILOT',
237+
'frequency' => '',
238+
'latitude' => '35.57682',
239+
'longitude' => '-115.04695',
240+
'altitude' => '29273',
241+
'groundspeed' => '467',
242+
'planned_aircraft' => '',
243+
'planned_tascruise' => '',
244+
'planned_depairport' => '',
245+
'planned_altitude' => '',
246+
'planned_destairport' => '',
247+
'server' => 'USA-WEST',
248+
'protrevision' => '', // 100
249+
'rating' => '1',
250+
'transponder' => '3262',
251+
'facilitytype' => '', // 0
252+
'visualrange' => '', // 0
253+
'planned_revision' => '',
254+
'planned_flighttype' => '',
255+
'planned_deptime' => '',
256+
'planned_actdeptime' => '',
257+
'planned_hrsenroute' => '',
258+
'planned_minenroute' => '',
259+
'planned_hrsfuel' => '',
260+
'planned_minfuel' => '',
261+
'planned_altairport' => '',
262+
'planned_remarks' => '',
263+
'planned_route' => '',
264+
'planned_depairport_lat' => '',
265+
'planned_depairport_lon' => '',
266+
'planned_destairport_lat' => '',
267+
'planned_destairport_lon' => '',
268+
'atis_message' => '',
269+
'time_last_atis_received' => '', // 20210101201013
210270
'time_logon' => '20210101201013',
211271
'heading' => '253',
212272
'QNH_iHg' => '30.18',
@@ -218,87 +278,87 @@ public function providerTestParseData()
218278
'realname' => 'John Foe',
219279
'clienttype' => 'ATC',
220280
'frequency' => '135.000',
221-
//'latitude' => '42.36296',
222-
//'longitude' => '-71.00643',
223-
//'altitude' => '0',
224-
//'groundspeed' => '0',
225-
//'planned_aircraft' => '',
226-
//'planned_tascruise' => '',
227-
//'planned_depairport' => '',
228-
//'planned_altitude' => '',
229-
//'planned_destairport' => '',
281+
'latitude' => '', // 42.36296
282+
'longitude' => '', // -71.00643
283+
'altitude' => '', // 0
284+
'groundspeed' => '', // 0
285+
'planned_aircraft' => '',
286+
'planned_tascruise' => '',
287+
'planned_depairport' => '',
288+
'planned_altitude' => '',
289+
'planned_destairport' => '',
230290
'server' => 'USA-WEST',
231-
//'protrevision' => '100',
291+
'protrevision' => '', // 100
232292
'rating' => '3',
233-
//'transponder' => '0',
293+
'transponder' => '', // 0
234294
'facilitytype' => '4',
235295
'visualrange' => '50',
236-
//'planned_revision' => '',
237-
//'planned_flighttype' => '',
238-
//'planned_deptime' => '',
239-
//'planned_actdeptime' => '',
240-
//'planned_hrsenroute' => '',
241-
//'planned_minenroute' => '',
242-
//'planned_hrsfuel' => '',
243-
//'planned_minfuel' => '',
244-
//'planned_altairport' => '',
245-
//'planned_remarks' => '',
246-
//'planned_route' => '',
247-
//'planned_depairport_lat' => '0',
248-
//'planned_depairport_lon' => '0',
249-
//'planned_destairport_lat' => '0',
250-
//'planned_destairport_lon' => '0',
296+
'planned_revision' => '',
297+
'planned_flighttype' => '',
298+
'planned_deptime' => '',
299+
'planned_actdeptime' => '',
300+
'planned_hrsenroute' => '',
301+
'planned_minenroute' => '',
302+
'planned_hrsfuel' => '',
303+
'planned_minfuel' => '',
304+
'planned_altairport' => '',
305+
'planned_remarks' => '',
306+
'planned_route' => '',
307+
'planned_depairport_lat' => '', // 0
308+
'planned_depairport_lon' => '', // 0
309+
'planned_destairport_lat' => '', // 0
310+
'planned_destairport_lon' => '', // 0
251311
'atis_message' => 'BOSTON LOGAN AIRPORT ATIS INFORMATION Z. 2254Z. 15005KT 10SM FEW120 BKN160 OVC250 01/-03 A3040. ILS RWY 22L APCH IN USE, DEPTG RWY 22R.',
252-
//'time_last_atis_received' => '20210101202838',
312+
'time_last_atis_received' => '20210102202838',
253313
'time_logon' => '20210101202838',
254-
//'heading' => '0',
255-
//'QNH_iHg' => '0',
256-
//'QNH_Mb' => '0',
314+
'heading' => '', // 0
315+
'QNH_iHg' => '', // 0
316+
'QNH_Mb' => '', // 0
257317
],
258318
],
259319
'prefile' => [
260320
[
261321
'callsign' => 'SWA3438',
262322
'cid' => '11223344556677',
263323
'realname' => 'Jane Doe',
264-
//'clienttype' => '',
265-
//'frequency' => '',
266-
//'latitude' => '0',
267-
//'longitude' => '0',
268-
//'altitude' => '0',
269-
//'groundspeed' => '0',
324+
'clienttype' => '',
325+
'frequency' => '',
326+
'latitude' => '', // 0
327+
'longitude' => '', // 0
328+
'altitude' => '', // 0
329+
'groundspeed' => '', // 0
270330
'planned_aircraft' => 'A21N/L',
271331
'planned_tascruise' => '449',
272332
'planned_depairport' => 'KCLT',
273333
'planned_altitude' => '37000',
274334
'planned_destairport' => 'KBOS',
275-
//'server' => '',
276-
//'protrevision' => '0',
277-
//'rating' => '0',
278-
//'transponder' => '0',
279-
//'facilitytype' => '0',
280-
//'visualrange' => '0',
281-
//'planned_revision' => '3',
335+
'server' => '',
336+
'protrevision' => '', // 0
337+
'rating' => '', // 0
338+
'transponder' => '', // 0
339+
'facilitytype' => '', // 0
340+
'visualrange' => '', // 0
341+
'planned_revision' => '', // 3
282342
'planned_flighttype' => 'I',
283343
'planned_deptime' => '2035',
284-
//'planned_actdeptime' => '2035',
344+
'planned_actdeptime' => '', // 2035
285345
'planned_hrsenroute' => '0129', // needs parsing
286-
//'planned_minenroute' => '29',
346+
'planned_minenroute' => '', // 29
287347
'planned_hrsfuel' => '0336', // needs parsing
288-
//'planned_minfuel' => '36',
348+
'planned_minfuel' => '', // 36
289349
'planned_altairport' => 'KJFK',
290350
'planned_remarks' => 'SEL/FMLP PER/C TALT/KATL RMK/TCAS /V/',
291351
'planned_route' => 'BARMY4 RDU THHMP OOD J42 RBV J222 JFK ROBUC3',
292-
//'planned_depairport_lat' => '0',
293-
//'planned_depairport_lon' => '0',
294-
//'planned_destairport_lat' => '0',
295-
//'planned_destairport_lon' => '0',
296-
// 'atis_message' => '',
297-
//'time_last_atis_received' => '00010101000000',
298-
//'time_logon' => '00010101000000',
299-
//'heading' => '0',
300-
//'QNH_iHg' => '0',
301-
//'QNH_Mb' => '0',
352+
'planned_depairport_lat' => '', // 0
353+
'planned_depairport_lon' => '', // 0
354+
'planned_destairport_lat' => '', // 0
355+
'planned_destairport_lon' => '', // 0
356+
'atis_message' => '',
357+
'time_last_atis_received' => '', // 00010101000000
358+
'time_logon' => '', // 00010101000000
359+
'heading' => '', // 0
360+
'QNH_iHg' => '', // 0
361+
'QNH_Mb' => '', // 0
302362
],
303363
],
304364
'servers' => [

0 commit comments

Comments
 (0)