From abc9b7a9999863b1d83e10e248337f70ec70dc7e Mon Sep 17 00:00:00 2001 From: Joe Presbrey Date: Sun, 12 Nov 2006 20:50:41 +0000 Subject: [PATCH] some XHTML/XML wrapper classes git-svn-id: svn://presbrey.mit.edu/php/lib@118 a142d4bd-2cfb-0310-9673-cb33a7e74f58 --- lib/joe/container.lib.php | 90 +++++++++++++++++++++++++++++++++++++++ lib/joe/css.lib.php | 24 +++++++++++ lib/joe/html.lib.php | 4 ++ lib/joe/rpcreply.lib.php | 15 +++++++ lib/joe/xml.lib.php | 20 +++++++++ 5 files changed, 153 insertions(+) create mode 100755 lib/joe/container.lib.php create mode 100755 lib/joe/css.lib.php create mode 100755 lib/joe/html.lib.php create mode 100755 lib/joe/rpcreply.lib.php create mode 100755 lib/joe/xml.lib.php diff --git a/lib/joe/container.lib.php b/lib/joe/container.lib.php new file mode 100755 index 0000000..5df45e2 --- /dev/null +++ b/lib/joe/container.lib.php @@ -0,0 +1,90 @@ +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("", 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 index 0000000..e7d1b70 --- /dev/null +++ b/lib/joe/css.lib.php @@ -0,0 +1,24 @@ +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 index 0000000..fed63e1 --- /dev/null +++ b/lib/joe/html.lib.php @@ -0,0 +1,4 @@ +$id),$data)); + } +} +?> diff --git a/lib/joe/xml.lib.php b/lib/joe/xml.lib.php new file mode 100755 index 0000000..0c1cd2c --- /dev/null +++ b/lib/joe/xml.lib.php @@ -0,0 +1,20 @@ +\n".parent::__toString(); + } +} +class CDATA extends Container { + protected function paint_contents() { + $r1 = "\n\n"; + return $r1.parent::paint_contents().$r2; + } + function __toString() { + return $this->paint_head().$this->paint_contents().$this->paint_foot(); + } +} -- 2.45.0