Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/ElggExtender.php (1 issue)

1
<?php
2
/**
3
 * The base class for \ElggEntity extenders.
4
 *
5
 * Extenders allow you to attach extended information to an
6
 * \ElggEntity.  Core supports two: \ElggAnnotation and \ElggMetadata.
7
 *
8
 * Saving the extender data to database is handled by the child class.
9
 *
10
 * @package    Elgg.Core
11
 * @subpackage DataModel.Extender
12
 * @see        \ElggAnnotation
13
 * @see        \ElggMetadata
14
 *
15
 * @property string $type         annotation or metadata (read-only after save)
16
 * @property int    $id           The unique identifier (read-only)
17
 * @property int    $entity_guid  The GUID of the entity that this extender describes
18
 * @property int    $owner_guid   The GUID of the owner of this extender
19
 * @property int    $access_id    Specifies the visibility level of this extender
20
 * @property string $name         The name of this extender
21
 * @property mixed  $value        The value of the extender (int or string)
22
 * @property int    $time_created A UNIX timestamp of when the extender was created (read-only, set on first save)
23
 * @property string $value_type   'integer' or 'text'
24
 * @property string $enabled      Is this extender enabled ('yes' or 'no')
25
 */
26
abstract class ElggExtender extends \ElggData {
27
28
	protected $int_columns = [
29
		'id',
30
		'entity_guid',
31
		'owner_guid',
32
		'time_created',
33
		'access_id',
34
	];
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39 1036
	protected function initializeAttributes() {
40 1036
		parent::initializeAttributes();
41
42 1036
		$this->attributes['type'] = null;
43 1036
		$this->attributes['id'] = null;
44 1036
		$this->attributes['entity_guid'] = null;
45 1036
		$this->attributes['owner_guid'] = null;
46 1036
		$this->attributes['access_id'] = ACCESS_PRIVATE;
47 1036
		$this->attributes['enabled'] = 'yes';
48 1036
	}
49
50
	/**
51
	 * Set an attribute
52
	 *
53
	 * @param string $name  Name
54
	 * @param mixed  $value Value
55
	 * @return void
56
	 */
57 1036
	public function __set($name, $value) {
58 1036
		if ($name === 'access_id' && $this instanceof ElggMetadata) {
59 1031
			$value = ACCESS_PUBLIC;
60
		}
61 1036
		if (isset($value) && in_array($name, $this->int_columns)) {
62 1036
			$value = (int) $value;
63
		}
64 1036
		$this->attributes[$name] = $value;
65 1036
		if ($name == 'value') {
66 1036
			if (is_bool($value)) {
67 370
				$value = (int) $value;
68
			}
69 1036
			$this->attributes['value_type'] = self::detectValueType($value);
70
		}
71 1036
	}
72
73
	/**
74
	 * Set the value of the extender
75
	 *
76
	 * @param mixed  $value      The value being set
77
	 * @param string $value_type The type of the : 'integer' or 'text'
78
	 * @return void
79
	 * @since 1.9
80
	 */
81
	public function setValue($value, $value_type = '') {
82
		$this->attributes['value'] = $value;
83
		$this->attributes['value_type'] = self::detectValueType($value, $value_type);
84
	}
85
86
	/**
87
	 * Gets an attribute
88
	 *
89
	 * @param string $name Name
90
	 * @return mixed
91
	 */
92 1036
	public function __get($name) {
93 1036
		if (array_key_exists($name, $this->attributes)) {
94 1036
			if ($name == 'value') {
95 1036
				switch ($this->attributes['value_type']) {
96
					case 'integer' :
97 453
						return (int) $this->attributes['value'];
98
						break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
99
					case 'text' :
100 1017
						return $this->attributes['value'];
101
						break;
102
					default :
103
						$msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
104
						throw new \UnexpectedValueException($msg);
105
						break;
106
				}
107
			}
108
109 1036
			if ($name === 'access_id' && $this instanceof ElggMetadata) {
110 335
				return ACCESS_PUBLIC;
111
			}
112
113 1036
			return $this->attributes[$name];
114
		}
115
116 4
		return null;
117
	}
118
119
	/**
120
	 * Get the GUID of the extender's owner entity.
121
	 *
122
	 * @return int The owner GUID
123
	 */
124
	public function getOwnerGUID() {
125
		return $this->owner_guid;
126
	}
127
128
	/**
129
	 * Get the entity that owns this extender
130
	 *
131
	 * @return \ElggEntity
132
	 */
133
	public function getOwnerEntity() {
134
		return get_entity($this->owner_guid);
135
	}
136
137
	/**
138
	 * Get the entity this describes.
139
	 *
140
	 * @return \ElggEntity The entity
141
	 */
142 35
	public function getEntity() {
143 35
		return get_entity($this->entity_guid);
144
	}
145
146
	/**
147
	 * Returns if a user can edit this entity extender.
148
	 *
149
	 * @param int $user_guid The GUID of the user doing the editing
150
	 *                      (defaults to currently logged in user)
151
	 *
152
	 * @return bool
153
	 * @see elgg_set_ignore_access()
154
	 */
155
	abstract public function canEdit($user_guid = 0);
156
157
	/**
158
	 * {@inheritdoc}
159
	 */
160 4
	public function toObject() {
161 4
		$object = new \stdClass();
162 4
		$object->id = $this->id;
163 4
		$object->entity_guid = $this->entity_guid;
164 4
		$object->owner_guid = $this->owner_guid;
165 4
		$object->name = $this->name;
166 4
		$object->value = $this->value;
167 4
		$object->time_created = date('c', $this->getTimeCreated());
168 4
		$object->read_access = $this->access_id;
169
		$params = [
170 4
			$this->getSubtype() => $this, // deprecated use
171 4
			$this->getType() => $this,
172
		];
173 4
		if (_elgg_services()->hooks->hasHandler('to:object', $this->getSubtype())) {
174
			_elgg_services()->deprecation->sendNotice("Triggering 'to:object' hook by extender name '{$this->getSubtype()}' has been deprecated. "
175
			. "Use the generic 'to:object','{$this->getType()}' hook instead.", '2.3');
176
			$object = _elgg_services()->hooks->trigger('to:object', $this->getSubtype(), $params, $object);
177
		}
178 4
		return _elgg_services()->hooks->trigger('to:object', $this->getType(), $params, $object);
179
	}
180
181
	/*
182
	 * SYSTEM LOG INTERFACE
183
	 */
184
185
	/**
186
	 * Return an identification for the object for storage in the system log.
187
	 * This id must be an integer.
188
	 *
189
	 * @return int
190
	 */
191 412
	public function getSystemLogID() {
192 412
		return $this->id;
193
	}
194
195
	/**
196
	 * Return a type of extension.
197
	 *
198
	 * @return string
199
	 */
200 442
	public function getType() {
201 442
		return $this->type;
202
	}
203
204
	/**
205
	 * Return a subtype. For metadata & annotations this is the 'name' and
206
	 * for relationship this is the relationship type.
207
	 *
208
	 * @return string
209
	 */
210 442
	public function getSubtype() {
211 442
		return $this->name;
212
	}
213
214
	/**
215
	 * Get a url for this extender.
216
	 *
217
	 * Plugins can register for the 'extender:url', <type> plugin hook to
218
	 * customize the url for an annotation or metadata.
219
	 *
220
	 * @return string
221
	 */
222 6
	public function getURL() {
223
224 6
		$params = ['extender' => $this];
225 6
		$url = _elgg_services()->hooks->trigger('extender:url', $this->getType(), $params, '');
226
227 6
		return elgg_normalize_url($url);
228
	}
229
230
	/**
231
	 * Detect the value_type for a value to be stored as metadata or an annotation
232
	 *
233
	 * @param mixed  $value      The value
234
	 * @param string $value_type If specified as "text" or "integer", overrides the detection.
235
	 *
236
	 * @return string
237
	 * @access private
238
	 * @internal
239
	 */
240 1036
	public static function detectValueType($value, $value_type = "") {
241 1036
		if ($value_type === 'integer' || $value_type === 'text') {
242
			return $value_type;
243
		}
244
245 1036
		return is_int($value) ? 'integer' : 'text';
246
	}
247
}
248