1 | <?php |
||
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() { |
||
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() { |
||
69 | |||
70 | /** |
||
71 | * @param mixed $model |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getType($model) { |
||
77 | |||
78 | /** |
||
79 | * @param mixed $model |
||
80 | * @param mixed $data |
||
81 | * @return mixed The model |
||
82 | */ |
||
83 | public function hydrate($model, $data) { |
||
94 | |||
95 | /** |
||
96 | * @param mixed $model |
||
97 | * @return Relationship |
||
98 | */ |
||
99 | public function target($model) { |
||
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.