| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?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");
- }
- }
- }
|