|
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\Group; |
|
7
|
|
|
use keeko\core\model\GroupQuery; |
|
8
|
|
|
use Tobscure\JsonApi\Collection; |
|
9
|
|
|
use keeko\core\model\UserGroupQuery; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
*/ |
|
13
|
|
|
trait UserSerializerTrait { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param mixed $model |
|
17
|
|
|
* @param mixed $data |
|
18
|
|
|
*/ |
|
19
|
|
|
public function addGroups($model, $data) { |
|
20
|
|
|
foreach ($data as $item) { |
|
21
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
|
22
|
|
|
if ($group !== null) { |
|
23
|
|
|
$model->addGroup($group); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param mixed $model |
|
30
|
|
|
* @param array $fields |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
|
|
33
|
|
|
return [ |
|
34
|
|
|
'id' => $model->Id(), |
|
35
|
|
|
'login_name' => $model->LoginName(), |
|
36
|
|
|
'password' => $model->Password(), |
|
37
|
|
|
'given_name' => $model->GivenName(), |
|
38
|
|
|
'family_name' => $model->FamilyName(), |
|
39
|
|
|
'display_name' => $model->DisplayName(), |
|
40
|
|
|
'email' => $model->Email(), |
|
41
|
|
|
'birthday' => $model->Birthday(\DateTime::ISO8601), |
|
42
|
|
|
'sex' => $model->Sex(), |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFields() { |
|
49
|
|
|
return ['id', 'login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex', 'created_at', 'updated_at']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $model |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getId($model) { |
|
|
|
|
|
|
56
|
|
|
return $model->getId(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getRelationships() { |
|
62
|
|
|
return [ |
|
63
|
|
|
'group' => Group::getSerializer()->getType(null) |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getSortFields() { |
|
70
|
|
|
return ['id', 'login_name', 'password', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param mixed $model |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getType($model) { |
|
|
|
|
|
|
77
|
|
|
return 'core/user'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param mixed $model |
|
82
|
|
|
* @param mixed $related |
|
83
|
|
|
*/ |
|
84
|
|
|
public function group($model, $related) { |
|
|
|
|
|
|
85
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
|
86
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param mixed $model |
|
91
|
|
|
* @param mixed $data |
|
92
|
|
|
*/ |
|
93
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
|
|
94
|
|
|
// attributes |
|
95
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
|
96
|
|
|
|
|
97
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'login_name', 'password' => function($v) { |
|
98
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
|
99
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
|
100
|
|
|
|
|
101
|
|
|
// relationships |
|
102
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
return $model; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param mixed $model |
|
109
|
|
|
* @param mixed $data |
|
110
|
|
|
*/ |
|
111
|
|
|
public function removeGroups($model, $data) { |
|
112
|
|
|
foreach ($data as $item) { |
|
113
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
|
114
|
|
|
if ($group !== null) { |
|
115
|
|
|
$model->removeGroup($group); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param mixed $model |
|
122
|
|
|
* @param mixed $data |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setGroups($model, $data) { |
|
125
|
|
|
UserGroupQuery::create()->filterByGroup($model)->delete(); |
|
126
|
|
|
$this->addGroups($model, $data); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.