Extended SimpleXMLElement:
<?php
class XmlElement extends \SimpleXMLElement
{
public static function factory(string $root): self
{
return new static('<?xml version="1.0" encoding="UTF-8"?><'.$root.'/>', LIBXML_BIGLINES | LIBXML_COMPACT);
}
public function addAttributes(iterable $attributes)
{
foreach ($attributes as $name => $value) {
$this->addAttribute($name, $value);
}
return $this;
}
public function addChild($name, $valueOrAttributes = null, $namespace = null)
{
if (is_array($valueOrAttributes)) {
$child = parent::addChild($name, null, $namespace);
foreach ($valueOrAttributes as $name => $value) {
$child->addAttribute($name, $value);
}
} else {
$child = parent::addChild($name, $valueOrAttributes, $namespace);
}
return $child;
}
public function addChilds(iterable $childs)
{
foreach ($childs as $name => $value) {
$this->addChild($name, $value);
}
return $this;
}
}
?>