|
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 Tobscure\JsonApi\Collection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
*/ |
|
11
|
|
|
trait UserSerializerTrait { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param mixed $model |
|
15
|
|
|
* @param array $fields |
|
16
|
|
|
*/ |
|
17
|
|
|
public function getAttributes($model, array $fields = null) { |
|
18
|
|
|
return [ |
|
19
|
|
|
'id' => $model->getId(), |
|
20
|
|
|
'login_name' => $model->getLoginName(), |
|
21
|
|
|
'password' => $model->getPassword(), |
|
22
|
|
|
'given_name' => $model->getGivenName(), |
|
23
|
|
|
'family_name' => $model->getFamilyName(), |
|
24
|
|
|
'display_name' => $model->getDisplayName(), |
|
25
|
|
|
'email' => $model->getEmail(), |
|
26
|
|
|
'birthday' => $model->getBirthday(\DateTime::ISO8601), |
|
27
|
|
|
'sex' => $model->getSex(), |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFields() { |
|
34
|
|
|
return ['id', 'login_name', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex', 'created_at', 'updated_at']; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param mixed $model |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getId($model) { |
|
42
|
|
|
return $model->getId(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getRelationships() { |
|
48
|
|
|
return [ |
|
49
|
|
|
'groups' => Group::getSerializer()->getType(null) |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getSortFields() { |
|
56
|
|
|
return ['id', 'login_name', 'password', 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param mixed $model |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getType($model) { |
|
64
|
|
|
return 'core/user'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param mixed $model |
|
69
|
|
|
* @return Relationship |
|
70
|
|
|
*/ |
|
71
|
|
|
public function groups($model) { |
|
72
|
|
|
$relationship = new Relationship(new Collection($model->getGroups(), Group::getSerializer())); |
|
73
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, 'groups'); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param mixed $model |
|
78
|
|
|
* @param mixed $data |
|
79
|
|
|
* @return mixed The model |
|
80
|
|
|
*/ |
|
81
|
|
|
public function hydrate($model, $data) { |
|
82
|
|
|
// attributes |
|
83
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
|
84
|
|
|
|
|
85
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['id', 'login_name', 'password' => function($v) { |
|
86
|
|
|
return password_hash($v, PASSWORD_BCRYPT); |
|
87
|
|
|
}, 'given_name', 'family_name', 'display_name', 'email', 'birthday', 'sex']); |
|
88
|
|
|
|
|
89
|
|
|
// relationships |
|
90
|
|
|
$this->hydrateRelationships($model, $data); |
|
91
|
|
|
|
|
92
|
|
|
return $model; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $model |
|
97
|
|
|
* @param mixed $data |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract protected function hydrateRelationships($model, $data); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.