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\User; |
7
|
|
|
use Tobscure\JsonApi\Resource; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
*/ |
11
|
|
|
trait SessionSerializerTrait { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param mixed $model |
15
|
|
|
* @param array $fields |
16
|
|
|
*/ |
17
|
|
|
public function getAttributes($model, array $fields = null) { |
|
|
|
|
18
|
|
|
return [ |
19
|
|
|
'token' => $model->Token(), |
20
|
|
|
'user_id' => $model->UserId(), |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
*/ |
26
|
|
|
public function getFields() { |
27
|
|
|
return ['token', 'user_id', 'created_at', 'updated_at']; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param mixed $model |
32
|
|
|
*/ |
33
|
|
|
public function getId($model) { |
|
|
|
|
34
|
|
|
return $model->getId(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
*/ |
39
|
|
|
public function getRelationships() { |
40
|
|
|
return [ |
41
|
|
|
'user' => User::getSerializer()->getType(null) |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
*/ |
47
|
|
|
public function getSortFields() { |
48
|
|
|
return ['token', 'user_id']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $model |
53
|
|
|
*/ |
54
|
|
|
public function getType($model) { |
|
|
|
|
55
|
|
|
return 'core/session'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param mixed $model |
60
|
|
|
* @param mixed $data |
61
|
|
|
*/ |
62
|
|
|
public function hydrate($model, $data) { |
|
|
|
|
63
|
|
|
// attributes |
64
|
|
|
$attribs = isset($data['attributes']) ? $data['attributes'] : []; |
65
|
|
|
|
66
|
|
|
$model = HydrateUtils::hydrate($attribs, $model, ['token', 'user_id']); |
67
|
|
|
|
68
|
|
|
// relationships |
69
|
|
|
$this->hydrateRelationships($model, $data); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $model; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $model |
76
|
|
|
* @param mixed $data |
77
|
|
|
*/ |
78
|
|
|
public function setUser($model, $data) { |
79
|
|
|
$model->setUserId($data['id']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $model |
84
|
|
|
* @param mixed $related |
85
|
|
|
*/ |
86
|
|
|
public function user($model, $related) { |
|
|
|
|
87
|
|
|
$serializer = User::getSerializer(); |
88
|
|
|
$relationship = new Relationship(new Resource($model->getUser(), $serializer)); |
89
|
|
|
$relationship->setLinks([ |
90
|
|
|
'related' => '%apiurl%' . $serializer->getType(null) . '/' . $serializer->getId($model) |
91
|
|
|
]); |
92
|
|
|
return $this->addRelationshipSelfLink($relationship, $model, $related); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.