<?php
namespace App\Amadeus\StructureParser\Parser; abstract class Itinerary { protected $transaction = null; protected $itinerary = null; private $cached_tripData = null; public function __construct($transaction) { $this->transaction = $transaction; $this->itinerary = new \App\Amadeus\StructureParser\Types\Itinerary(); $this->initialize(); } abstract protected function updateInternalSegmentData(); public function initialize() { $this->updateInternalSegmentData(); $this->itinerary->setDeparture($this->getDeparture()); $this->itinerary->setDestination($this->getDestination()); } public function destroy() { $this->transaction = null; $this->itinerary->destroy(); $this->itinerary = null; } public function isValid() { return true; } public function getItineraryObj() { return $this->itinerary; } public function getSegments() { return $this->itinerary->getSegments(); } public function getSegmentsStatuses() { $ret = []; foreach($this->itinerary->getSegments() as $segment) { $ret[] = $segment->segmentStatus; } return $ret; } public function getDeparture() { return $this->internal_getDeparture(); } public function getDestination() { return $this->internal_getDestination(); } public function getTripType() { return $this->itinerary->getTripType(); } protected function internal_getDeparture($segments = null) { if($segments===null){ $segments = $this->getSegments(); } $tripData = $this->itinerary->getItineraryAnalizer(); if(count($segments)==1){ return $segments[0]->getDeparture(); } if(!$tripData->getType() && $segments[0]) { return $segments[0]->getDeparture(); } elseif($tripData->getDeparture()) { return $segments[$tripData->getDepartureIndex()->index]->getDeparture(); } return null; } protected function internal_getDestination($segments = null) { if($segments===null){ $segments = $this->getSegments(); } if(count($segments)==1){ return $segments[0]->getDestination(); } $tripData = $this->itinerary->getItineraryAnalizer(); $keys = array_keys($segments); $lastSegmentIdx = end($keys); $ret = null; if ($segments) { if (!$tripData->getType()) { $ret = $segments[0]->getDestination(); } elseif ($segments[$tripData->getArrivalIndex()->index]) { if ($tripData->getArrivalIndex()->node =='departure') { $ret = $segments[$tripData->getArrivalIndex()->index]->getDeparture(); } else { $ret = $segments[$tripData->getArrivalIndex()->index]->getDestination(); } } if ($ret) { $ret->date = $segments[$lastSegmentIdx]->destination->date; $ret->time = $segments[$lastSegmentIdx]->destination->time; return $ret; } } return null; } } 