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\Application; |
7
|
|
|
use Tobscure\JsonApi\Resource; |
8
|
|
|
use keeko\core\model\Localization; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
*/ |
12
|
|
|
trait ApplicationUriSerializerTrait { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param mixed $model |
16
|
|
|
* @param mixed $related |
17
|
|
|
*/ |
18
|
|
|
public function application($model, $related) { |
|
|
|
|
19
|
|
|
$serializer = Application::getSerializer(); |
20
|
|
|
$relationship = new Relationship(new Resource($model->getApplication(), $serializer)); |
21
|
|
|
$relationship->setLinks([ |
22
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
23
|
|
|
]); |
24
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $model |
29
|
|
|
* @param array $fields |
30
|
|
|
*/ |
31
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
32
|
|
|
return [ |
33
|
|
|
'id' => $model->Id(), |
34
|
|
|
'httphost' => $model->Httphost(), |
35
|
|
|
'basepath' => $model->Basepath(), |
36
|
|
|
'secure' => $model->Secure(), |
37
|
|
|
'application_id' => $model->ApplicationId(), |
38
|
|
|
'localization_id' => $model->LocalizationId(), |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
*/ |
44
|
|
|
public function getFields() { |
45
|
|
|
return ['id', 'httphost', 'basepath', 'secure', 'application_id', 'localization_id']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $model |
50
|
|
|
*/ |
51
|
|
|
public function getId($model) { |
|
|
|
|
52
|
|
|
return $model->getId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
*/ |
57
|
|
|
public function getRelationships() { |
58
|
|
|
return [ |
59
|
|
|
'application' => Application::getSerializer()->getType(null), |
60
|
|
|
'localization' => Localization::getSerializer()->getType(null) |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
*/ |
66
|
|
|
public function getSortFields() { |
67
|
|
|
return ['id', 'httphost', 'basepath', 'secure', 'application_id', 'localization_id']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param mixed $model |
72
|
|
|
*/ |
73
|
|
|
public function getType($model) { |
|
|
|
|
74
|
|
|
return 'core/application-uri'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param mixed $model |
79
|
|
|
* @param mixed $data |
80
|
|
|
*/ |
81
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
82
|
|
|
// attributes |
83
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
84
|
|
|
|
85
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'httphost', 'basepath', 'secure', 'application_id', 'localization_id']); |
86
|
|
|
|
87
|
|
|
// relationships |
88
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return $model; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $model |
95
|
|
|
* @param mixed $related |
96
|
|
|
*/ |
97
|
|
|
public function localization($model, $related) { |
|
|
|
|
98
|
|
|
$serializer = Localization::getSerializer(); |
99
|
|
|
$relationship = new Relationship(new Resource($model->getLocalization(), $serializer)); |
100
|
|
|
$relationship->setLinks([ |
101
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
102
|
|
|
]); |
103
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $model |
108
|
|
|
* @param mixed $data |
109
|
|
|
*/ |
110
|
|
|
public function setApplication($model, $data) { |
111
|
|
|
$model->setApplicationId($data['id']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $model |
116
|
|
|
* @param mixed $data |
117
|
|
|
*/ |
118
|
|
|
public function setLocalization($model, $data) { |
119
|
|
|
$model->setLocalizationId($data['id']); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.