Created
August 26, 2015 16:50
-
-
Save ziadoz/80f520829eb8bfc1548b to your computer and use it in GitHub Desktop.
Simple XML Element Builder (Proxies to SimpleXMLElement class)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \SimpleXMLElement; | |
use \BadMethodCallException; | |
class XmlElement | |
{ | |
/** | |
* The default XML DOCTYPE. | |
* | |
* @var string | |
*/ | |
const DOCTYPE = '<?xml version="1.0" encoding="UTF-8"?>'; | |
/** | |
* The internal SimpleXMLElement. | |
* | |
* @var SimpleXMLElement | |
*/ | |
private $element; | |
/** | |
* Create a new element. | |
* | |
* @param $name | |
*/ | |
public function __construct($name) | |
{ | |
$xml = static::DOCTYPE; | |
$xml .= '<' . $name . '></' . $name . '>'; | |
$this->element = new SimpleXMLElement($xml); | |
} | |
/* | |
* Proxy methods to the underlying SimpleXMLElement. | |
* | |
* @param string $method | |
* @param mixed $args | |
* @return mixed | |
* @throw \BadMethodCallException | |
*/ | |
public function __call($method, $args) | |
{ | |
if (method_exists($this->element, $method)) { | |
return call_user_func_array([$this->element, $method], $args); | |
} | |
throw new BadMethodCallException(sprintf('The method "%s" does not exist', $method)); | |
} | |
/** | |
* Return the XML, but strip out the DOCTYPE first. | |
* | |
* @param null $filename | |
* @return mixed | |
*/ | |
public function asXML($filename = null) | |
{ | |
$xml = $this->element->asXML(); | |
if (! $xml) { | |
return false; | |
} | |
$xml = trim(str_replace(static::DOCTYPE, '', $xml)); | |
if (! $filename) { | |
return $xml; | |
} | |
return file_put_contents($filename, $xml) > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment