/**
* Merges new elements into a source `<fields>` element.
*
* @param \SimpleXMLElement $source The source element.
* @param \SimpleXMLElement $new The new element to merge.
*
* @return void
*
* @since 1.7.0
*/
protected static function mergeNodes(\SimpleXMLElement $source, \SimpleXMLElement $new)
{
// The assumption is that the inputs are at the same relative level.
// So we just have to scan the children and deal with them.
// Update the attributes of the child node.
foreach ($new->attributes() as $name => $value) {
if (isset($source[$name])) {
$source[$name] = (string) $value;
} else {
$source->addAttribute($name, $value);
}
}
foreach ($new->children() as $child) {
$type = $child->getName();
$name = $child['name'];
// Does this node exist?
$fields = $source->xpath($type . '[@name="' . $name . '"]');
if (empty($fields)) {
// This node does not exist, so add it.
self::addNode($source, $child);
} else {
// This node does exist.
switch ($type) {
case 'field':
self::mergeNode($fields[0], $child);
break;
default:
self::mergeNodes($fields[0], $child);
break;
}
}
}
}