ElggData.php 5.9 KB

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