<?php
 namespace App\Utils; class SortServicesSegments { protected $idOrder = null; protected $serviceSegments = []; public function __construct($serviceSegments = []) { if ($serviceSegments) { $this->setServiceSegments($serviceSegments); } } public function setServiceSegments(Array $serviceSegments) { $this->serviceSegments = $serviceSegments; } public function importFromOrder($idOrder) { $this->serviceSegments = \Entity\Order::select($idOrder)->servicesSegmentsList; } public function getSortedServicesSegments() { foreach ($this->serviceSegments as $servSegmDet) { $idService = $servSegmDet['idService']; if ($servSegmDet['segmentIndex']!==null) { $segmentIndex = $servSegmDet['segmentIndex']; } else { $segmentIndex = null; } $servEnt = \Entity\Service::select($idService); $offEnt = $servEnt->chosenOffer; if ($segmentIndex===null) { if ($offEnt->recordExists()) { $startDate = $offEnt->startDatetime->formatDatetimeIso(); } else { $startDate = $servEnt->startDate; } $services[$idService.'_'.$segmentIndex] = $startDate; } else { $itinSegments = []; if ($offEnt->itinerary) { $itinSegments = $offEnt->itinerary->getSegments(); } if ($itinSegments and $itinSegments[$segmentIndex]) { $currSegment = $itinSegments[$segmentIndex]; $services[$idService.'_'.$segmentIndex] = $currSegment->getDepartureDateTime()->formatDatetimeIso(); } } } asort($services); $sortedServices = array_keys($services); $return = []; foreach($sortedServices as $servDet) { list($idService, $segmentIndex) = explode('_', $servDet); if (is_numeric($segmentIndex)) { $return[$idService.'_'.$segmentIndex] = ['idService'=>$idService, 'segmentIndex'=>$segmentIndex]; } else { $return[$idService.'_'] = ['idService'=>$idService]; } } return $return; } } 