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\Module; |
7
|
|
|
use Tobscure\JsonApi\Resource; |
8
|
|
|
use keeko\core\model\Group; |
9
|
|
|
use Tobscure\JsonApi\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
*/ |
13
|
|
|
trait ActionSerializerTrait { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param mixed $model |
17
|
|
|
* @param array $fields |
18
|
|
|
*/ |
19
|
|
|
public function getAttributes($model, array $fields = null) { |
20
|
|
|
return [ |
21
|
|
|
'id' => $model->getId(), |
22
|
|
|
'name' => $model->getName(), |
23
|
|
|
'title' => $model->getTitle(), |
24
|
|
|
'description' => $model->getDescription(), |
25
|
|
|
'class_name' => $model->getClassName(), |
26
|
|
|
'module_id' => $model->getModuleId(), |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
*/ |
32
|
|
|
public function getFields() { |
33
|
|
|
return ['id', 'name', 'title', 'description', 'class_name', 'module_id']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $model |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getId($model) { |
41
|
|
|
return $model->getId(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
*/ |
46
|
|
|
public function getRelationships() { |
47
|
|
|
return [ |
48
|
|
|
'module' => Module::getSerializer()->getType(null), |
49
|
|
|
'groups' => Group::getSerializer()->getType(null) |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
*/ |
55
|
|
|
public function getSortFields() { |
56
|
|
|
return ['id', 'name', 'title', 'description', 'class_name', 'module_id']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $model |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getType($model) { |
64
|
|
|
return 'core/action'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $model |
69
|
|
|
* @return Relationship |
70
|
|
|
*/ |
71
|
|
|
public function groups($model) { |
72
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
73
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'groups'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $model |
78
|
|
|
* @param mixed $data |
79
|
|
|
* @return mixed The model |
80
|
|
|
*/ |
81
|
|
|
public function hydrate($model, $data) { |
82
|
|
|
// attributes |
83
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
84
|
|
|
|
85
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'name', 'title', 'description', 'class_name', 'module_id']); |
86
|
|
|
|
87
|
|
|
// relationships |
88
|
|
|
$this->hydrateRelationships($model, $data); |
89
|
|
|
|
90
|
|
|
return $model; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $model |
95
|
|
|
* @return Relationship |
96
|
|
|
*/ |
97
|
|
|
public function module($model) { |
98
|
|
|
$serializer = Module::getSerializer(); |
99
|
|
|
$relationship = new Relationship(new Resource($model->getModule(), $serializer)); |
100
|
|
|
$relationship->setLinks([ |
101
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
102
|
|
|
]); |
103
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'module'); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $model |
108
|
|
|
* @param mixed $data |
109
|
|
|
*/ |
110
|
|
|
abstract protected function hydrateRelationships($model, $data); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
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.