2 function __autoload($class_name) {
3 require_once strtolower($class_name).'.lib.php';
6 abstract class Container {
10 function __construct($id=null,$attributes=array(),$contents=array()) {
12 if (is_array($attributes)) {
13 $this->aAttributes = $attributes;
15 if (is_array($contents))
16 $this->aContents = $contents;
18 $this->add($contents);
21 if (isset($this->aAttributes[$nm])) {
22 $r = $this->aAttributes[$nm];
26 function __set($nm, $val) {
27 // if (isset($this->aAttributes[$nm])) {
28 $this->aAttributes[$nm] = $val;
31 function __isset($nm) {
32 return isset($this->aAttributes[$nm]);
34 function __unset($nm) {
35 unset($this->aAttributes[$nm]);
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; }
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());
51 return sprintf("<%s>", strtolower($this->get_id()));
55 protected function paint_attributes() {
57 foreach($this->get_attributes() as $k=>$v) {
61 } elseif (is_object($v)) {
62 $r[] = "$k=\"".$v->__toString()."\"";
65 return implode(' ',$r);
67 protected function paint_foot() {
69 return sprintf("</%s>", strtolower($this->get_id()));
71 protected function paint_contents($split='') {
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)) {
83 return implode($split,$r);
85 function __toString() {
86 return $this->paint_head().$this->paint_contents().$this->paint_foot();
90 class Entity extends Container {}