]> andersk Git - sql-web.git/commitdiff
some XHTML/XML wrapper classes
authorJoe Presbrey <presbrey@mit.edu>
Sun, 12 Nov 2006 20:50:41 +0000 (20:50 +0000)
committerJoe Presbrey <presbrey@mit.edu>
Sun, 12 Nov 2006 20:50:41 +0000 (20:50 +0000)
git-svn-id: svn://presbrey.mit.edu/php/lib@118 a142d4bd-2cfb-0310-9673-cb33a7e74f58

lib/joe/container.lib.php [new file with mode: 0755]
lib/joe/css.lib.php [new file with mode: 0755]
lib/joe/html.lib.php [new file with mode: 0755]
lib/joe/rpcreply.lib.php [new file with mode: 0755]
lib/joe/xml.lib.php [new file with mode: 0755]

diff --git a/lib/joe/container.lib.php b/lib/joe/container.lib.php
new file mode 100755 (executable)
index 0000000..5df45e2
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+function __autoload($class_name) {
+           require_once strtolower($class_name).'.lib.php';
+}
+
+abstract class Container {
+       var $sID;
+       var $aAttributes;
+       var $aContents;
+       function __construct($id=null,$attributes=array(),$contents=array()) {
+               $this->sID = $id;
+               if (is_array($attributes)) {
+                       $this->aAttributes = $attributes;
+               }
+               if (is_array($contents))
+                       $this->aContents = $contents;
+               else
+                       $this->add($contents);
+       }
+    function __get($nm) {
+        if (isset($this->aAttributes[$nm])) {
+            $r = $this->aAttributes[$nm];
+            return $r;
+        }
+    }
+    function __set($nm, $val) {
+       //      if (isset($this->aAttributes[$nm])) {
+                       $this->aAttributes[$nm] = $val;
+       //      }
+    }
+    function __isset($nm) {
+        return isset($this->aAttributes[$nm]);
+    }
+    function __unset($nm) {
+        unset($this->aAttributes[$nm]);
+    }
+
+       function add($obj) { $this->aContents[] = $obj; }
+       function set($obj) { $this->aContents = array($obj); }
+       function get_id() { return $this->sID; }
+       function has_id() { return !is_null($this->sID); }
+       function has_attributes() { return count($this->aAttributes); }
+       function get_attributes() { return $this->aAttributes; }
+       function get_contents() { return $this->aContents; }
+
+       protected function paint_head() {
+               if ($this->has_id()) {
+                       if ($this->has_attributes()) {
+                               return sprintf("<%s %s>", strtolower($this->get_id()), $this->paint_attributes());
+                       } else {
+                               return sprintf("<%s>", strtolower($this->get_id()));
+                       }
+               }
+       }
+       protected function paint_attributes() {
+               $r = array();
+               foreach($this->get_attributes() as $k=>$v) {
+                       $k = strtolower($k);
+                       if (is_string($v)) {
+                               $r[] = "$k=\"$v\"";
+                       } elseif (is_object($v)) {
+                               $r[] = "$k=\"".$v->__toString()."\"";
+                       }
+               }
+               return implode(' ',$r);
+       }
+       protected function paint_foot() {
+               if ($this->has_id())
+                       return sprintf("</%s>", strtolower($this->get_id()));
+       }
+       protected function paint_contents($split='') {
+               $r = array();
+               foreach($this->get_contents() as $item) {
+                       if (is_string($item) && is_callable($item)) {
+                               $args = func_get_args();
+                               $r[] = ''.call_user_func($item,$args);
+                       } elseif (is_object($item)) {
+                               $r[] = ''.$item->__toString();
+                       } elseif (is_string($item)) {
+                               $r[] = ''.$item;
+                       }
+               }
+               return implode($split,$r);
+       }
+       function __toString() {
+               return $this->paint_head().$this->paint_contents().$this->paint_foot();
+       }
+}
+
+class Entity extends Container {}
diff --git a/lib/joe/css.lib.php b/lib/joe/css.lib.php
new file mode 100755 (executable)
index 0000000..e7d1b70
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+require_once 'container.lib.php';
+
+class CSS extends Container {
+       protected function paint_head() {
+               if ($this->has_id())
+                       return sprintf("%s {", $this->get_id());
+       }
+       protected function paint_attributes() {
+               $r = array();
+               foreach($this->get_attributes() as $k=>$v) {
+                       //$k = strtolower($k);
+                       $r[] = "$k: $v;";
+               }
+               return implode('',$r);
+       }
+       protected function paint_foot() {
+               if ($this->has_id())
+                       return sprintf("};");
+       }
+       function __toString() {
+               return $this->paint_head().$this->paint_attributes().$this->paint_foot();
+       }
+}
diff --git a/lib/joe/html.lib.php b/lib/joe/html.lib.php
new file mode 100755 (executable)
index 0000000..fed63e1
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+require_once 'container.lib.php';
+
+class HTML extends Container {}
diff --git a/lib/joe/rpcreply.lib.php b/lib/joe/rpcreply.lib.php
new file mode 100755 (executable)
index 0000000..7a151b9
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+require_once 'xml.lib.php';
+
+class RPCReply extends XML {
+       function __construct() {
+               parent::__construct('response');
+       }
+       function add_eval($data) {
+               parent::add(new CDATA('eval',array(),$data));
+       }
+       function add_update($id,$data) {
+               parent::add(new CDATA('update',array('id'=>$id),$data));
+       }
+}
+?>
diff --git a/lib/joe/xml.lib.php b/lib/joe/xml.lib.php
new file mode 100755 (executable)
index 0000000..0c1cd2c
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+require_once 'container.lib.php';
+
+class HTML extends Container {}
+class XHTML extends Container {}
+class XML extends Container {
+       function __toString() {
+               return "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n".parent::__toString();
+       }
+}
+class CDATA extends Container {
+       protected function paint_contents() {
+               $r1 = "\n<![CDATA[";
+               $r2 = "]]>\n";
+               return $r1.parent::paint_contents().$r2;
+       }
+       function __toString() {
+               return $this->paint_head().$this->paint_contents().$this->paint_foot();
+       }
+}
This page took 0.067346 seconds and 5 git commands to generate.