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 | { |
||
14 | |||
15 | use \Elgg\TimeUsing; |
||
16 | |||
17 | /** |
||
18 | * The main attributes of an entity. |
||
19 | * Holds attributes to save to database |
||
20 | * Blank entries for all database fields should be created by the constructor. |
||
21 | * Subclasses should add to this in their constructors. |
||
22 | * Any field not appearing in this will be viewed as metadata |
||
23 | */ |
||
24 | protected $attributes = []; |
||
25 | |||
26 | /** |
||
27 | * Initialize the attributes array. |
||
28 | * |
||
29 | * This is vital to distinguish between metadata and base parameters. |
||
30 | * |
||
31 | * @return void |
||
32 | */ |
||
33 | 5348 | protected function initializeAttributes() { |
|
34 | // Create attributes array if not already created |
||
35 | 5348 | if (!is_array($this->attributes)) { |
|
36 | $this->attributes = []; |
||
37 | } |
||
38 | |||
39 | 5348 | $this->attributes['time_created'] = null; |
|
40 | 5348 | } |
|
41 | |||
42 | /** |
||
43 | * Provides a pointer to the database object. |
||
44 | * |
||
45 | * @return \Elgg\Database The database where this data is (will be) stored. |
||
46 | */ |
||
47 | 7 | protected function getDatabase() { |
|
48 | 7 | return _elgg_services()->db; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Test if property is set either as an attribute or metadata. |
||
53 | * |
||
54 | * @tip Use isset($entity->property) |
||
55 | * |
||
56 | * @param string $name The name of the attribute or metadata. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 1223 | public function __isset($name) { |
|
61 | 1223 | return $this->$name !== null; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get a URL for this object |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | abstract public function getURL(); |
||
70 | |||
71 | /** |
||
72 | * Save this data to the appropriate database table. |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | abstract public function save(); |
||
77 | |||
78 | /** |
||
79 | * Delete this data. |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | abstract public function delete(); |
||
84 | |||
85 | /** |
||
86 | * Returns the UNIX epoch time that this entity was created |
||
87 | * |
||
88 | * @return int UNIX epoch time |
||
89 | */ |
||
90 | 20 | public function getTimeCreated() { |
|
91 | 20 | return $this->attributes['time_created']; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get a plain old object copy for public consumption |
||
96 | * |
||
97 | * @return \stdClass |
||
98 | */ |
||
99 | abstract public function toObject(); |
||
100 | |||
101 | /* |
||
102 | * ITERATOR INTERFACE |
||
103 | */ |
||
104 | |||
105 | protected $valid = false; |
||
106 | |||
107 | /** |
||
108 | * Iterator interface |
||
109 | * |
||
110 | * @see Iterator::rewind() |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | public function rewind() { |
||
115 | $this->valid = (false !== reset($this->attributes)); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Iterator interface |
||
120 | * |
||
121 | * @see Iterator::current() |
||
122 | * |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function current() { |
||
126 | return current($this->attributes); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Iterator interface |
||
131 | * |
||
132 | * @see Iterator::key() |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function key() { |
||
137 | return key($this->attributes); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Iterator interface |
||
142 | * |
||
143 | * @see Iterator::next() |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | public function next() { |
||
148 | $this->valid = (false !== next($this->attributes)); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Iterator interface |
||
153 | * |
||
154 | * @see Iterator::valid() |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function valid() { |
||
159 | return $this->valid; |
||
160 | } |
||
161 | |||
162 | /* |
||
163 | * ARRAY ACCESS INTERFACE |
||
164 | */ |
||
165 | |||
166 | /** |
||
167 | * Array access interface |
||
168 | * |
||
169 | * @see \ArrayAccess::offsetSet() |
||
170 | * |
||
171 | * @param mixed $key Name |
||
172 | * @param mixed $value Value |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function offsetSet($key, $value) { |
||
177 | if (array_key_exists($key, $this->attributes)) { |
||
178 | $this->attributes[$key] = $value; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Array access interface |
||
184 | * |
||
185 | * @see \ArrayAccess::offsetGet() |
||
186 | * |
||
187 | * @param mixed $key Name |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | 1 | public function offsetGet($key) { |
|
192 | 1 | if (array_key_exists($key, $this->attributes)) { |
|
193 | 1 | return $this->attributes[$key]; |
|
194 | } |
||
195 | return null; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Array access interface |
||
200 | * |
||
201 | * @see \ArrayAccess::offsetUnset() |
||
202 | * |
||
203 | * @param mixed $key Name |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public function offsetUnset($key) { |
||
208 | if (array_key_exists($key, $this->attributes)) { |
||
209 | // Full unsetting is dangerous for our objects |
||
210 | $this->attributes[$key] = ""; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Array access interface |
||
216 | * |
||
217 | * @see \ArrayAccess::offsetExists() |
||
218 | * |
||
219 | * @param int $offset Offset |
||
220 | * |
||
221 | * @return int |
||
222 | */ |
||
223 | public function offsetExists($offset) { |
||
224 | return array_key_exists($offset, $this->attributes); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
225 | } |
||
226 | } |
||
227 |