1 | <?php |
||
2 | |||
3 | namespace Elgg\Notifications; |
||
4 | |||
5 | use ElggData; |
||
6 | use ElggEntity; |
||
7 | use stdClass; |
||
8 | |||
9 | /** |
||
10 | * Allow events to be (un)serialized |
||
11 | */ |
||
12 | trait EventSerialization { |
||
13 | |||
14 | /** |
||
15 | * Serializes event object for database storage |
||
16 | * @return string |
||
17 | */ |
||
18 | 49 | public function serialize() { |
|
19 | 49 | $data = new stdClass(); |
|
20 | 49 | $data->action = $this->action; |
|
21 | 49 | if ($this->object instanceof ElggData) { |
|
22 | 49 | if ($this->object instanceof ElggEntity) { |
|
23 | 28 | $data->object_id = $this->object->guid; |
|
24 | } else { |
||
25 | 21 | $data->object_id = $this->object->id; |
|
26 | } |
||
27 | 49 | $data->object_type = $this->object->getType(); |
|
28 | 49 | $data->object_subtype = $this->object->getSubtype(); |
|
29 | } |
||
30 | 49 | if ($this->actor) { |
|
31 | 49 | $data->actor_guid = $this->actor->guid; |
|
32 | } |
||
33 | 49 | return serialize($data); |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * Unserializes the event object stored in the database |
||
38 | * |
||
39 | * @param string $serialized Serialized string |
||
40 | * @return string |
||
41 | */ |
||
42 | 36 | public function unserialize($serialized) { |
|
43 | 36 | $data = unserialize($serialized); |
|
44 | 36 | if (isset($data->action)) { |
|
45 | 36 | $this->action = $data->action; |
|
1 ignored issue
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
46 | } |
||
47 | 36 | if (isset($data->object_id) && isset($data->object_type)) { |
|
48 | 36 | switch ($data->object_type) { |
|
49 | case 'object' : |
||
50 | case 'user' : |
||
51 | case 'group' : |
||
52 | case 'site' : |
||
53 | 18 | $this->object = get_entity($data->object_id); |
|
1 ignored issue
–
show
|
|||
54 | 18 | break; |
|
55 | case 'annotation' : |
||
56 | 6 | $this->object = elgg_get_annotation_from_id($data->object_id); |
|
57 | 6 | break; |
|
58 | case 'metadata' : |
||
59 | 6 | $this->object = elgg_get_metadata_from_id($data->object_id); |
|
60 | 6 | break; |
|
61 | case 'relationship' : |
||
62 | 6 | $this->object = get_relationship($data->object_id); |
|
63 | } |
||
64 | } |
||
65 | |||
66 | 36 | if (isset($data->actor_guid)) { |
|
67 | 36 | $this->actor = get_entity($data->actor_guid); |
|
1 ignored issue
–
show
|
|||
68 | } |
||
69 | 36 | } |
|
70 | |||
71 | } |
||
72 |