public function parseXml(\DOMElement $root, $options = array())
{
$schema = $this->schema;
if ($root->hasAttribute('updateOnly')) {
$update_only = $root->hasAttribute('updateOnly');
if (in_array(strtolower($update_only), array('1', 'true'))) {
$this->update_only = true;
}
}
foreach ($root->childNodes as $child) {
if (!$child instanceof \DOMElement) {
continue;
}
$local_name = preg_replace('/^.*:/', '', $child->tagName);
if (!isset($schema[$local_name])) {
throw new \Exception("Unrecognised tag {$local_name}");
}
switch ($schema[$local_name]['type']) {
case 'list':
$this->{$local_name} = array();
foreach ($child->childNodes as $list_item) {
if (!$list_item instanceof \DOMElement) {
continue;
}
$cls = __NAMESPACE__ . '\\' . $schema[$local_name]['resource'];
$this->{$local_name}[] = $cls::fromXmlDom($this->version, $list_item, $options);
}
break;
case 'date':
// TODO: Move parsing into specific object types to after validation. Validate first to check eligible?
if (!strlen($child->textContent)) {
break;
}
if ($date = \DateTime::createFromFormat('Y-m-d', $child->textContent)) {
$this->{$local_name} = $date->format('Y-m-d H:i:s');
} else {
throw new \Exception("invalid date format for {$local_name}");
}
break;
case 'time':
if (!strlen($child->textContent)) {
break;
}
if (preg_match('/^\\d\\d:\\d\\d$/', $child->textContent)) {
$this->{$local_name} = $child->textContent;
} else {
throw new \Exception("invalid time format for {$local_name}");
}
break;
case 'resource':
$cls = __NAMESPACE__ . '\\' . $schema[$local_name]['resource'];
$this->{$local_name} = $cls::fromXmlDom($this->version, $child, $options);
break;
case 'boolean':
$this->{$local_name} = (bool) $child->textContent;
break;
default:
$this->{$local_name} = $child->textContent;
}
}
}