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 keeko\core\model\GroupQuery; |
10
|
|
|
use Tobscure\JsonApi\Collection; |
11
|
|
|
use keeko\core\model\GroupActionQuery; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*/ |
15
|
|
|
trait ActionSerializerTrait { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $model |
19
|
|
|
* @param mixed $data |
20
|
|
|
*/ |
21
|
|
|
public function addGroups($model, $data) { |
22
|
|
|
foreach ($data as $item) { |
23
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
24
|
|
|
if ($group !== null) { |
25
|
|
|
$model->addGroup($group); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $model |
32
|
|
|
* @param array $fields |
33
|
|
|
*/ |
34
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
35
|
|
|
return [ |
36
|
|
|
'id' => $model->Id(), |
37
|
|
|
'name' => $model->Name(), |
38
|
|
|
'title' => $model->Title(), |
39
|
|
|
'description' => $model->Description(), |
40
|
|
|
'class_name' => $model->ClassName(), |
41
|
|
|
'module_id' => $model->ModuleId(), |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
*/ |
47
|
|
|
public function getFields() { |
48
|
|
|
return ['id', 'name', 'title', 'description', 'class_name', 'module_id']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $model |
53
|
|
|
*/ |
54
|
|
|
public function getId($model) { |
|
|
|
|
55
|
|
|
return $model->getId(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
*/ |
60
|
|
|
public function getRelationships() { |
61
|
|
|
return [ |
62
|
|
|
'module' => Module::getSerializer()->getType(null), |
63
|
|
|
'group' => Group::getSerializer()->getType(null) |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
*/ |
69
|
|
|
public function getSortFields() { |
70
|
|
|
return ['id', 'name', 'title', 'description', 'class_name', 'module_id']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $model |
75
|
|
|
*/ |
76
|
|
|
public function getType($model) { |
|
|
|
|
77
|
|
|
return 'core/action'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed $model |
82
|
|
|
* @param mixed $related |
83
|
|
|
*/ |
84
|
|
|
public function group($model, $related) { |
|
|
|
|
85
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
86
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $model |
91
|
|
|
* @param mixed $data |
92
|
|
|
*/ |
93
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
94
|
|
|
// attributes |
95
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
96
|
|
|
|
97
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'name', 'title', 'description', 'class_name', 'module_id']); |
98
|
|
|
|
99
|
|
|
// relationships |
100
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
101
|
|
|
|
102
|
|
|
return $model; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $model |
107
|
|
|
* @param mixed $related |
108
|
|
|
*/ |
109
|
|
|
public function module($model, $related) { |
|
|
|
|
110
|
|
|
$serializer = Module::getSerializer(); |
111
|
|
|
$relationship = new Relationship(new Resource($model->getModule(), $serializer)); |
112
|
|
|
$relationship->setLinks([ |
113
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
114
|
|
|
]); |
115
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param mixed $model |
120
|
|
|
* @param mixed $data |
121
|
|
|
*/ |
122
|
|
|
public function removeGroups($model, $data) { |
123
|
|
|
foreach ($data as $item) { |
124
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
125
|
|
|
if ($group !== null) { |
126
|
|
|
$model->removeGroup($group); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param mixed $model |
133
|
|
|
* @param mixed $data |
134
|
|
|
*/ |
135
|
|
|
public function setGroups($model, $data) { |
136
|
|
|
GroupActionQuery::create()->filterByGroup($model)->delete(); |
137
|
|
|
$this->addGroups($model, $data); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param mixed $model |
142
|
|
|
* @param mixed $data |
143
|
|
|
*/ |
144
|
|
|
public function setModule($model, $data) { |
145
|
|
|
$model->setModuleId($data['id']); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.