<?php
namespace App\Amadeus\StructureParser\Types; class Itinerary extends \App\Amadeus\StructureParser\Types\CompressedSerializableObject { public $segments = []; public $departureSegmentIndex = null; public $destinationSegmentIndex = null; public $departure = null; public $destination = null; protected $itineraryAnalizer = null; protected function serializeUnknownObject($ret, $object) { if ($ret['$%c*'] == 'App\Utils\Itinerary\ItineraryAnalyser') { $ret['skip'] = true; return $ret; } if ($ret['$%c*'] == 'stdClass') { $ret['data'] = serialize($object); return $ret; } return parent::serializeUnknownObject($ret, $object); } protected function unserializeUnknownObject($object, $attributes) { if ($attributes['$%c*'] == 'stdClass') { $object = unserialize($attributes['data']); return $object; } return parent::unserializeUnknownObject($object, $attributes); } public function destroy() { foreach($this->segments as $k=>$segment){ $segment->destroy(); unset($this->segments[$k]); } $this->segments = []; $this->departure = null; $this->destination = null; } public function appendSegment(\App\Amadeus\StructureParser\Types\ItinerarySegment $segment) { $this->segments[] = $segment; return $this; } public function hasSegments() { return (bool)$this->segments; } public function getSegments() { return $this->segments; } public function getSegment($index = 0) { return $this->segments[$index]; } public function getDeparture() { if(!$this->departure && $this->segments[(int)$this->departureSegmentIndex]){ $this->departure = $this->segments[(int)$this->departureSegmentIndex]->getDeparture(); } return $this->departure; } public function getDestination() { if (!$this->destination && $this->segments[(int)$this->destinationSegmentIndex]){ $this->destination = $this->segments[(int)$this->destinationSegmentIndex]->getDestination(); } return $this->destination; } public function setDeparture(?\App\Amadeus\StructureParser\Types\ItineraryLocation $segment=null) { $this->departure = $segment; return $this; } public function setDestination(?\App\Amadeus\StructureParser\Types\ItineraryLocation $segment=null) { $this->destination = $segment; return $this; } public function validate() { $hasDeparture = (bool)$this->departure; $hasDestination = (bool)$this->destination; $foundDeparture = null; $foundDestination = null; if($this->segments) { foreach($this->segments as $index=>$segment) { if($hasDeparture && !$foundDeparture && $this->departure===$segment) { $foundDeparture = $index; } if($hasDestination && !$foundDestination && $this->destination===$segment) { $foundDestination = $index; } } if($hasDeparture && $foundDeparture===null) { throw new \App\Amadeus\StructureParser\StructureException("This itinerary has a departure, but doesn't match any itinerary segment"); } if($hasDestination && $foundDestination===null) { throw new \App\Amadeus\StructureParser\StructureException("This itinerary has a destination, but doesn't match any itinerary segment"); } if($hasDeparture) { $this->departureSegmentIndex = $foundDeparture; } if($hasDestination) { $this->destinationSegmentIndex = $foundDestination; } } return $this; } public function getTripType() { return $this->getItineraryAnalizer()->getType(); } public function getItineraryAnalizer() { if ($this->itineraryAnalizer===null) { $itinerary = $this->generateCompatibleItinerary(); $itineraryAnalyser = new \App\Utils\Itinerary\ItineraryAnalyser($itinerary); $itineraryAnalyser->analyse(); $this->itineraryAnalizer = $itineraryAnalyser; } return $this->itineraryAnalizer; } public function generateCompatibleItinerary() { $segments = $this->getSegments(); $itinerary = new \App\Itinerary(); if ($segments) { foreach($segments as $idx=>$seg){ $nextSeg = $segments[$idx+1]; $itinerarySegment = new \App\ItinerarySegment(); $itinerarySegment->setDeparturePlace($seg->departure->countryCode, $seg->departure->cityId, $seg->departure->airportCode); $depDateTime = new \Engine\DateTime($seg->departure->date.' '.$seg->departure->time); $itinerarySegment->setDepartureDateTime($depDateTime); $itinerarySegment->setArrivalPlace($seg->destination->countryCode, $seg->destination->cityId, $seg->destination->airportCode); $arrDateTime = new \Engine\DateTime($seg->destination->date.' '.$seg->destination->time); $itinerarySegment->setArrivalDateTime($arrDateTime); $itinerarySegment->setDuration($seg->duration); if ($nextSeg) { $itinerarySegment->setStopOverAllowed($nextSeg->stopOverPermitted); } $itinerarySegment->setIdSupplier($seg->supplierCode); $itinerarySegment->setTransportNumber($seg->transportNumber); $itinerary->addSegment($itinerarySegment); } } return $itinerary; } } 