-
Notifications
You must be signed in to change notification settings - Fork 38
/
UserResource.php
135 lines (123 loc) · 4.01 KB
/
UserResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
namespace WoohooLabs\Yin\Examples\User\JsonApi\Resource;
use WoohooLabs\Yin\JsonApi\Schema\Link\Link;
use WoohooLabs\Yin\JsonApi\Schema\Link\RelationshipLinks;
use WoohooLabs\Yin\JsonApi\Schema\Link\ResourceLinks;
use WoohooLabs\Yin\JsonApi\Schema\Relationship\ToManyRelationship;
use WoohooLabs\Yin\JsonApi\Schema\Resource\AbstractResource;
class UserResource extends AbstractResource
{
/**
* @var ContactResource
*/
private $contactTransformer;
public function __construct(ContactResource $contactTransformer)
{
$this->contactTransformer = $contactTransformer;
}
/**
* Provides information about the "type" member of the current resource.
*
* The method returns the type of the current resource.
*
* @param array $user
*/
public function getType($user): string
{
return "users";
}
/**
* Provides information about the "id" member of the current resource.
*
* The method returns the ID of the current resource which should be a UUID.
*
* @param array $user
*/
public function getId($user): string
{
return (string) $user["id"];
}
/**
* Provides information about the "meta" member of the current resource.
*
* The method returns an array of non-standard meta information about the resource. If
* this array is empty, the member won't appear in the response.
*
* @param array $user
*/
public function getMeta($user): array
{
return [];
}
/**
* Provides information about the "links" member of the current resource.
*
* The method returns a new ResourceLinks object if you want to provide linkage
* data about the resource or null if it should be omitted from the response.
*
* @param array $user
*/
public function getLinks($user): ?ResourceLinks
{
return null;
}
/**
* Provides information about the "attributes" member of the current resource.
*
* The method returns an array where the keys signify the attribute names,
* while the values are callables receiving the domain object as an argument,
* and they should return the value of the corresponding attribute.
*
* @param array $user
* @return callable[]
*/
public function getAttributes($user): array
{
return [
"firstname" => function (array $user) {
return $user["firstname"];
},
"surname" => function (array $user) {
return $user["lastname"];
},
];
}
/**
* Returns an array of relationship names which are included in the response by default.
*
* @param array $user
*/
public function getDefaultIncludedRelationships($user): array
{
return [];
}
/**
* Provides information about the "relationships" member of the current resource.
*
* The method returns an array where the keys signify the relationship names,
* while the values are callables receiving the domain object as an argument,
* and they should return a new relationship instance (to-one or to-many).
*
* @param array $user
* @return callable[]
*/
public function getRelationships($user): array
{
return [
"contacts" => function (array $user) {
return ToManyRelationship::create()
->setLinks(
RelationshipLinks::createWithoutBaseUri(
new Link("/users/" . $user["id"] . "/contacts"),
new Link("/users/" . $user["id"] . "/relationships/contacts")
)
)
->setDataAsCallable(function () use ($user) {
return $user["contacts"];
}, $this->contactTransformer)
->omitDataWhenNotIncluded();
},
];
}
}