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 groups($model, $related) { |
37
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
38
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function hydrate($model, $data) { |
|
|
|
|
42
|
|
|
// attributes |
43
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
44
|
|
|
|
45
|
|
|
$user = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) { |
46
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
47
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
48
|
|
|
|
49
|
|
|
// relationships |
50
|
|
|
$this->hydrateRelationships($model, $data); |
51
|
|
|
|
52
|
|
|
return $user; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getRelationships() { |
56
|
|
|
return ['group' => 'group/groups']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setGroups($model, $data) { |
60
|
|
|
UserGroupQuery::create()->filterByUser($model)->deleteAll(); |
61
|
|
|
$this->addGroups($model, $data); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function addGroups($model, $data) { |
|
|
|
|
65
|
|
|
foreach ($data as $item) { |
66
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
67
|
|
|
if ($group !== null) { |
68
|
|
|
$model->addGroup($group); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function removeGroups($model, $data) { |
|
|
|
|
74
|
|
|
foreach ($data as $item) { |
75
|
|
|
$group = GroupQuery::create()->findOneById($item['id']); |
76
|
|
|
if ($group !== null) { |
77
|
|
|
$model->removeGroup($group); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
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.