1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Elgg\Collections\CollectionItemInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A generic class that contains shared code among |
7
|
|
|
* \ElggExtender, \ElggEntity, and \ElggRelationship |
8
|
|
|
* |
9
|
|
|
* @package Elgg.Core |
10
|
|
|
* @subpackage DataModel |
11
|
|
|
*/ |
12
|
|
|
abstract class ElggData implements CollectionItemInterface, |
13
|
|
|
Serializable, |
14
|
|
|
Loggable, |
15
|
|
|
Iterator, |
16
|
|
|
ArrayAccess { |
17
|
|
|
|
18
|
|
|
use \Elgg\TimeUsing; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The main attributes of an entity. |
22
|
|
|
* Holds attributes to save to database |
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 metadata |
26
|
|
|
*/ |
27
|
|
|
protected $attributes = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initialize the attributes array. |
31
|
|
|
* |
32
|
|
|
* This is vital to distinguish between metadata and base parameters. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
6094 |
|
protected function initializeAttributes() { |
37
|
|
|
// Create attributes array if not already created |
38
|
6094 |
|
if (!is_array($this->attributes)) { |
|
|
|
|
39
|
|
|
$this->attributes = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
6094 |
|
$this->attributes['time_created'] = null; |
43
|
6094 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Provides a pointer to the database object. |
47
|
|
|
* |
48
|
|
|
* @return \Elgg\Database The database where this data is (will be) stored. |
49
|
|
|
*/ |
50
|
5 |
|
protected function getDatabase() { |
51
|
5 |
|
return _elgg_services()->db; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test if property is set either as an attribute or metadata. |
56
|
|
|
* |
57
|
|
|
* @tip Use isset($entity->property) |
58
|
|
|
* |
59
|
|
|
* @param string $name The name of the attribute or metadata. |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
1672 |
|
public function __isset($name) { |
64
|
1672 |
|
return $this->$name !== null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Unset a property from metadata or attribute. |
69
|
|
|
* |
70
|
|
|
* @warning If you use this to unset an attribute, you must save the object! |
71
|
|
|
* |
72
|
|
|
* @param string $name The name of the attribute or metadata. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
94 |
|
public function __unset($name) { |
77
|
94 |
|
$this->$name = null; |
78
|
94 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get a URL for this object |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
abstract public function getURL(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Save this data to the appropriate database table. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
abstract public function save(); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delete this data. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
abstract public function delete(); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the UNIX epoch time that this entity was created |
103
|
|
|
* |
104
|
|
|
* @return int UNIX epoch time |
105
|
|
|
*/ |
106
|
113 |
|
public function getTimeCreated() { |
107
|
113 |
|
return $this->attributes['time_created']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get a plain old object copy for public consumption |
112
|
|
|
* |
113
|
|
|
* @param array $params Export parameters |
114
|
|
|
* |
115
|
|
|
* @return \Elgg\Export\Data |
116
|
|
|
*/ |
117
|
|
|
abstract public function toObject(array $params = []); |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* ITERATOR INTERFACE |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
protected $valid = false; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Iterator interface |
127
|
|
|
* |
128
|
|
|
* @see Iterator::rewind() |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
7 |
|
public function rewind() { |
133
|
7 |
|
$this->valid = (false !== reset($this->attributes)); |
134
|
7 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Iterator interface |
138
|
|
|
* |
139
|
|
|
* @see Iterator::current() |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
7 |
|
public function current() { |
144
|
7 |
|
return current($this->attributes); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Iterator interface |
149
|
|
|
* |
150
|
|
|
* @see Iterator::key() |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
7 |
|
public function key() { |
155
|
7 |
|
return key($this->attributes); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Iterator interface |
160
|
|
|
* |
161
|
|
|
* @see Iterator::next() |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
7 |
|
public function next() { |
166
|
7 |
|
$this->valid = (false !== next($this->attributes)); |
167
|
7 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Iterator interface |
171
|
|
|
* |
172
|
|
|
* @see Iterator::valid() |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
7 |
|
public function valid() { |
177
|
7 |
|
return $this->valid; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* |
181
|
|
|
* ARRAY ACCESS INTERFACE |
182
|
|
|
*/ |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Array access interface |
186
|
|
|
* |
187
|
|
|
* @see \ArrayAccess::offsetSet() |
188
|
|
|
* |
189
|
|
|
* @param mixed $key Name |
190
|
|
|
* @param mixed $value Value |
191
|
|
|
* |
192
|
|
|
* @return void |
193
|
|
|
*/ |
194
|
|
|
public function offsetSet($key, $value) { |
195
|
|
|
if (array_key_exists($key, $this->attributes)) { |
196
|
|
|
$this->attributes[$key] = $value; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Array access interface |
202
|
|
|
* |
203
|
|
|
* @see \ArrayAccess::offsetGet() |
204
|
|
|
* |
205
|
|
|
* @param mixed $key Name |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
7 |
|
public function offsetGet($key) { |
210
|
7 |
|
if (array_key_exists($key, $this->attributes)) { |
211
|
7 |
|
return $this->$key; |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
return null; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Array access interface |
219
|
|
|
* |
220
|
|
|
* @see \ArrayAccess::offsetUnset() |
221
|
|
|
* |
222
|
|
|
* @param mixed $key Name |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
7 |
|
public function offsetUnset($key) { |
227
|
7 |
|
if (array_key_exists($key, $this->attributes)) { |
228
|
|
|
// Full unsetting is dangerous for our objects |
229
|
5 |
|
unset($this->$key); |
230
|
|
|
} |
231
|
7 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Array access interface |
235
|
|
|
* |
236
|
|
|
* @see \ArrayAccess::offsetExists() |
237
|
|
|
* |
238
|
|
|
* @param int $offset Offset |
239
|
|
|
* |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function offsetExists($offset) { |
243
|
|
|
return array_key_exists($offset, $this->attributes); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
|
|
public function getId() { |
250
|
|
|
return $this->getSystemLogID(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
public function getPriority() { |
257
|
|
|
return $this->getTimeCreated(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritdoc} |
262
|
|
|
*/ |
263
|
24 |
|
public function serialize() { |
264
|
24 |
|
return serialize($this->attributes); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
70 |
|
public function unserialize($serialized) { |
271
|
70 |
|
$this->attributes = unserialize($serialized); |
272
|
70 |
|
} |
273
|
|
|
} |
274
|
|
|
|