/** * initialize the rowset * @param SimpleXMLElement $xml * @optional String $name * @optional String $rowname */ public function __construct($xml, $name = null, $rowname = 'row') { $this->_name = (string) ($name !== null) ? $name : $xml->attributes()->name; foreach ($xml->{$rowname} as $rowxml) { $row = array(); foreach ($rowxml->attributes() as $attkey => $attval) { $row[$attkey] = (string) $attval; } foreach ($rowxml->children() as $child) { $element = PhealElement::parse_element($child); $row[(string) $element->_name] = $element; } $rowObject = new PhealRowSetRow($row); $rowObject->setStringValue((string) $rowxml); $this->append($rowObject); } }
/** * initializes the PhealResult * @param SimpleXMLElement $xml */ public function __construct($xml) { // switch to UTC $oldtz = date_default_timezone_get(); date_default_timezone_set('UTC'); $this->request_time = (string) $xml->currentTime; $this->cached_until = (string) $xml->cachedUntil; $this->request_time_unixtime = (int) strtotime($xml->currentTime); $this->cached_until_unixtime = (int) strtotime($xml->cachedUntil); // workaround if cachedUntil is missing in API response (request + 1 hour) if (!$this->cached_until) { $this->cached_until_unixtime = $this->request_time_unixtime + 60 * 60; $this->cached_until = date('Y-m-d H:i:s', $this->cached_until_unixtime); } // switch back to normal time date_default_timezone_set($oldtz); // error detection if ($xml->error) { throw new PhealAPIException($xml->error["code"], (string) $xml->error, $xml); } $this->_element = PhealElement::parse_element($xml->result); }
/** * parse SimpleXMLElement * @param SimpleXMLElement $element * @return mixed */ public static function parse_element($element) { if ($element->getName() == "rowset") { $re = new PhealRowSet($element); } elseif ($element->getName() == "result" && $element->member) { $container = new PhealContainer(); $container->add_element('members', new PhealRowSet($element, 'members', 'member')); $re = new PhealElement('result', $container); } else { $key = $element->getName(); $echilds = $element->children(); if (count($echilds) > 0) { $container = new PhealContainer(); foreach ($echilds as $celement) { $cel = PhealElement::parse_element($celement); if (count($celement->attributes()) > 0) { $container->add_element($cel->_name, $cel); } else { $container->add_element($cel->_name, $cel->_value); } } $value = $container; } else { $value = (string) $element; } $re = new PhealElement($key, $value); if (count($element->attributes()) > 0) { foreach ($element->attributes() as $attelem) { $re->add_attrib($attelem->getName(), (string) $attelem); } } } return $re; }