1: <?php
2: /**
3: * Open Data Definition (ODD) superclass.
4: *
5: * @package Elgg.Core
6: * @subpackage ODD
7: */
8: abstract class ODD {
9: /**
10: * Attributes.
11: */
12: private $attributes = array();
13:
14: /**
15: * Optional body.
16: */
17: private $body;
18:
19: /**
20: * Construct an ODD document with initial values.
21: */
22: public function __construct() {
23: $this->body = "";
24: }
25:
26: /**
27: * Returns an array of attributes
28: *
29: * @return array
30: */
31: public function getAttributes() {
32: return $this->attributes;
33: }
34:
35: /**
36: * Sets an attribute
37: *
38: * @param string $key Name
39: * @param mixed $value Value
40: *
41: * @return void
42: */
43: public function setAttribute($key, $value) {
44: $this->attributes[$key] = $value;
45: }
46:
47: /**
48: * Returns an attribute
49: *
50: * @param string $key Name
51: *
52: * @return mixed
53: */
54: public function getAttribute($key) {
55: if (isset($this->attributes[$key])) {
56: return $this->attributes[$key];
57: }
58:
59: return NULL;
60: }
61:
62: /**
63: * Sets the body of the ODD.
64: *
65: * @param mixed $value Value
66: *
67: * @return void
68: */
69: public function setBody($value) {
70: $this->body = $value;
71: }
72:
73: /**
74: * Gets the body of the ODD.
75: *
76: * @return mixed
77: */
78: public function getBody() {
79: return $this->body;
80: }
81:
82: /**
83: * Set the published time.
84: *
85: * @param int $time Unix timestamp
86: *
87: * @return void
88: */
89: public function setPublished($time) {
90: $this->attributes['published'] = date("r", $time);
91: }
92:
93: /**
94: * Return the published time as a unix timestamp.
95: *
96: * @return int or false on failure.
97: */
98: public function getPublishedAsTime() {
99: return strtotime($this->attributes['published']);
100: }
101:
102: /**
103: * For serialisation, implement to return a string name of the tag eg "header" or "metadata".
104: *
105: * @return string
106: */
107: abstract protected function getTagName();
108:
109: /**
110: * Magic function to generate valid ODD XML for this item.
111: *
112: * @return string
113: */
114: public function __toString() {
115: // Construct attributes
116: $attr = "";
117: foreach ($this->attributes as $k => $v) {
118: $attr .= ($v != "") ? "$k=\"$v\" " : "";
119: }
120:
121: $body = $this->getBody();
122: $tag = $this->getTagName();
123:
124: $end = "/>";
125: if ($body != "") {
126: $end = "><![CDATA[$body]]></{$tag}>";
127: }
128:
129: return "<{$tag} $attr" . $end . "\n";
130: }
131: }
132: