Completed
Push — 3.0 ( dabdf7...6f0792 )
by Jeroen
130:21 queued 31:25
created

ElggData::__unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 6011
	protected function initializeAttributes() {
37
		// Create attributes array if not already created
38 6011
		if (!is_array($this->attributes)) {
39
			$this->attributes = [];
40
		}
41
42 6011
		$this->attributes['time_created'] = null;
43 6011
	}
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 1605
	public function __isset($name) {
64 1605
		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
	public function __unset($name) {
77
		$this->$name = null;
78
	}
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 111
94 111
	/**
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
	public function getTimeCreated() {
107
		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 7
	/*
120 7
	 * ITERATOR INTERFACE
121 7
	 */
122
123
	protected $valid = false;
124
125
	/**
126
	 * Iterator interface
127
	 *
128
	 * @see Iterator::rewind()
129
	 *
130 7
	 * @return void
131 7
	 */
132
	public function rewind() {
133
		$this->valid = (false !== reset($this->attributes));
134
	}
135
136
	/**
137
	 * Iterator interface
138
	 *
139
	 * @see Iterator::current()
140
	 *
141 7
	 * @return mixed
142 7
	 */
143
	public function current() {
144
		return current($this->attributes);
145
	}
146
147
	/**
148
	 * Iterator interface
149
	 *
150
	 * @see Iterator::key()
151
	 *
152 7
	 * @return string
153 7
	 */
154 7
	public function key() {
155
		return key($this->attributes);
156
	}
157
158
	/**
159
	 * Iterator interface
160
	 *
161
	 * @see Iterator::next()
162
	 *
163 7
	 * @return void
164 7
	 */
165
	public function next() {
166
		$this->valid = (false !== next($this->attributes));
167
	}
168
169
	/**
170
	 * Iterator interface
171
	 *
172
	 * @see Iterator::valid()
173
	 *
174
	 * @return bool
175
	 */
176
	public function valid() {
177
		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 7
			$this->attributes[$key] = $value;
197 7
		}
198 7
	}
199
200
	/**
201 1
	 * Array access interface
202
	 *
203
	 * @see \ArrayAccess::offsetGet()
204
	 *
205
	 * @param mixed $key Name
206
	 *
207
	 * @return mixed
208
	 */
209
	public function offsetGet($key) {
210
		if (array_key_exists($key, $this->attributes)) {
211
			return $this->$key;
212
		}
213 7
214 7
		return null;
215
	}
216 5
217
	/**
218 7
	 * Array access interface
219
	 *
220
	 * @see \ArrayAccess::offsetUnset()
221
	 *
222
	 * @param mixed $key Name
223
	 *
224
	 * @return void
225
	 */
226
	public function offsetUnset($key) {
227
		if (array_key_exists($key, $this->attributes)) {
228
			// Full unsetting is dangerous for our objects
229
			unset($this->$key);
230
		}
231
	}
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 22
		return $this->getSystemLogID();
251 22
	}
252
253
	/**
254
	 * {@inheritdoc}
255
	 */
256
	public function getPriority() {
257 46
		return $this->getTimeCreated();
258 46
	}
259 46
260
	/**
261
	 * {@inheritdoc}
262
	 */
263
	public function serialize() {
264
		return serialize($this->attributes);
265
	}
266
267
	/**
268
	 * {@inheritdoc}
269
	 */
270
	public function unserialize($serialized) {
271
		$this->attributes = unserialize($serialized);
272
	}
273
}
274