public static createFromXml ( SimpleXMLElement $xml, DateTime $date, |
||
$xml | SimpleXMLElement | |
$date | DateTime | |
$obj |
public static function createFromXml(\SimpleXMLElement $xml, \DateTime $date, Stop $obj = null)
{
if (!$obj) {
$obj = new self();
}
$obj->station = Entity\Location\Station::createStationFromXml($xml->Station);
// deprecated, use location instead
foreach ($xml->children() as $location) {
$location = Entity\LocationFactory::createFromXml($location);
if ($location) {
$obj->location = $location;
break;
}
}
$isArrival = false;
if ($xml->Arr) {
$isArrival = true;
$arrivalDate = self::calculateDateTime((string) $xml->Arr->Time, $date, $xml->StopPrognosis);
$obj->arrival = $arrivalDate->format(\DateTime::ISO8601);
$obj->arrivalTimestamp = $arrivalDate->getTimestamp();
$obj->platform = trim((string) $xml->Arr->Platform->Text);
}
if ($xml->Dep) {
$departureDate = self::calculateDateTime((string) $xml->Dep->Time, $date, $xml->StopPrognosis);
$obj->departure = $departureDate->format(\DateTime::ISO8601);
$obj->departureTimestamp = $departureDate->getTimestamp();
$obj->platform = trim((string) $xml->Dep->Platform->Text);
}
$obj->prognosis = Prognosis::createFromXml($xml->StopPrognosis, $date, $isArrival);
if ($obj->prognosis) {
if ($obj->prognosis->arrival && $obj->arrival) {
$obj->delay = (strtotime($obj->prognosis->arrival) - strtotime($obj->arrival)) / 60;
}
if ($obj->prognosis->departure && $obj->departure) {
$obj->delay = (strtotime($obj->prognosis->departure) - strtotime($obj->departure)) / 60;
}
}
if ($xml->StAttrList) {
foreach ($xml->StAttrList->StAttr as $attr) {
if ($attr['code'] == 'RA') {
$obj->realtimeAvailability = (string) $attr['text'];
}
}
}
return $obj;
}
public static function createFromXml(\SimpleXMLElement $xml, Connection $obj = null) { if (!$obj) { $obj = new self(); } $date = \DateTime::createFromFormat('Ymd', (string) $xml->Overview->Date, new \DateTimeZone('Europe/Zurich')); $date->setTimezone(new \DateTimeZone('Europe/Zurich')); $date->setTime(0, 0, 0); $obj->from = Entity\Schedule\Stop::createFromXml($xml->Overview->Departure->BasicStop, $date, null); $obj->to = Entity\Schedule\Stop::createFromXml($xml->Overview->Arrival->BasicStop, $date, null); $obj->duration = (string) $xml->Overview->Duration->Time; $obj->transfers = (int) $xml->Overview->Transfers; $obj->service = new Entity\Schedule\Service(); if (isset($xml->Overview->ServiceDays->RegularServiceText)) { $obj->service->regular = (string) $xml->Overview->ServiceDays->RegularServiceText->Text; } if (isset($xml->Overview->ServiceDays->IrregularServiceText)) { $obj->service->irregular = (string) $xml->Overview->ServiceDays->IrregularServiceText->Text; } if (isset($xml->Overview->Products->Product)) { foreach ($xml->Overview->Products->Product as $product) { $obj->products[] = trim((string) $product['cat']); } } $capacities1st = []; $capacities2nd = []; foreach ($xml->ConSectionList->ConSection as $section) { if ($section->Journey) { if ($section->Journey->PassList->BasicStop) { foreach ($section->Journey->PassList->BasicStop as $stop) { if (isset($stop->StopPrognosis->Capacity1st)) { $capacities1st[] = (int) $stop->StopPrognosis->Capacity1st; } if (isset($stop->StopPrognosis->Capacity2nd)) { $capacities2nd[] = (int) $stop->StopPrognosis->Capacity2nd; } } } } } if (count($capacities1st) > 0) { $obj->capacity1st = max($capacities1st); } if (count($capacities2nd) > 0) { $obj->capacity2nd = max($capacities2nd); } foreach ($xml->ConSectionList->ConSection as $section) { $obj->sections[] = Entity\Schedule\Section::createFromXml($section, $date, null); } return $obj; }