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 Tobscure\JsonApi\Collection; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*/ |
15
|
|
|
trait LocalizationSerializerTrait { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $model |
19
|
|
|
* @return Relationship |
20
|
|
|
*/ |
21
|
|
|
public function extLang($model) { |
22
|
|
|
$serializer = Language::getSerializer(); |
23
|
|
|
$relationship = new Relationship(new Resource($model->getExtLang(), $serializer)); |
24
|
|
|
$relationship->setLinks([ |
25
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
26
|
|
|
]); |
27
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'ext-lang'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $model |
32
|
|
|
* @param array $fields |
33
|
|
|
*/ |
34
|
|
|
public function getAttributes($model, array $fields = null) { |
35
|
|
|
return [ |
36
|
|
|
'id' => $model->getId(), |
37
|
|
|
'parent_id' => $model->getParentId(), |
38
|
|
|
'name' => $model->getName(), |
39
|
|
|
'locale' => $model->getLocale(), |
40
|
|
|
'language_id' => $model->getLanguageId(), |
41
|
|
|
'ext_language_id' => $model->getExtLanguageId(), |
42
|
|
|
'region' => $model->getRegion(), |
43
|
|
|
'script_id' => $model->getScriptId(), |
44
|
|
|
'is_default' => $model->getIsDefault(), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
*/ |
50
|
|
|
public function getFields() { |
51
|
|
|
return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $model |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getId($model) { |
59
|
|
|
return $model->getId(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
*/ |
64
|
|
|
public function getRelationships() { |
65
|
|
|
return [ |
66
|
|
|
'parent' => Localization::getSerializer()->getType(null), |
67
|
|
|
'ext-lang' => Language::getSerializer()->getType(null), |
68
|
|
|
'script' => LanguageScript::getSerializer()->getType(null), |
69
|
|
|
'language-variants' => LanguageVariant::getSerializer()->getType(null) |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
*/ |
75
|
|
|
public function getSortFields() { |
76
|
|
|
return ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $model |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getType($model) { |
84
|
|
|
return 'core/localization'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $model |
89
|
|
|
* @param mixed $data |
90
|
|
|
* @return mixed The model |
91
|
|
|
*/ |
92
|
|
|
public function hydrate($model, $data) { |
93
|
|
|
// attributes |
94
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
95
|
|
|
|
96
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'parent_id', 'name', 'locale', 'language_id', 'ext_language_id', 'region', 'script_id', 'is_default']); |
97
|
|
|
|
98
|
|
|
// relationships |
99
|
|
|
$this->hydrateRelationships($model, $data); |
100
|
|
|
|
101
|
|
|
return $model; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $model |
106
|
|
|
* @return Relationship |
107
|
|
|
*/ |
108
|
|
|
public function languageVariants($model) { |
109
|
|
|
$relationship = new Relationship(new Collection($model->getLanguageVariants(), LanguageVariant::getSerializer())); |
110
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'language-variants'); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param mixed $model |
115
|
|
|
* @return Relationship |
116
|
|
|
*/ |
117
|
|
|
public function parent($model) { |
118
|
|
|
$serializer = Localization::getSerializer(); |
119
|
|
|
$relationship = new Relationship(new Resource($model->getParent(), $serializer)); |
120
|
|
|
$relationship->setLinks([ |
121
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
122
|
|
|
]); |
123
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'parent'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param mixed $model |
128
|
|
|
* @return Relationship |
129
|
|
|
*/ |
130
|
|
|
public function script($model) { |
131
|
|
|
$serializer = LanguageScript::getSerializer(); |
132
|
|
|
$relationship = new Relationship(new Resource($model->getScript(), $serializer)); |
133
|
|
|
$relationship->setLinks([ |
134
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
135
|
|
|
]); |
136
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'script'); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param mixed $model |
141
|
|
|
* @param mixed $data |
142
|
|
|
*/ |
143
|
|
|
abstract protected function hydrateRelationships($model, $data); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.