XmlWriter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. class Core_XmlWriter extends XMLWriter {
  3. /**
  4. * @param mixed $arg1
  5. * @param mixed $arg2
  6. * @param mixed $arg3
  7. *
  8. * @example h('tag') => <tag/>
  9. * @example h('tag', null) => <tag/>
  10. * @example h('tag', '') => <tag></tag>
  11. * @example h('tag', ['id', 'ID'], null) => <tag id="ID"/>
  12. * @example h('tag', ['id', 'ID'], '') => <tag id="ID"></tag>
  13. * @example h('tag', ['tag2', null]) => <tag><tag2/></tag>
  14. * @example h('tag', ['id' => 'ID'], 'tag2') => <tag id="ID"><tag2/></tag>
  15. * @example h('tag', ['id' => 'ID'], ['tag2', null]) => <tag id="ID"><tag2/></tag>
  16. */
  17. public function h($arg1, $arg2 = false, $arg3 = false) {
  18. // DBG::log(['arg1'=>$arg1, 'arg2'=>$arg2, 'arg3'=>$arg3], '', "XMLWriter::h(...)");
  19. $tag = null;
  20. $attrs = [];
  21. $childrens = [];// [] or null or ''
  22. $isEmpty = false;
  23. $text = null;
  24. if (null === $arg1 && !empty($arg2)) { $text = $arg2; } // h( null, text )
  25. else if (is_string($arg1) && false === $arg2 && false === $arg3) { $tag = $arg1; $isEmpty = true; } // h( tag )
  26. else if (is_string($arg1) && null === $arg2 && false === $arg3) { $tag = $arg1; $isEmpty = true; } // h( tag, null )
  27. else if (is_string($arg1) && '' === $arg2 && false === $arg3) { $tag = $arg1; $text = ''; } // h( tag, null )
  28. else if (is_string($arg1) && is_string($arg2) && false === $arg3) { $tag = $arg1; $text = $arg2; } // h( tag, text )
  29. else if (is_string($arg1) && is_array($arg2) && false === $arg3) { $tag = $arg1; $childrens = $arg2; } // h( tag, childrens )
  30. else if (is_string($arg1) && is_array($arg2) && null === $arg3) { $tag = $arg1; $attrs = $arg2; $isEmpty = true; } // h( tag, attrs, null )
  31. else if (is_string($arg1) && is_array($arg2) && '' === $arg3) { $tag = $arg1; $attrs = $arg2; $text = ''; } // h( tag, attrs, '' )
  32. else if (is_string($arg1) && is_array($arg2) && is_string($arg3)) { $tag = $arg1; $attrs = $arg2; $text = $arg3; } // h( tag, attrs, '' )
  33. else if (is_string($arg1) && is_array($arg2) && is_array($arg3)) { $tag = $arg1; $attrs = $arg2; $childrens = $arg3; } // h( tag, attrs, childrens )
  34. else if (is_array($arg1) && false === $arg2 && false === $arg3) {
  35. if (1 == count($arg1)) $this->h($arg1[0]);
  36. else if (2 == count($arg1)) $this->h($arg1[0], $arg1[1]);
  37. else if (3 == count($arg1)) $this->h($arg1[0], $arg1[1], $arg1[2]);
  38. else throw new Exception("XmlWriter Sytnax Error #h01");
  39. return;
  40. }
  41. if (!$tag) {
  42. if (!empty($text)) $this->text($text);
  43. else {
  44. DBG::log("tag({$tag}) text({$text}) args({$arg1}, {$arg2}, {$arg3})", 'string', "XmlWriter Sytnax Error #h02");
  45. throw new Exception("XmlWriter Sytnax Error #h02");
  46. }
  47. }
  48. if ($isEmpty) {
  49. if (empty($attrs)) {
  50. $this->writeElement($tag, null);
  51. } else {
  52. $this->startElement($tag);
  53. foreach ($attrs as $k => $v) {
  54. $this->writeAttribute($k, $v);
  55. }
  56. $this->endElement();
  57. }
  58. return;
  59. }
  60. // $tag, !$isEmpty, ...
  61. $this->startElement($tag);
  62. foreach ($attrs as $k => $v) $this->writeAttribute($k, $v);
  63. if (!empty($childrens)) {
  64. foreach ($childrens as $child) {
  65. $this->h($child);
  66. }
  67. } else if (null !== $text) {
  68. $this->text($text);
  69. }
  70. $this->endElement();
  71. // echo "tag(".var_export($tag,true).") text(".var_export($text,true).") attrs(".var_export($attrs,true).") args({$arg1}, {$arg2}, {$arg3})"."\n";
  72. return;
  73. // if (is_string($arg1) && !empty($arg2) && empty($arg3) && '' === $arg3) $args = [$arg1, $arg2, $arg3];
  74. // else if (is_string($arg1) && !empty($arg2) && empty($arg3) && '' === $arg3) $args = [$arg1, $arg2, $arg3];
  75. // else if (is_string($arg1) && !empty($arg2) && empty($arg3)) $args = [$arg1, $arg2];
  76. // else if (is_string($arg1) && !empty($arg2) && !empty($arg3)) $args = [$arg1, $arg2, $arg3];
  77. // else if (is_string($arg1) && empty($arg2) && empty($arg3) && '' === $arg2) $args = [$arg1, $arg2];
  78. // else $args = $arg1;
  79. // echo "DBG::h(".json_encode($args).")" . "\n";
  80. if (is_string($args)) {
  81. $this->writeElement($args, null);
  82. } else if (is_array($args)) {
  83. if (1 == count($args)) {// [ tagName ]
  84. // echo "DBG::h cnt=1 [".json_encode($args)."]" . "\n";
  85. $this->writeElement($args, null);
  86. } else if (2 == count($args)) {// [ tagName, $childrens ]
  87. list($tag, $childrens) = $args;
  88. if (null === $tag && is_string($childrens)) $this->writeText($childrens);
  89. else if (!empty($tag) && null === $childrens) $this->writeElement($tag, null);
  90. else if (!empty($tag) && '' === $childrens) $this->writeElement($tag, '');
  91. else if (!empty($tag) && is_string($childrens)) $this->writeElement($tag, $childrens);
  92. else if (!empty($tag) && is_array($childrens)) {
  93. $this->startElement($tag);
  94. foreach ($childrens as $child) {
  95. $this->h($child);
  96. }
  97. $this->endElement();
  98. }
  99. else throw new Exception("TODO: syntax error args: " . json_encode($args));
  100. } else if (3 == count($args)) {// [ tagName, $attrs, $childrens ]
  101. list($tag, $attrs, $childrens) = $args;
  102. $this->startElement($tag);
  103. foreach ($attrs as $k => $v) {
  104. $this->writeAttribute($k, $v);
  105. }
  106. if (null === $childrens) ;
  107. else if ('' === $childrens) $this->text('');
  108. else if (is_string($childrens)) $this->text($childrens);
  109. else if (is_array($childrens)) {
  110. foreach ($childrens as $child) {
  111. $this->h($child);
  112. }
  113. }
  114. else throw new Exception("TODO: count=3 args: " . json_encode($args));
  115. $this->endElement();
  116. } else {
  117. throw new Exception("TODO: !count args: " . json_encode($args));
  118. }
  119. } else {
  120. throw new Exception("TODO: !\$args args");
  121. }
  122. }
  123. }