1: <?php
2: /**
3: * A generic class that contains shared code b/w
4: * ElggExtender, ElggEntity, and ElggRelationship
5: *
6: * @package Elgg.Core
7: * @subpackage DataModel
8: *
9: * @property int $owner_guid
10: * @property int $time_created
11: */
12: abstract class ElggData implements
13: Loggable, // Can events related to this object class be logged
14: Iterator, // Override foreach behaviour
15: ArrayAccess, // Override for array access
16: Exportable
17: {
18:
19: /**
20: * The main attributes of an entity.
21: * Holds attributes to save to database
22: * This contains the site's main properties (id, etc)
23: * Blank entries for all database fields should be created by the constructor.
24: * Subclasses should add to this in their constructors.
25: * Any field not appearing in this will be viewed as a
26: */
27: protected $attributes = array();
28:
29: // @codingStandardsIgnoreStart
30: /**
31: * Initialise the attributes array.
32: *
33: * This is vital to distinguish between metadata and base parameters.
34: *
35: * @param bool $pre18_api Compatibility for subclassing in 1.7 -> 1.8 change.
36: * Passing true (default) emits a deprecation notice.
37: * Passing false returns false. Core constructors always pass false.
38: * Does nothing either way since attributes are initialized by the time
39: * this is called.
40: * @return void
41: * @deprecated 1.8 Use initializeAttributes()
42: */
43: protected function initialise_attributes($pre18_api = true) {
44: if ($pre18_api) {
45: elgg_deprecated_notice('initialise_attributes() is deprecated by initializeAttributes()', 1.8);
46: }
47: }
48: // @codingStandardsIgnoreEnd
49:
50: /**
51: * Initialize the attributes array.
52: *
53: * This is vital to distinguish between metadata and base parameters.
54: *
55: * @return void
56: */
57: protected function initializeAttributes() {
58: // Create attributes array if not already created
59: if (!is_array($this->attributes)) {
60: $this->attributes = array();
61: }
62:
63: $this->attributes['time_created'] = NULL;
64: }
65:
66: /**
67: * Return an attribute or a piece of metadata.
68: *
69: * @param string $name Name
70: *
71: * @return mixed
72: */
73: public function __get($name) {
74: return $this->get($name);
75: }
76:
77: /**
78: * Set an attribute or a piece of metadata.
79: *
80: * @param string $name Name
81: * @param mixed $value Value
82: *
83: * @return mixed
84: */
85: public function __set($name, $value) {
86: return $this->set($name, $value);
87: }
88:
89: /**
90: * Test if property is set either as an attribute or metadata.
91: *
92: * @tip Use isset($entity->property)
93: *
94: * @param string $name The name of the attribute or metadata.
95: *
96: * @return bool
97: */
98: function __isset($name) {
99: return $this->$name !== NULL;
100: }
101:
102: /**
103: * Fetch the specified attribute
104: *
105: * @param string $name The attribute to fetch
106: *
107: * @return mixed The attribute, if it exists. Otherwise, null.
108: */
109: abstract protected function get($name);
110:
111: /**
112: * Set the specified attribute
113: *
114: * @param string $name The attribute to set
115: * @param mixed $value The value to set it to
116: *
117: * @return bool The success of your set function?
118: */
119: abstract protected function set($name, $value);
120:
121: /**
122: * Get a URL for this object
123: *
124: * @return string
125: */
126: abstract public function getURL();
127:
128: /**
129: * Save this data to the appropriate database table.
130: *
131: * @return bool
132: */
133: abstract public function save();
134:
135: /**
136: * Delete this data.
137: *
138: * @return bool
139: */
140: abstract public function delete();
141:
142: /**
143: * Returns the UNIX epoch time that this entity was created
144: *
145: * @return int UNIX epoch time
146: */
147: public function getTimeCreated() {
148: return $this->time_created;
149: }
150:
151: /*
152: * SYSTEM LOG INTERFACE
153: */
154:
155: /**
156: * Return the class name of the object.
157: *
158: * @return string
159: */
160: public function getClassName() {
161: return get_class($this);
162: }
163:
164: /**
165: * Return the GUID of the owner of this object.
166: *
167: * @return int
168: * @deprecated 1.8 Use getOwnerGUID() instead
169: */
170: public function getObjectOwnerGUID() {
171: elgg_deprecated_notice("getObjectOwnerGUID() was deprecated. Use getOwnerGUID().", 1.8);
172: return $this->owner_guid;
173: }
174:
175: /*
176: * ITERATOR INTERFACE
177: */
178:
179: /*
180: * This lets an entity's attributes be displayed using foreach as a normal array.
181: * Example: http://www.sitepoint.com/print/php5-standard-library
182: */
183: protected $valid = FALSE;
184:
185: /**
186: * Iterator interface
187: *
188: * @see Iterator::rewind()
189: *
190: * @return void
191: */
192: public function rewind() {
193: $this->valid = (FALSE !== reset($this->attributes));
194: }
195:
196: /**
197: * Iterator interface
198: *
199: * @see Iterator::current()
200: *
201: * @return mixed
202: */
203: public function current() {
204: return current($this->attributes);
205: }
206:
207: /**
208: * Iterator interface
209: *
210: * @see Iterator::key()
211: *
212: * @return string
213: */
214: public function key() {
215: return key($this->attributes);
216: }
217:
218: /**
219: * Iterator interface
220: *
221: * @see Iterator::next()
222: *
223: * @return void
224: */
225: public function next() {
226: $this->valid = (FALSE !== next($this->attributes));
227: }
228:
229: /**
230: * Iterator interface
231: *
232: * @see Iterator::valid()
233: *
234: * @return bool
235: */
236: public function valid() {
237: return $this->valid;
238: }
239:
240: /*
241: * ARRAY ACCESS INTERFACE
242: */
243:
244: /*
245: * This lets an entity's attributes be accessed like an associative array.
246: * Example: http://www.sitepoint.com/print/php5-standard-library
247: */
248:
249: /**
250: * Array access interface
251: *
252: * @see ArrayAccess::offsetSet()
253: *
254: * @param mixed $key Name
255: * @param mixed $value Value
256: *
257: * @return void
258: */
259: public function offsetSet($key, $value) {
260: if (array_key_exists($key, $this->attributes)) {
261: $this->attributes[$key] = $value;
262: }
263: }
264:
265: /**
266: * Array access interface
267: *
268: * @see ArrayAccess::offsetGet()
269: *
270: * @param mixed $key Name
271: *
272: * @return mixed
273: */
274: public function offsetGet($key) {
275: if (array_key_exists($key, $this->attributes)) {
276: return $this->attributes[$key];
277: }
278: return null;
279: }
280:
281: /**
282: * Array access interface
283: *
284: * @see ArrayAccess::offsetUnset()
285: *
286: * @param mixed $key Name
287: *
288: * @return void
289: */
290: public function offsetUnset($key) {
291: if (array_key_exists($key, $this->attributes)) {
292: // Full unsetting is dangerous for our objects
293: $this->attributes[$key] = "";
294: }
295: }
296:
297: /**
298: * Array access interface
299: *
300: * @see ArrayAccess::offsetExists()
301: *
302: * @param int $offset Offset
303: *
304: * @return int
305: */
306: public function offsetExists($offset) {
307: return array_key_exists($offset, $this->attributes);
308: }
309: }
310: