Elgg  Version 4.3
ElggExtender.php
Go to the documentation of this file.
1 <?php
2 
3 use Elgg\Exceptions\UnexpectedValueException as ElggUnexpectedValueException;
4 
27 abstract class ElggExtender extends \ElggData {
28 
32  protected const INTEGER_ATTR_NAMES = [
33  'id',
34  'entity_guid',
35  'owner_guid',
36  'access_id',
37  'time_created',
38  ];
39 
43  protected function initializeAttributes() {
44  parent::initializeAttributes();
45 
46  $this->attributes['type'] = null;
47  $this->attributes['id'] = null;
48  $this->attributes['entity_guid'] = null;
49  $this->attributes['owner_guid'] = null;
50  $this->attributes['access_id'] = ACCESS_PRIVATE;
51  $this->attributes['enabled'] = 'yes';
52  }
53 
61  public function __set($name, $value) {
62  if ($name === 'access_id' && $this instanceof ElggMetadata) {
64  }
65  if (isset($value) && in_array($name, static::INTEGER_ATTR_NAMES)) {
66  $value = (int) $value;
67  }
68  $this->attributes[$name] = $value;
69  if ($name == 'value') {
70  $this->attributes['value_type'] = self::detectValueType($value);
71  }
72  }
73 
82  public function setValue($value, $value_type = '') {
83  $this->attributes['value'] = $value;
84  $this->attributes['value_type'] = self::detectValueType($value, $value_type);
85  }
86 
95  public function __get($name) {
96  if (array_key_exists($name, $this->attributes)) {
97  if ($name == 'value') {
98  switch ($this->attributes['value_type']) {
99  case 'bool' :
100  return (bool) $this->attributes['value'];
101  case 'integer' :
102  return (int) $this->attributes['value'];
103  case 'text' :
104  return $this->attributes['value'];
105  default :
106  $msg = "{$this->attributes['value_type']} is not a supported \ElggExtender value type.";
107  throw new ElggUnexpectedValueException($msg);
108  break;
109  }
110  }
111 
112  if ($name === 'access_id' && $this instanceof ElggMetadata) {
113  return ACCESS_PUBLIC;
114  }
115 
116  return $this->attributes[$name];
117  }
118 
119  return null;
120  }
121 
127  public function getOwnerGUID() {
128  return $this->owner_guid;
129  }
130 
136  public function getOwnerEntity() {
137  return get_entity($this->owner_guid);
138  }
139 
145  public function getEntity() {
146  return get_entity($this->entity_guid);
147  }
148 
157  abstract public function canEdit($user_guid = 0);
158 
162  public function toObject(array $params = []) {
163 
164  $object = new \Elgg\Export\Extender();
165  $object->id = $this->id;
166  $object->entity_guid = $this->entity_guid;
167  $object->owner_guid = $this->owner_guid;
168  $object->name = $this->name;
169  $object->value = $this->value;
170  $object->time_created = date('c', $this->getTimeCreated());
171  $object->read_access = $this->access_id;
172 
173  $params[$this->getType()] = $this;
174 
175  return _elgg_services()->hooks->trigger('to:object', $this->getType(), $params, $object);
176  }
177 
178  /*
179  * SYSTEM LOG INTERFACE
180  */
181 
188  public function getSystemLogID() {
189  return $this->id;
190  }
191 
197  public function getType() {
198  return $this->type;
199  }
200 
207  public function getSubtype() {
208  return $this->name;
209  }
210 
219  public function getURL() {
220 
221  $params = ['extender' => $this];
222  $url = _elgg_services()->hooks->trigger('extender:url', $this->getType(), $params, '');
223 
224  return elgg_normalize_url($url);
225  }
226 
236  public static function detectValueType($value, string $value_type = ''): string {
237  if (in_array($value_type, ['integer', 'text', 'bool'])) {
238  return $value_type;
239  }
240 
241  if (is_int($value)) {
242  return 'integer';
243  } elseif (is_bool($value)) {
244  return 'bool';
245  }
246 
247  return 'text';
248  }
249 }
getType()
Return a type of extension.
$user_guid
Definition: login_as.php:10
getTimeCreated()
Returns the UNIX epoch time that this entity was created.
Definition: ElggData.php:100
initializeAttributes()
{}
$params
Saves global plugin settings.
Definition: save.php:13
getEntity()
Get the entity this describes.
elgg_normalize_url($url)
Definition: output.php:153
__get($name)
Gets an attribute.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
canEdit($user_guid=0)
Returns if a user can edit this entity extender.
const INTEGER_ATTR_NAMES
$type
Definition: delete.php:21
__set($name, $value)
Set an attribute.
$value
Definition: generic.php:51
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
$owner_guid
const ACCESS_PRIVATE
Definition: constants.php:12
Exception thrown if a value does not match with a set of values.
static detectValueType($value, string $value_type= '')
Detect the value_type for a value to be stored as metadata or an annotation.
toObject(array $params=[])
{}
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
getOwnerEntity()
Get the entity that owns this extender.
getOwnerGUID()
Get the GUID of the extender&#39;s owner entity.
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
getSystemLogID()
Return an identification for the object for storage in the system log.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
const ACCESS_PUBLIC
Definition: constants.php:14
$access_id
Definition: access.php:11
$id
Generic annotation delete action.
Definition: delete.php:6
getSubtype()
Return a subtype.
setValue($value, $value_type= '')
Set the value of the extender.
getURL()
Get a url for this extender.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:69