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\Action; |
7
|
|
|
use Tobscure\JsonApi\Resource; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
*/ |
11
|
|
|
trait ApiSerializerTrait { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param mixed $model |
15
|
|
|
* @param mixed $related |
16
|
|
|
*/ |
17
|
|
|
public function action($model, $related) { |
|
|
|
|
18
|
|
|
$serializer = Action::getSerializer(); |
19
|
|
|
$relationship = new Relationship(new Resource($model->getAction(), $serializer)); |
20
|
|
|
$relationship->setLinks([ |
21
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
22
|
|
|
]); |
23
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $model |
28
|
|
|
* @param array $fields |
29
|
|
|
*/ |
30
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
31
|
|
|
return [ |
32
|
|
|
'id' => $model->Id(), |
33
|
|
|
'route' => $model->Route(), |
34
|
|
|
'method' => $model->Method(), |
35
|
|
|
'action_id' => $model->ActionId(), |
36
|
|
|
'required_params' => $model->RequiredParams(), |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
*/ |
42
|
|
|
public function getFields() { |
43
|
|
|
return ['id', 'route', 'method', 'action_id', 'required_params']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $model |
48
|
|
|
*/ |
49
|
|
|
public function getId($model) { |
|
|
|
|
50
|
|
|
return $model->getId(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
*/ |
55
|
|
|
public function getRelationships() { |
56
|
|
|
return [ |
57
|
|
|
'action' => Action::getSerializer()->getType(null) |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
*/ |
63
|
|
|
public function getSortFields() { |
64
|
|
|
return ['id', 'route', 'method', 'action_id', 'required_params']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $model |
69
|
|
|
*/ |
70
|
|
|
public function getType($model) { |
|
|
|
|
71
|
|
|
return 'core/api'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $model |
76
|
|
|
* @param mixed $data |
77
|
|
|
*/ |
78
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
79
|
|
|
// attributes |
80
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
81
|
|
|
|
82
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'route', 'method', 'action_id', 'required_params']); |
83
|
|
|
|
84
|
|
|
// relationships |
85
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $model; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $model |
92
|
|
|
* @param mixed $data |
93
|
|
|
*/ |
94
|
|
|
public function setAction($model, $data) { |
95
|
|
|
$model->setActionId($data['id']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.