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
|
|
|
* @return Relationship |
17
|
|
|
*/ |
18
|
|
|
public function actor($model) { |
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, 'actor'); |
|
|
|
|
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->getId(), |
34
|
|
|
'actor_id' => $model->getActorId(), |
35
|
|
|
'verb' => $model->getVerb(), |
36
|
|
|
'object_id' => $model->getObjectId(), |
37
|
|
|
'target_id' => $model->getTargetId(), |
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
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getId($model) { |
52
|
|
|
return $model->getId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
*/ |
57
|
|
|
public function getRelationships() { |
58
|
|
|
return [ |
59
|
|
|
'actor' => User::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
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getType($model) { |
75
|
|
|
return 'core/activity'; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $model |
80
|
|
|
* @param mixed $data |
81
|
|
|
* @return mixed The model |
82
|
|
|
*/ |
83
|
|
|
public function hydrate($model, $data) { |
84
|
|
|
// attributes |
85
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
86
|
|
|
|
87
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'actor_id', 'verb', 'object_id', 'target_id']); |
88
|
|
|
|
89
|
|
|
// relationships |
90
|
|
|
$this->hydrateRelationships($model, $data); |
91
|
|
|
|
92
|
|
|
return $model; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $model |
97
|
|
|
* @return Relationship |
98
|
|
|
*/ |
99
|
|
|
public function target($model) { |
100
|
|
|
$serializer = ActivityObject::getSerializer(); |
101
|
|
|
$relationship = new Relationship(new Resource($model->getTarget(), $serializer)); |
102
|
|
|
$relationship->setLinks([ |
103
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
104
|
|
|
]); |
105
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'target'); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $model |
110
|
|
|
* @param mixed $data |
111
|
|
|
*/ |
112
|
|
|
abstract protected function hydrateRelationships($model, $data); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.