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
|
6 |
|
private function __construct() |
32
|
|
|
{ |
33
|
6 |
|
} |
34
|
|
|
|
35
|
|
|
public static function fromMeta($meta) |
36
|
|
|
{ |
37
|
|
|
$r = new self; |
38
|
|
|
$r->replaceMeta($meta); |
39
|
|
|
|
40
|
|
|
return $r; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function fromSelfLink($link) |
44
|
|
|
{ |
45
|
|
|
$r = new self; |
46
|
|
|
$r->setSelfLink($link); |
47
|
|
|
|
48
|
|
|
return $r; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function fromRelatedLink($link) |
52
|
|
|
{ |
53
|
|
|
$r = new self; |
54
|
|
|
$r->setRelatedLink($link); |
55
|
|
|
|
56
|
|
|
return $r; |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
public static function fromData($data) |
60
|
|
|
{ |
61
|
6 |
|
$r = new self; |
62
|
6 |
|
$r->data = $data; |
63
|
|
|
|
64
|
6 |
|
return $r; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the data object. |
69
|
|
|
* |
70
|
|
|
* @return \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null |
71
|
|
|
*/ |
72
|
|
|
public function getData() |
73
|
|
|
{ |
74
|
|
|
return $this->data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the data object. |
79
|
|
|
* |
80
|
|
|
* @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data |
81
|
|
|
*/ |
82
|
|
|
public function setData($data) |
83
|
|
|
{ |
84
|
|
|
$this->data = $data; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build the relationship as an array. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
3 |
|
public function jsonSerialize() |
93
|
|
|
{ |
94
|
3 |
|
$relationship = []; |
95
|
|
|
|
96
|
3 |
|
if ($this->data) { |
97
|
3 |
|
$relationship['data'] = is_array($this->data) |
98
|
3 |
|
? array_map([$this, 'buildIdentifier'], $this->data) |
99
|
3 |
|
: $this->buildIdentifier($this->data); |
100
|
3 |
|
} |
101
|
|
|
|
102
|
3 |
|
return array_filter($relationship + [ |
103
|
3 |
|
'meta' => $this->meta, |
104
|
3 |
|
'links' => $this->links |
105
|
3 |
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Build an idenitfier array for the given resource. |
110
|
|
|
* |
111
|
|
|
* @param ResourceInterface $resource |
112
|
|
|
* |
113
|
|
|
* @return array |
|
|
|
|
114
|
|
|
*/ |
115
|
3 |
|
private function buildIdentifier(ResourceInterface $resource) |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
3 |
|
'type' => $resource->getType(), |
119
|
3 |
|
'id' => $resource->getId() |
120
|
3 |
|
]; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.