]> andersk Git - sql-web.git/blob - lib/joe/container.lib.php
some XHTML/XML wrapper classes
[sql-web.git] / lib / joe / container.lib.php
1 <?php
2 function __autoload($class_name) {
3             require_once strtolower($class_name).'.lib.php';
4 }
5
6 abstract class Container {
7         var $sID;
8         var $aAttributes;
9         var $aContents;
10         function __construct($id=null,$attributes=array(),$contents=array()) {
11                 $this->sID = $id;
12                 if (is_array($attributes)) {
13                         $this->aAttributes = $attributes;
14                 }
15                 if (is_array($contents))
16                         $this->aContents = $contents;
17                 else
18                         $this->add($contents);
19         }
20     function __get($nm) {
21         if (isset($this->aAttributes[$nm])) {
22             $r = $this->aAttributes[$nm];
23             return $r;
24         }
25     }
26     function __set($nm, $val) {
27         //      if (isset($this->aAttributes[$nm])) {
28                         $this->aAttributes[$nm] = $val;
29         //      }
30     }
31     function __isset($nm) {
32         return isset($this->aAttributes[$nm]);
33     }
34     function __unset($nm) {
35         unset($this->aAttributes[$nm]);
36     }
37
38         function add($obj) { $this->aContents[] = $obj; }
39         function set($obj) { $this->aContents = array($obj); }
40         function get_id() { return $this->sID; }
41         function has_id() { return !is_null($this->sID); }
42         function has_attributes() { return count($this->aAttributes); }
43         function get_attributes() { return $this->aAttributes; }
44         function get_contents() { return $this->aContents; }
45
46         protected function paint_head() {
47                 if ($this->has_id()) {
48                         if ($this->has_attributes()) {
49                                 return sprintf("<%s %s>", strtolower($this->get_id()), $this->paint_attributes());
50                         } else {
51                                 return sprintf("<%s>", strtolower($this->get_id()));
52                         }
53                 }
54         }
55         protected function paint_attributes() {
56                 $r = array();
57                 foreach($this->get_attributes() as $k=>$v) {
58                         $k = strtolower($k);
59                         if (is_string($v)) {
60                                 $r[] = "$k=\"$v\"";
61                         } elseif (is_object($v)) {
62                                 $r[] = "$k=\"".$v->__toString()."\"";
63                         }
64                 }
65                 return implode(' ',$r);
66         }
67         protected function paint_foot() {
68                 if ($this->has_id())
69                         return sprintf("</%s>", strtolower($this->get_id()));
70         }
71         protected function paint_contents($split='') {
72                 $r = array();
73                 foreach($this->get_contents() as $item) {
74                         if (is_string($item) && is_callable($item)) {
75                                 $args = func_get_args();
76                                 $r[] = ''.call_user_func($item,$args);
77                         } elseif (is_object($item)) {
78                                 $r[] = ''.$item->__toString();
79                         } elseif (is_string($item)) {
80                                 $r[] = ''.$item;
81                         }
82                 }
83                 return implode($split,$r);
84         }
85         function __toString() {
86                 return $this->paint_head().$this->paint_contents().$this->paint_foot();
87         }
88 }
89
90 class Entity extends Container {}
This page took 0.039729 seconds and 5 git commands to generate.