1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\model\serializer; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\serializer\AbstractSerializer; |
5
|
|
|
use keeko\core\utils\HydrateUtils; |
6
|
|
|
use Tobscure\JsonApi\Relationship; |
7
|
|
|
use keeko\core\model\Group; |
8
|
|
|
use Tobscure\JsonApi\Collection; |
9
|
|
|
use keeko\core\model\UserGroupQuery; |
10
|
|
|
use keeko\core\model\GroupQuery; |
11
|
|
|
|
12
|
|
|
class UserSerializer extends AbstractSerializer { |
13
|
|
|
|
14
|
|
|
public function getType($model) { |
15
|
|
|
return 'user/users'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function getId($model) { |
19
|
|
|
return $model->getId(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getAttributes($model, array $fields = null) { |
23
|
|
|
return [ |
24
|
|
|
'login_name' => $model->getLoginName(), |
25
|
|
|
'given_name' => $model->getGivenName(), |
26
|
|
|
'family_name' => $model->getFamilyName(), |
27
|
|
|
'display_name' => $model->getDisplayName(), |
28
|
|
|
'email' => $model->getEmail(), |
29
|
|
|
'birthday' => $model->getBirthday(\DateTime::ISO8601), |
30
|
|
|
'sex' => $model->getSex(), |
31
|
|
|
'created_at' => $model->getCreatedAt(\DateTime::ISO8601), |
32
|
|
|
'updated_at' => $model->getUpdatedAt(\DateTime::ISO8601) |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getFields() { |
37
|
|
|
return ['login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex', 'created_at', 'updated_at']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getSortFields() { |
41
|
|
|
return ['login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function groups($model, $related) { |
45
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
46
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function hydrate($model, $data) { |
|
|
|
|
50
|
|
|
// attributes |
51
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
52
|
|
|
|
53
|
|
|
$user = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) { |
54
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
55
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
56
|
|
|
|
57
|
|
|
// relationships |
58
|
|
|
$this->hydrateRelationships($model, $data); |
59
|
|
|
|
60
|
|
|
return $user; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getRelationships() { |
64
|
|
|
return ['group' => 'group/groups']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setGroups($model, $data) { |
68
|
|
|
UserGroupQuery::create()->filterByUser($model)->deleteAll(); |
69
|
|
|
$this->addGroups($model, $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function addGroups($model, $data) { |
|
|
|
|
73
|
|
|
foreach ($data as $item) { |
74
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
75
|
|
|
if ($group !== null) { |
76
|
|
|
$model->addGroup($group); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function removeGroups($model, $data) { |
|
|
|
|
82
|
|
|
foreach ($data as $item) { |
83
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
84
|
|
|
if ($group !== null) { |
85
|
|
|
$model->removeGroup($group); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
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.