소스 검색

added Core_XmlWriter

Piotr Labudda 9 년 전
부모
커밋
dca0acc78d
1개의 변경된 파일132개의 추가작업 그리고 0개의 파일을 삭제
  1. 132 0
      SE/se-lib/Core/XmlWriter.php

+ 132 - 0
SE/se-lib/Core/XmlWriter.php

@@ -0,0 +1,132 @@
+<?php
+
+class Core_XmlWriter extends XMLWriter {
+
+  /**
+   * @param mixed $arg1
+   * @param mixed $arg2
+   * @param mixed $arg3
+   *
+   * @example h('tag')       => <tag/>
+   * @example h('tag', null) => <tag/>
+   * @example h('tag', '')   => <tag></tag>
+   * @example h('tag', ['id', 'ID'], null) => <tag id="ID"/>
+   * @example h('tag', ['id', 'ID'], '')   => <tag id="ID"></tag>
+   * @example h('tag', ['tag2', null])     => <tag><tag2/></tag>
+   * @example h('tag', ['id' => 'ID'], 'tag2')         => <tag id="ID"><tag2/></tag>
+   * @example h('tag', ['id' => 'ID'], ['tag2', null]) => <tag id="ID"><tag2/></tag>
+   */
+  public function h($arg1, $arg2 = false, $arg3 = false) {
+    DBG::log(['arg1'=>$arg1, 'arg2'=>$arg2, 'arg3'=>$arg3], '', "XMLWriter::h(...)");
+    $tag = null;
+    $attrs = [];
+    $childrens = [];// [] or null or ''
+    $isEmpty = false;
+    $text = null;
+    if (null === $arg1 && !empty($arg2)) { $text = $arg2; } // h( null, text )
+    else if (is_string($arg1) && false === $arg2 && false === $arg3) { $tag = $arg1; $isEmpty = true; } // h( tag )
+    else if (is_string($arg1) && null === $arg2 && false === $arg3) { $tag = $arg1; $isEmpty = true; } // h( tag, null )
+    else if (is_string($arg1) && '' === $arg2 && false === $arg3) { $tag = $arg1; $text = ''; } // h( tag, null )
+    else if (is_string($arg1) && is_string($arg2) && false === $arg3) { $tag = $arg1; $text = $arg2; } // h( tag, text )
+    else if (is_string($arg1) && is_array($arg2) && false === $arg3) { $tag = $arg1; $childrens = $arg2; } // h( tag, childrens )
+    else if (is_string($arg1) && is_array($arg2) && null === $arg3) { $tag = $arg1; $attrs = $arg2; $isEmpty = true; } // h( tag, attrs, null )
+    else if (is_string($arg1) && is_array($arg2) && '' === $arg3) { $tag = $arg1; $attrs = $arg2; $text = ''; } // h( tag, attrs, '' )
+    else if (is_string($arg1) && is_array($arg2) && is_string($arg3)) { $tag = $arg1; $attrs = $arg2; $text = $arg3; } // h( tag, attrs, '' )
+    else if (is_string($arg1) && is_array($arg2) && is_array($arg3)) { $tag = $arg1; $attrs = $arg2; $childrens = $arg3; } // h( tag, attrs, childrens )
+    else if (is_array($arg1) && false === $arg2 && false === $arg3) {
+      if (1 == count($arg1)) $this->h($arg1[0]);
+      else if (2 == count($arg1)) $this->h($arg1[0], $arg1[1]);
+      else if (3 == count($arg1)) $this->h($arg1[0], $arg1[1], $arg1[2]);
+      else throw new Exception("XmlWriter Sytnax Error #h01");
+      return;
+    }
+
+    if (!$tag) {
+      if (!empty($text)) $this->text($text);
+      else {
+        echo "tag({$tag}) text({$text}) args({$arg1}, {$arg2}, {$arg3})"."\n";
+        throw new Exception("XmlWriter Sytnax Error #h02");
+      }
+    }
+
+    if ($isEmpty) {
+      if (empty($attrs)) {
+        $this->writeElement($tag, null);
+      } else {
+        $this->startElement($tag);
+        foreach ($attrs as $k => $v) {
+          $this->writeAttribute($k, $v);
+        }
+        $this->endElement();
+      }
+      return;
+    }
+
+    // $tag, !$isEmpty, ...
+    $this->startElement($tag);
+    foreach ($attrs as $k => $v) $this->writeAttribute($k, $v);
+    if (!empty($childrens)) {
+      foreach ($childrens as $child) {
+        $this->h($child);
+      }
+    } else if (null !== $text) {
+      $this->text($text);
+    }
+    $this->endElement();
+    // echo "tag(".var_export($tag,true).") text(".var_export($text,true).") attrs(".var_export($attrs,true).") args({$arg1}, {$arg2}, {$arg3})"."\n";
+
+    return;
+
+    // if (is_string($arg1) && !empty($arg2) && empty($arg3) && '' === $arg3) $args = [$arg1, $arg2, $arg3];
+    // else if (is_string($arg1) && !empty($arg2) && empty($arg3) && '' === $arg3) $args = [$arg1, $arg2, $arg3];
+    // else if (is_string($arg1) && !empty($arg2) && empty($arg3)) $args = [$arg1, $arg2];
+    // else if (is_string($arg1) && !empty($arg2) && !empty($arg3)) $args = [$arg1, $arg2, $arg3];
+    // else if (is_string($arg1) && empty($arg2) && empty($arg3) && '' === $arg2) $args = [$arg1, $arg2];
+    // else $args = $arg1;
+
+    // echo "DBG::h(".json_encode($args).")" . "\n";
+    if (is_string($args)) {
+      $this->writeElement($args, null);
+    } else if (is_array($args)) {
+      if (1 == count($args)) {// [ tagName ]
+        // echo "DBG::h cnt=1 [".json_encode($args)."]" . "\n";
+        $this->writeElement($args, null);
+      } else if (2 == count($args)) {// [ tagName, $childrens ]
+        list($tag, $childrens) = $args;
+        if (null === $tag && is_string($childrens)) $this->writeText($childrens);
+        else if (!empty($tag) && null === $childrens) $this->writeElement($tag, null);
+        else if (!empty($tag) && '' === $childrens) $this->writeElement($tag, '');
+        else if (!empty($tag) && is_string($childrens)) $this->writeElement($tag, $childrens);
+        else if (!empty($tag) && is_array($childrens)) {
+          $this->startElement($tag);
+          foreach ($childrens as $child) {
+            $this->h($child);
+          }
+          $this->endElement();
+        }
+        else throw new Exception("TODO: syntax error args: " . json_encode($args));
+      } else if (3 == count($args)) {// [ tagName, $attrs, $childrens ]
+        list($tag, $attrs, $childrens) = $args;
+        $this->startElement($tag);
+        foreach ($attrs as $k => $v) {
+          $this->writeAttribute($k, $v);
+        }
+        if (null === $childrens) ;
+        else if ('' === $childrens) $this->text('');
+        else if (is_string($childrens)) $this->text($childrens);
+        else if (is_array($childrens)) {
+          foreach ($childrens as $child) {
+            $this->h($child);
+          }
+        }
+        else throw new Exception("TODO: count=3 args: " . json_encode($args));
+        $this->endElement();
+      } else {
+        throw new Exception("TODO: !count args: " . json_encode($args));
+      }
+    } else {
+      throw new Exception("TODO: !\$args args");
+    }
+  }
+
+}