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 users($model, $related) { |
34
|
|
|
$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer())); |
35
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function owner($model) { |
39
|
|
|
$user = UserQuery::create()->findOneById($model->getOwnerId()); |
|
|
|
|
40
|
|
|
$serializer = User::getSerializer(); |
|
|
|
|
41
|
|
|
$relationship = new Relationship(new Resource($user, $serializer)); |
42
|
|
|
$relationship->setLinks([ |
43
|
|
|
'related' => '%apiurl%' . $serializer->getType() . '/' . $serializer->getId($user) |
|
|
|
|
44
|
|
|
]); |
45
|
|
|
return $relationship; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function hydrate($model, $data) { |
|
|
|
|
49
|
|
|
// attributes |
50
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
51
|
|
|
|
52
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['login_name', 'password' => function ($v) { |
53
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
54
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
55
|
|
|
|
56
|
|
|
// relationships |
57
|
|
|
$this->hydrateRelationships($model, $data); |
58
|
|
|
|
59
|
|
|
return $model; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getRelationships() { |
63
|
|
|
return [ |
64
|
|
|
'users' => 'user/users', |
65
|
|
|
'owner' => 'user/users' |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setUsers($model, $data) { |
70
|
|
|
UserGroupQuery::create()->filterByGroup($model)->deleteAll(); |
71
|
|
|
$this->addUsers($model, $data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function addUsers($model, $data) { |
|
|
|
|
75
|
|
|
foreach ($data as $item) { |
76
|
|
|
$group = UserQuery::create()->findOneById($item['id']); |
77
|
|
|
if ($group !== null) { |
78
|
|
|
$model->addUser($group); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function removeUsers($model, $data) { |
|
|
|
|
84
|
|
|
foreach ($data as $item) { |
85
|
|
|
$group = UserQuery::create()->findOneById($item['id']); |
86
|
|
|
if ($group !== null) { |
87
|
|
|
$model->removeUser($group); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setOwner($model, $data) { |
93
|
|
|
$model->setOwnerId($data['id']); |
94
|
|
|
} |
95
|
|
|
} |
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.