Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. class xmlCreator {
  2. var $instance;
  3. var $curPath = array();
  4. var $xmlNode;
  5. var $rootNode;
  6.  
  7. function __constructor ($xmlContent = null) {
  8. $this->__instance($xmlContent);
  9. }
  10.  
  11. function __instance($content=null, $rootElement=null) {
  12. if ($content == null) {
  13. $content="<".$rootElement."></".$rootElement.">";
  14. $this->rootNode = $rootElement;
  15. }
  16. $this->instance = new SimpleXMLElement($content);
  17. }
  18.  
  19. function __set($element,$value) {
  20. $this->__call($element, $args)->value($value);
  21. }
  22.  
  23. function __get($element) {
  24. if (isset($this->xmlNode[$element])) {
  25. if ($element == $this->rootNode) {
  26. $this->curPath = array();
  27. } else {
  28. $this->curPath[] = $element;
  29. }
  30. }
  31. return $this;
  32. }
  33.  
  34. function __call($element, $args) {
  35. if (is_null($this->instance)) {
  36. $this->__instance(null, $element);
  37. }
  38. $count = count($this->xmlNode);
  39. $this->xmlNode[$count]['name'] = $element;
  40. $this->xmlNode[$count]['path'] = $this->curPath;
  41.  
  42. if (isset($args[0])) {
  43. if (isset($args[1])) { $this->xmlNode[$count]['callback'] = $args[1]; }
  44. $this->xmlNode[$count]['param'] = $args[0];
  45. }
  46.  
  47. return $this;
  48. }
  49.  
  50. function _value($value) {
  51. $this->xmlNode[end($this->curPath)]['value'] = $value;
  52. return $this;
  53. }
  54.  
  55. function __toString() {
  56. $callback_node = array();
  57. $n = 0;
  58. foreach ($this->xmlNode as $num=>$params) {
  59. if ($params['name'] != $this->rootNode) {
  60. $path = $this->instance;
  61. if (count($params['path']) > 0) {
  62. $path = $path->{implode("->", $params['path'])};
  63. }
  64.  
  65. if (isset($params['value'])) {
  66. $element = $path->addChild($params['name'], $params['value']);
  67. } else {
  68. $element = $path->addChild($params['name']);
  69. }
  70. } else {
  71. $element = $this->instance;
  72. }
  73. if (isset($params['param'])) {
  74. foreach ($params['param'] as $nameParam=>$valueParam) {
  75. if (!isset($params['callback']) || (isset($params['callback']) && !in_array($nameParam, $params['callback']))) {
  76. $element->addAttribute($nameParam, $valueParam);
  77. } else {
  78. $n++;
  79. $callback_node[$n]['element'] = $element;
  80. $callback_node[$n]['param'] = $nameParam;
  81. $callback_node[$n]['func'] = $valueParam;
  82. }
  83. }
  84. }
  85. }
  86. foreach ($callback_node as $callback) {
  87. $callback['element']->addAttribute($callback['param'], $callback['func']($callback['element']->packed->asXML()));
  88. }
  89.  
  90. return $this->instance->asXml();
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement