|
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\Localization; |
|
7
|
|
|
use Tobscure\JsonApi\Resource; |
|
8
|
|
|
use keeko\core\model\Language; |
|
9
|
|
|
use keeko\core\model\LanguageScript; |
|
10
|
|
|
use keeko\core\model\LanguageVariant; |
|
11
|
|
|
use keeko\core\model\LanguageVariantQuery; |
|
12
|
|
|
use Tobscure\JsonApi\Collection; |
|
13
|
|
|
use keeko\core\model\LocalizationVariantQuery; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
*/ |
|
17
|
|
|
trait LocalizationSerializerTrait { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param mixed $model |
|
21
|
|
|
* @param mixed $data |
|
22
|
|
|
*/ |
|
23
|
|
|
public function addLanguageVariants($model, $data) { |
|
24
|
|
|
foreach ($data as $item) { |
|
25
|
|
|
$languageVariant = LanguageVariantQuery::create()->findOneById($item['id']); |
|
26
|
|
|
if ($languageVariant !== null) { |
|
27
|
|
|
$model->addLanguageVariant($languageVariant); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param mixed $model |
|
34
|
|
|
* @param mixed $related |
|
35
|
|
|
*/ |
|
36
|
|
|
public function extLang($model, $related) { |
|
|
|
|
|
|
37
|
|
|
$serializer = Language::getSerializer(); |
|
38
|
|
|
$relationship = new Relationship(new Resource($model->getExtLang(), $serializer)); |
|
39
|
|
|
$relationship->setLinks([ |
|
40
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
41
|
|
|
]); |
|
42
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param mixed $model |
|
47
|
|
|
* @param array $fields |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
|
|
50
|
|
|
return [ |
|
51
|
|
|
'id' => $model->Id(), |
|
52
|
|
|
'parent_id' => $model->ParentId(), |
|
53
|
|
|
'name' => $model->Name(), |
|
54
|
|
|
'locale' => $model->Locale(), |
|
55
|
|
|
'language_id' => $model->LanguageId(), |
|
56
|
|
|
'ext_language_id' => $model->ExtLanguageId(), |
|
57
|
|
|
'region' => $model->Region(), |
|
58
|
|
|
'script_id' => $model->ScriptId(), |
|
59
|
|
|
'is_default' => $model->IsDefault(), |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getFields() { |
|
66
|
|
|
return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param mixed $model |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getId($model) { |
|
|
|
|
|
|
73
|
|
|
return $model->getId(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getRelationships() { |
|
79
|
|
|
return [ |
|
80
|
|
|
'localization' => Localization::getSerializer()->getType(null), |
|
81
|
|
|
'language' => Language::getSerializer()->getType(null), |
|
82
|
|
|
'ext-lang' => Language::getSerializer()->getType(null), |
|
83
|
|
|
'script' => LanguageScript::getSerializer()->getType(null), |
|
84
|
|
|
'language-variant' => LanguageVariant::getSerializer()->getType(null) |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getSortFields() { |
|
91
|
|
|
return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param mixed $model |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getType($model) { |
|
|
|
|
|
|
98
|
|
|
return 'core/localization'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $model |
|
103
|
|
|
* @param mixed $data |
|
104
|
|
|
*/ |
|
105
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
|
|
106
|
|
|
// attributes |
|
107
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
|
108
|
|
|
|
|
109
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']); |
|
110
|
|
|
|
|
111
|
|
|
// relationships |
|
112
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
return $model; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param mixed $model |
|
119
|
|
|
* @param mixed $related |
|
120
|
|
|
*/ |
|
121
|
|
|
public function language($model, $related) { |
|
|
|
|
|
|
122
|
|
|
$serializer = Language::getSerializer(); |
|
123
|
|
|
$relationship = new Relationship(new Resource($model->getLanguage(), $serializer)); |
|
124
|
|
|
$relationship->setLinks([ |
|
125
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
126
|
|
|
]); |
|
127
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $model |
|
132
|
|
|
* @param mixed $related |
|
133
|
|
|
*/ |
|
134
|
|
|
public function languageVariant($model, $related) { |
|
|
|
|
|
|
135
|
|
|
$relationship = new Relationship(new Collection($model->getLanguageVariants(), LanguageVariant::getSerializer())); |
|
136
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param mixed $model |
|
141
|
|
|
* @param mixed $related |
|
142
|
|
|
*/ |
|
143
|
|
|
public function localization($model, $related) { |
|
|
|
|
|
|
144
|
|
|
$serializer = Localization::getSerializer(); |
|
145
|
|
|
$relationship = new Relationship(new Resource($model->getLocalization(), $serializer)); |
|
146
|
|
|
$relationship->setLinks([ |
|
147
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
148
|
|
|
]); |
|
149
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param mixed $model |
|
154
|
|
|
* @param mixed $data |
|
155
|
|
|
*/ |
|
156
|
|
|
public function removeLanguageVariants($model, $data) { |
|
157
|
|
|
foreach ($data as $item) { |
|
158
|
|
|
$languageVariant = LanguageVariantQuery::create()->findOneById($item['id']); |
|
159
|
|
|
if ($languageVariant !== null) { |
|
160
|
|
|
$model->removeLanguageVariant($languageVariant); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param mixed $model |
|
167
|
|
|
* @param mixed $related |
|
168
|
|
|
*/ |
|
169
|
|
|
public function script($model, $related) { |
|
|
|
|
|
|
170
|
|
|
$serializer = LanguageScript::getSerializer(); |
|
171
|
|
|
$relationship = new Relationship(new Resource($model->getScript(), $serializer)); |
|
172
|
|
|
$relationship->setLinks([ |
|
173
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
|
174
|
|
|
]); |
|
175
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param mixed $model |
|
180
|
|
|
* @param mixed $data |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setExtLang($model, $data) { |
|
183
|
|
|
$model->setExtLangId($data['id']); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param mixed $model |
|
188
|
|
|
* @param mixed $data |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setLanguage($model, $data) { |
|
191
|
|
|
$model->setLanguageId($data['id']); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param mixed $model |
|
196
|
|
|
* @param mixed $data |
|
197
|
|
|
*/ |
|
198
|
|
|
public function setLanguageVariants($model, $data) { |
|
199
|
|
|
LocalizationVariantQuery::create()->filterByLanguageVariant($model)->delete(); |
|
200
|
|
|
$this->addLanguageVariants($model, $data); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param mixed $model |
|
205
|
|
|
* @param mixed $data |
|
206
|
|
|
*/ |
|
207
|
|
|
public function setLocalization($model, $data) { |
|
208
|
|
|
$model->setLocalizationId($data['id']); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param mixed $model |
|
213
|
|
|
* @param mixed $data |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setScript($model, $data) { |
|
216
|
|
|
$model->setScriptId($data['id']); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
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
@returnannotation as described here.