1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\model\serializer; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\serializer\AbstractSerializer; |
5
|
|
|
use keeko\core\utils\HydrateUtils; |
6
|
|
|
use keeko\core\model\UserQuery; |
7
|
|
|
use Tobscure\JsonApi\Relationship; |
8
|
|
|
use Tobscure\JsonApi\Resource; |
9
|
|
|
use keeko\core\model\User; |
10
|
|
|
use Tobscure\JsonApi\Collection; |
11
|
|
|
use keeko\core\model\UserGroupQuery; |
12
|
|
|
|
13
|
|
|
class GroupSerializer extends AbstractSerializer { |
14
|
|
|
|
15
|
|
|
public function getType($model) { |
16
|
|
|
return 'group/groups'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getId($model) { |
20
|
|
|
return $model->getId(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getAttributes($model, array $fields = null) { |
24
|
|
|
return [ |
25
|
|
|
'name' => $model->getName(), |
26
|
|
|
'is_guest' => $model->getIsGuest(), |
27
|
|
|
'is_default' => $model->getIsDefault(), |
28
|
|
|
'is_active' => $model->getIsActive(), |
29
|
|
|
'is_system' => $model->getIsSystem() |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getFields() { |
34
|
|
|
return ['name', 'is_guest', 'is_default', 'is_active', 'is_system']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getSortFields() { |
38
|
|
|
return ['name']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function users($model, $related) { |
42
|
|
|
$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer())); |
43
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function owner($model) { |
47
|
|
|
$user = UserQuery::create()->findOneById($model->getOwnerId()); |
|
|
|
|
48
|
|
|
$serializer = User::getSerializer(); |
|
|
|
|
49
|
|
|
$relationship = new Relationship(new Resource($user, $serializer)); |
50
|
|
|
$relationship->setLinks([ |
51
|
|
|
'related' => '%apiurl%' . $serializer->getType() . '/' . $serializer->getId($user) |
|
|
|
|
52
|
|
|
]); |
53
|
|
|
return $relationship; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function hydrate($model, $data) { |
|
|
|
|
57
|
|
|
// attributes |
58
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
59
|
|
|
|
60
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) { |
61
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
62
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
63
|
|
|
|
64
|
|
|
// relationships |
65
|
|
|
$this->hydrateRelationships($model, $data); |
66
|
|
|
|
67
|
|
|
return $model; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getRelationships() { |
71
|
|
|
return [ |
72
|
|
|
'users' => 'user/users', |
73
|
|
|
'owner' => 'user/users' |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setUsers($model, $data) { |
78
|
|
|
UserGroupQuery::create()->filterByGroup($model)->deleteAll(); |
79
|
|
|
$this->addUsers($model, $data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function addUsers($model, $data) { |
|
|
|
|
83
|
|
|
foreach ($data as $item) { |
84
|
|
|
$group = UserQuery::create()->findOneById($item['id']); |
85
|
|
|
if ($group !== null) { |
86
|
|
|
$model->addUser($group); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function removeUsers($model, $data) { |
|
|
|
|
92
|
|
|
foreach ($data as $item) { |
93
|
|
|
$group = UserQuery::create()->findOneById($item['id']); |
94
|
|
|
if ($group !== null) { |
95
|
|
|
$model->removeUser($group); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setOwner($model, $data) { |
101
|
|
|
$model->setOwnerId($data['id']); |
102
|
|
|
} |
103
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.