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\Package; |
7
|
|
|
use Tobscure\JsonApi\Resource; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
*/ |
11
|
|
|
trait ApplicationSerializerTrait { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param mixed $model |
15
|
|
|
* @param array $fields |
16
|
|
|
*/ |
17
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
18
|
|
|
return [ |
19
|
|
|
'class_name' => $model->ClassName(), |
20
|
|
|
'id' => $model->Id(), |
21
|
|
|
'name' => $model->Name(), |
22
|
|
|
'title' => $model->Title(), |
23
|
|
|
'description' => $model->Description(), |
24
|
|
|
'installed_version' => $model->InstalledVersion(), |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
*/ |
30
|
|
|
public function getFields() { |
31
|
|
|
return ['class_name', 'id', 'name', 'title', 'description', 'installed_version']; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param mixed $model |
36
|
|
|
*/ |
37
|
|
|
public function getId($model) { |
|
|
|
|
38
|
|
|
return $model->getId(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
*/ |
43
|
|
|
public function getRelationships() { |
44
|
|
|
return [ |
45
|
|
|
'package' => Package::getSerializer()->getType(null) |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
*/ |
51
|
|
|
public function getSortFields() { |
52
|
|
|
return ['class_name', 'id', 'name', 'title', 'description', 'installed_version']; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param mixed $model |
57
|
|
|
*/ |
58
|
|
|
public function getType($model) { |
|
|
|
|
59
|
|
|
return 'core/application'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $model |
64
|
|
|
* @param mixed $data |
65
|
|
|
*/ |
66
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
67
|
|
|
// attributes |
68
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
69
|
|
|
|
70
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['class_name', 'id', 'name', 'title', 'description', 'installed_version']); |
71
|
|
|
|
72
|
|
|
// relationships |
73
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $model; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $model |
80
|
|
|
* @param mixed $related |
81
|
|
|
*/ |
82
|
|
|
public function package($model, $related) { |
|
|
|
|
83
|
|
|
$serializer = Package::getSerializer(); |
84
|
|
|
$relationship = new Relationship(new Resource($model->getPackage(), $serializer)); |
85
|
|
|
$relationship->setLinks([ |
86
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
87
|
|
|
]); |
88
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param mixed $model |
93
|
|
|
* @param mixed $data |
94
|
|
|
*/ |
95
|
|
|
public function setPackage($model, $data) { |
96
|
|
|
$model->setPackageId($data['id']); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.