1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\serializer\base; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\Api; |
5
|
|
|
use keeko\core\model\Group; |
6
|
|
|
use keeko\core\model\Module; |
7
|
|
|
use keeko\core\serializer\TypeInferencer; |
8
|
|
|
use keeko\framework\utils\HydrateUtils; |
9
|
|
|
use Tobscure\JsonApi\Collection; |
10
|
|
|
use Tobscure\JsonApi\Relationship; |
11
|
|
|
use Tobscure\JsonApi\Resource; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*/ |
15
|
|
|
trait ActionSerializerTrait { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
*/ |
19
|
|
|
private $methodNames = [ |
20
|
|
|
'groups' => 'Group', |
21
|
|
|
'apis' => 'Api' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
*/ |
26
|
|
|
private $methodPluralNames = [ |
27
|
|
|
'groups' => 'Groups', |
28
|
|
|
'apis' => 'Apis' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param mixed $model |
33
|
|
|
* @return Relationship |
34
|
|
|
*/ |
35
|
|
|
public function apis($model) { |
36
|
|
|
$method = 'get' . $this->getCollectionMethodPluralName('apis'); |
37
|
|
|
$relationship = new Relationship(new Collection($model->$method(), Api::getSerializer())); |
38
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'api'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $model |
43
|
|
|
* @param array $fields |
44
|
|
|
*/ |
45
|
|
|
public function getAttributes($model, array $fields = null) { |
46
|
|
|
return [ |
47
|
|
|
'name' => $model->getName(), |
48
|
|
|
'title' => $model->getTitle(), |
49
|
|
|
'description' => $model->getDescription(), |
50
|
|
|
'class-name' => $model->getClassName() |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
*/ |
56
|
|
|
public function getFields() { |
57
|
|
|
return ['name', 'title', 'description', 'class-name']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param mixed $model |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getId($model) { |
65
|
|
|
if ($model !== null) { |
66
|
|
|
return $model->getId(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
*/ |
74
|
|
|
public function getRelationships() { |
75
|
|
|
return [ |
76
|
|
|
'module' => Module::getSerializer()->getType(null), |
77
|
|
|
'groups' => Group::getSerializer()->getType(null), |
78
|
|
|
'apis' => Api::getSerializer()->getType(null) |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
*/ |
84
|
|
|
public function getSortFields() { |
85
|
|
|
return ['name', 'title', 'description', 'class-name']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $model |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getType($model) { |
93
|
|
|
return 'core/action'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $model |
98
|
|
|
* @return Relationship |
99
|
|
|
*/ |
100
|
|
|
public function groups($model) { |
101
|
|
|
$method = 'get' . $this->getCollectionMethodPluralName('groups'); |
102
|
|
|
$relationship = new Relationship(new Collection($model->$method(), Group::getSerializer())); |
103
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'group'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $model |
108
|
|
|
* @param mixed $data |
109
|
|
|
* @return mixed The model |
110
|
|
|
*/ |
111
|
|
|
public function hydrate($model, $data) { |
112
|
|
|
// attributes |
113
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
114
|
|
|
|
115
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'name', 'title', 'description', 'class-name', 'module-id']); |
116
|
|
|
|
117
|
|
|
// relationships |
118
|
|
|
//$this->hydrateRelationships($model, $data); |
|
|
|
|
119
|
|
|
|
120
|
|
|
return $model; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param mixed $model |
125
|
|
|
* @return Relationship |
126
|
|
|
*/ |
127
|
|
|
public function module($model) { |
128
|
|
|
$serializer = Module::getSerializer(); |
129
|
|
|
$id = $serializer->getId($model->getModule()); |
130
|
|
|
if ($id !== null) { |
131
|
|
|
$relationship = new Relationship(new Resource($model->getModule(), $serializer)); |
132
|
|
|
$relationship->setLinks([ |
133
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $id |
134
|
|
|
]); |
135
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'module'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Relationship $relationship |
143
|
|
|
* @param mixed $model |
144
|
|
|
* @param string $related |
145
|
|
|
* @return Relationship |
146
|
|
|
*/ |
147
|
|
|
abstract protected function addRelationshipSelfLink(Relationship $relationship, $model, $related); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param mixed $relatedName |
151
|
|
|
*/ |
152
|
|
|
protected function getCollectionMethodName($relatedName) { |
|
|
|
|
153
|
|
|
if (isset($this->methodNames[$relatedName])) { |
154
|
|
|
return $this->methodNames[$relatedName]; |
155
|
|
|
} |
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param mixed $relatedName |
161
|
|
|
*/ |
162
|
|
|
protected function getCollectionMethodPluralName($relatedName) { |
|
|
|
|
163
|
|
|
if (isset($this->methodPluralNames[$relatedName])) { |
164
|
|
|
return $this->methodPluralNames[$relatedName]; |
165
|
|
|
} |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
*/ |
171
|
|
|
protected function getTypeInferencer() { |
|
|
|
|
172
|
|
|
return TypeInferencer::getInstance(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.