1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of JSON-API. |
5
|
|
|
* |
6
|
|
|
* (c) Toby Zerner <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tobscure\JsonApi; |
13
|
|
|
|
14
|
|
|
use JsonSerializable; |
15
|
|
|
|
16
|
|
|
class Relationship implements JsonSerializable |
17
|
|
|
{ |
18
|
|
|
use LinksTrait; |
19
|
|
|
use SelfLinkTrait; |
20
|
|
|
use RelatedLinkTrait; |
21
|
|
|
use PaginationLinksTrait; |
22
|
|
|
use MetaTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The data object. |
26
|
|
|
* |
27
|
|
|
* @var \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null |
28
|
|
|
*/ |
29
|
|
|
protected $data; |
30
|
|
|
|
31
|
21 |
|
private function __construct() |
32
|
|
|
{ |
33
|
21 |
|
} |
34
|
21 |
|
|
35
|
|
|
public static function fromMeta($meta) |
36
|
|
|
{ |
37
|
|
|
$r = new self; |
38
|
|
|
$r->replaceMeta($meta); |
39
|
|
|
return $r; |
40
|
|
|
} |
41
|
12 |
|
|
42
|
|
|
public static function fromSelfLink($link) |
43
|
12 |
|
{ |
44
|
|
|
$r = new self; |
45
|
|
|
$r->setSelfLink($link); |
46
|
|
|
return $r; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function fromRelatedLink($link) |
50
|
|
|
{ |
51
|
|
|
$r = new self; |
52
|
|
|
$r->setRelatedLink($link); |
53
|
|
|
return $r; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function fromData($data) |
57
|
|
|
{ |
58
|
|
|
$r = new self; |
59
|
|
|
$r->data = $data; |
60
|
|
|
return $r; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the data object. |
65
|
12 |
|
* |
66
|
|
|
* @return \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null |
67
|
12 |
|
*/ |
68
|
|
|
public function getData() |
69
|
12 |
|
{ |
70
|
12 |
|
return $this->data; |
71
|
12 |
|
} |
72
|
|
|
|
73
|
12 |
|
/** |
74
|
|
|
* Set the data object. |
75
|
|
|
* |
76
|
|
|
* @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data |
77
|
12 |
|
*/ |
78
|
|
|
public function setData($data) |
79
|
|
|
{ |
80
|
|
|
$this->data = $data; |
81
|
12 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Build the relationship as an array. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function jsonSerialize() |
89
|
|
|
{ |
90
|
|
|
$relationship = []; |
91
|
|
|
|
92
|
|
|
if ($this->data) { |
93
|
|
|
$relationship['data'] = is_array($this->data) |
94
|
|
|
? array_map([$this, 'buildIdentifier'], $this->data) |
95
|
|
|
: $this->buildIdentifier($this->data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return array_filter($relationship + [ |
99
|
|
|
'meta' => $this->meta, |
100
|
|
|
'links' => $this->links |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Build an idenitfier array for the given resource. |
106
|
|
|
* |
107
|
|
|
* @param ResourceInterface $resource |
108
|
|
|
* |
109
|
|
|
* @return array |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
private function buildIdentifier(ResourceInterface $resource) |
112
|
|
|
{ |
113
|
|
|
return [ |
114
|
|
|
'type' => $resource->getType(), |
115
|
|
|
'id' => $resource->getId() |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.