1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
<?php
namespace Coast;
use SimpleXMLElement;
use Coast\File;
class Xml extends SimpleXMLElement
{
public function appendChild($child)
{
$node = dom_import_simplexml($this);
$node->appendChild($node->ownerDocument->importNode(dom_import_simplexml($child), true));
return $this;
}
public function addCData($value)
{
$node = dom_import_simplexml($this);
$node->appendChild($node->ownerDocument->createCDATASection($value));
return $this;
}
public function toArray()
{
$xml = simplexml_load_string($this->asXML(), 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode((array) $xml), true);
}
public function toString()
{
return $this->asXML();
}
public function __toString()
{
return $this->toString();
}
public function writeFile(File $file)
{
return $this->asXML((string) $file);
}
}