Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class xmlCreator {
- var $instance;
- var $curPath = array();
- var $xmlNode;
- var $rootNode;
- function __constructor ($xmlContent = null) {
- $this->__instance($xmlContent);
- }
- function __instance($content=null, $rootElement=null) {
- if ($content == null) {
- $content="<".$rootElement."></".$rootElement.">";
- $this->rootNode = $rootElement;
- }
- $this->instance = new SimpleXMLElement($content);
- }
- function __set($element,$value) {
- $this->__call($element, $args)->value($value);
- }
- function __get($element) {
- if (isset($this->xmlNode[$element])) {
- if ($element == $this->rootNode) {
- $this->curPath = array();
- } else {
- $this->curPath[] = $element;
- }
- }
- return $this;
- }
- function __call($element, $args) {
- if (is_null($this->instance)) {
- $this->__instance(null, $element);
- }
- $count = count($this->xmlNode);
- $this->xmlNode[$count]['name'] = $element;
- $this->xmlNode[$count]['path'] = $this->curPath;
- if (isset($args[0])) {
- if (isset($args[1])) { $this->xmlNode[$count]['callback'] = $args[1]; }
- $this->xmlNode[$count]['param'] = $args[0];
- }
- return $this;
- }
- function _value($value) {
- $this->xmlNode[end($this->curPath)]['value'] = $value;
- return $this;
- }
- function __toString() {
- $callback_node = array();
- $n = 0;
- foreach ($this->xmlNode as $num=>$params) {
- if ($params['name'] != $this->rootNode) {
- $path = $this->instance;
- if (count($params['path']) > 0) {
- $path = $path->{implode("->", $params['path'])};
- }
- if (isset($params['value'])) {
- $element = $path->addChild($params['name'], $params['value']);
- } else {
- $element = $path->addChild($params['name']);
- }
- } else {
- $element = $this->instance;
- }
- if (isset($params['param'])) {
- foreach ($params['param'] as $nameParam=>$valueParam) {
- if (!isset($params['callback']) || (isset($params['callback']) && !in_array($nameParam, $params['callback']))) {
- $element->addAttribute($nameParam, $valueParam);
- } else {
- $n++;
- $callback_node[$n]['element'] = $element;
- $callback_node[$n]['param'] = $nameParam;
- $callback_node[$n]['func'] = $valueParam;
- }
- }
- }
- }
- foreach ($callback_node as $callback) {
- $callback['element']->addAttribute($callback['param'], $callback['func']($callback['element']->packed->asXML()));
- }
- return $this->instance->asXml();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement