createFromXml() public static method

public static createFromXml ( SimpleXMLElement $xml )
$xml SimpleXMLElement
Ejemplo n.º 1
0
 public static function createFromXml(\SimpleXMLElement $xml, \DateTime $date, Stop $obj = null)
 {
     if (!$obj) {
         $obj = new Stop();
     }
     $dateTime = null;
     $isArrival = false;
     $obj->station = Entity\Location\Station::createFromXml($xml->Station);
     // deprecated, use location instead
     foreach ($xml->children() as $location) {
         $location = Entity\LocationFactory::createFromXml($location);
         if ($location) {
             $obj->location = $location;
             break;
         }
     }
     if ($xml->Arr) {
         $isArrival = true;
         $dateTime = self::calculateDateTime((string) $xml->Arr->Time, $date);
         $obj->arrival = $dateTime->format(\DateTime::ISO8601);
         $obj->arrivalTimestamp = $dateTime->getTimestamp();
         $obj->platform = trim((string) $xml->Arr->Platform->Text);
     }
     if ($xml->Dep) {
         $dateTime = self::calculateDateTime((string) $xml->Dep->Time, $date);
         $obj->departure = $dateTime->format(\DateTime::ISO8601);
         $obj->departureTimestamp = $dateTime->getTimestamp();
         $obj->platform = trim((string) $xml->Dep->Platform->Text);
     }
     $obj->prognosis = Prognosis::createFromXml($xml->StopPrognosis, $dateTime, $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;
         }
     }
     return $obj;
 }
Ejemplo n.º 2
0
 public function testCreateFromXml_Unknown()
 {
     $xml = new \SimpleXMLElement('<YouDontKnowMe />');
     $this->assertNull(LocationFactory::createFromXml($xml));
 }
Ejemplo n.º 3
0
 /**
  * @return array
  */
 public function findLocations(LocationQuery $query)
 {
     // send request
     $response = $this->sendQuery($query);
     // parse result
     $result = simplexml_load_string($response->getContent());
     $locations = array();
     $viaCount = 0;
     foreach ($result->LocValRes as $part) {
         $id = (string) $part['id'];
         // A "via" can occur 0-5 times
         if ($id == "via") {
             $id = $id . ++$viaCount;
         }
         $locations[$id] = array();
         foreach ($part->children() as $location) {
             $location = Entity\LocationFactory::createFromXml($location);
             if ($location) {
                 $locations[$id][] = $location;
             }
         }
     }
     if (count($locations) > 1) {
         return $locations;
     }
     return reset($locations);
 }
Ejemplo n.º 4
0
 /**
  * @return array
  */
 public function findLocations(LocationQuery $query)
 {
     // send request
     $result = $this->sendAndParseQuery($query);
     $locations = [];
     $viaCount = 0;
     foreach ($result->LocValRes as $part) {
         $id = (string) $part['id'];
         // A "via" can occur 0-5 times
         if ($id == 'via') {
             $id = $id . ++$viaCount;
         }
         $locations[$id] = [];
         foreach ($part->children() as $location) {
             $location = Entity\LocationFactory::createFromXml($location);
             if ($location) {
                 $locations[$id][] = $location;
             }
         }
     }
     if (count($locations) > 1) {
         return $locations;
     }
     return reset($locations);
 }