'); $this->toXml($dom, $context); $this->data = $dom->saveXML(); } return $this; } /** * @return string|null */ public function getData(): ?string { $data = $this->data; $this->clear(); return $data; } /** * @param SimpleXMLElement $dom * @param $data */ public function toXml(SimpleXMLElement $dom, $data) { foreach ($data as $key => $val) { if (is_numeric($key)) { $key = 'item' . $key; } if (is_array($val)) { $node = $dom->addChild($key); $this->toXml($node, $val); } else if (is_object($val)) { $val = get_object_vars($val); $node = $dom->addChild($key); $this->toXml($node, $val); } else { $dom->addChild($key, htmlspecialchars((string)$val)); } } } public function clear(): void { $this->data = null; unset($this->data); } }