1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Jean Silva <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Jeancsil\FlightSpy\History\ElasticSearch; |
7
|
|
|
|
8
|
|
|
class MappingProcessor implements Processor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $dataCache; |
14
|
|
|
|
15
|
|
|
public function process(array $data) { |
16
|
|
|
$this->dataCache = $data; |
17
|
|
|
|
18
|
|
|
$mappedData = []; |
|
|
|
|
19
|
|
|
$mappedData['Creation'] = (new \DateTime())->format(\DATE_ATOM); |
|
|
|
|
20
|
|
|
$mappedData['SessionKey'] = $data['SessionKey']; |
21
|
|
|
$mappedData['Status'] = $data['Status']; |
|
|
|
|
22
|
|
|
$mappedData['Query'] = $data['Query']; |
|
|
|
|
23
|
|
|
unset($mappedData['Query']['LocationSchema']); |
24
|
|
|
|
25
|
|
|
$mappedData['Query']['OriginPlace'] = $this->getPlaceNameById($data['Query']['OriginPlace']); |
|
|
|
|
26
|
|
|
$mappedData['Query']['DestinationPlace'] = $this->getPlaceNameById($data['Query']['DestinationPlace']); |
27
|
|
|
$mappedData['Query']['OutboundDate'] = $this->formatDate($mappedData['Query']['OutboundDate']); |
|
|
|
|
28
|
|
|
$mappedData['Query']['InboundDate'] = $this->formatDate($mappedData['Query']['InboundDate']); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$count = 0; |
|
|
|
|
31
|
|
|
$mappedData['Itineraries'] = []; |
32
|
|
|
foreach ($data['Itineraries'] as $itinerary) { |
33
|
|
|
$outboundCarrier = $this->getCarrier($itinerary['OutboundLegId']); |
34
|
|
|
$inboundCarrier = $this->getCarrier($itinerary['InboundLegId']); |
|
|
|
|
35
|
|
|
|
36
|
|
|
foreach ($itinerary['PricingOptions'] as $priceOption) { |
37
|
|
|
$mappedData['Itineraries'][$count] = [ |
38
|
|
|
'Price' => $priceOption['Price'], |
39
|
|
|
'Deeplink' => 'none',//$priceOption['DeeplinkUrl'], |
40
|
|
|
'OutboundCarrier' => $outboundCarrier['Name'], |
41
|
|
|
'InboundCarrier' => $inboundCarrier['Name'], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$count++; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $mappedData; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function formatDate($date) { |
52
|
|
|
return (new \DateTime($date)) |
53
|
|
|
->format(\DATE_ATOM); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $placeId |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
private function getPlaceNameById($placeId) { |
61
|
|
|
foreach ($this->dataCache['Places'] as $place) { |
62
|
|
|
if ($place['Id'] == $placeId) { |
63
|
|
|
return $place['Name']; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $legId |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function getCarrier($legId) { |
73
|
|
|
foreach ($this->dataCache['Legs'] as $leg) { |
74
|
|
|
if ($leg['Id'] == $legId) { |
75
|
|
|
foreach ($this->dataCache['Carriers'] as $carrier) { |
76
|
|
|
if ($carrier['Id'] == $leg['Carriers'][0]) { |
77
|
|
|
return $carrier; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.