|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\core\serializer\base; |
|
3
|
|
|
|
|
4
|
|
|
use keeko\framework\utils\HydrateUtils; |
|
5
|
|
|
use Tobscure\JsonApi\Relationship; |
|
6
|
|
|
use keeko\core\model\User; |
|
7
|
|
|
use Tobscure\JsonApi\Resource; |
|
8
|
|
|
use keeko\core\model\ActivityObject; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
*/ |
|
12
|
|
|
trait ActivitySerializerTrait { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param mixed $model |
|
16
|
|
|
* @param mixed $related |
|
17
|
|
|
*/ |
|
18
|
|
|
public function actor($model, $related) { |
|
|
|
|
|
|
19
|
|
|
$serializer = User::getSerializer(); |
|
20
|
|
|
$relationship = new Relationship(new Resource($model->getActor(), $serializer)); |
|
21
|
|
|
$relationship->setLinks([ |
|
22
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
23
|
|
|
]); |
|
24
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param mixed $model |
|
29
|
|
|
* @param array $fields |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
|
|
32
|
|
|
return [ |
|
33
|
|
|
'id' => $model->Id(), |
|
34
|
|
|
'actor_id' => $model->ActorId(), |
|
35
|
|
|
'verb' => $model->Verb(), |
|
36
|
|
|
'object_id' => $model->ObjectId(), |
|
37
|
|
|
'target_id' => $model->TargetId(), |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getFields() { |
|
44
|
|
|
return ['id', 'actor_id', 'verb', 'object_id', 'target_id']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $model |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getId($model) { |
|
|
|
|
|
|
51
|
|
|
return $model->getId(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getRelationships() { |
|
57
|
|
|
return [ |
|
58
|
|
|
'actor' => User::getSerializer()->getType(null), |
|
59
|
|
|
'object' => ActivityObject::getSerializer()->getType(null), |
|
60
|
|
|
'target' => ActivityObject::getSerializer()->getType(null) |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getSortFields() { |
|
67
|
|
|
return ['id', 'actor_id', 'verb', 'object_id', 'target_id']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param mixed $model |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getType($model) { |
|
|
|
|
|
|
74
|
|
|
return 'core/activity'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param mixed $model |
|
79
|
|
|
* @param mixed $data |
|
80
|
|
|
*/ |
|
81
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
|
|
82
|
|
|
// attributes |
|
83
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
|
84
|
|
|
|
|
85
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'actor_id', 'verb', 'object_id', 'target_id']); |
|
86
|
|
|
|
|
87
|
|
|
// relationships |
|
88
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
return $model; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param mixed $model |
|
95
|
|
|
* @param mixed $related |
|
96
|
|
|
*/ |
|
97
|
|
|
public function object($model, $related) { |
|
|
|
|
|
|
98
|
|
|
$serializer = ActivityObject::getSerializer(); |
|
99
|
|
|
$relationship = new Relationship(new Resource($model->getObject(), $serializer)); |
|
100
|
|
|
$relationship->setLinks([ |
|
101
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
102
|
|
|
]); |
|
103
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param mixed $model |
|
108
|
|
|
* @param mixed $data |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setActor($model, $data) { |
|
111
|
|
|
$model->setActorId($data['id']); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param mixed $model |
|
116
|
|
|
* @param mixed $data |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setObject($model, $data) { |
|
119
|
|
|
$model->setObjectId($data['id']); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param mixed $model |
|
124
|
|
|
* @param mixed $data |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setTarget($model, $data) { |
|
127
|
|
|
$model->setTargetId($data['id']); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $model |
|
132
|
|
|
* @param mixed $related |
|
133
|
|
|
*/ |
|
134
|
|
|
public function target($model, $related) { |
|
|
|
|
|
|
135
|
|
|
$serializer = ActivityObject::getSerializer(); |
|
136
|
|
|
$relationship = new Relationship(new Resource($model->getTarget(), $serializer)); |
|
137
|
|
|
$relationship->setLinks([ |
|
138
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
139
|
|
|
]); |
|
140
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.