|
1
|
|
|
<?php namespace Neomerx\JsonApi\Schema; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Closure; |
|
20
|
|
|
use Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface; |
|
21
|
|
|
use Neomerx\JsonApi\Factories\Exceptions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Neomerx\JsonApi |
|
25
|
|
|
*/ |
|
26
|
|
|
class RelationshipObject implements RelationshipObjectInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $name; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var object|array|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $data; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface> |
|
40
|
|
|
*/ |
|
41
|
|
|
private $links; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var object|array|null|Closure |
|
45
|
|
|
*/ |
|
46
|
|
|
private $meta; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $isShowData; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var bool |
|
55
|
|
|
*/ |
|
56
|
|
|
private $isRoot; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool |
|
60
|
|
|
*/ |
|
61
|
|
|
private $isMetaEvaluated = false; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var bool |
|
65
|
|
|
*/ |
|
66
|
|
|
private $isDataEvaluated = false; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string|null $name |
|
70
|
|
|
* @param object|array|null|Closure $data |
|
71
|
|
|
* @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface> $links |
|
72
|
|
|
* @param object|array|null|Closure $meta |
|
73
|
|
|
* @param bool $isShowData |
|
74
|
|
|
* @param bool $isRoot |
|
75
|
|
|
* |
|
76
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
77
|
|
|
*/ |
|
78
|
84 |
|
public function __construct( |
|
79
|
|
|
?string $name, |
|
80
|
|
|
$data, |
|
81
|
|
|
array $links, |
|
82
|
|
|
$meta, |
|
83
|
|
|
bool $isShowData, |
|
84
|
|
|
bool $isRoot |
|
85
|
|
|
) { |
|
86
|
84 |
|
$isOk = (($isRoot === false && $name !== null) || ($isRoot === true && $name === null)); |
|
87
|
84 |
|
$isOk ?: Exceptions::throwInvalidArgument('name', $name); |
|
88
|
|
|
|
|
89
|
84 |
|
$this->setName($name)->setData($data)->setLinks($links)->setMeta($meta); |
|
90
|
84 |
|
$this->isShowData = $isShowData; |
|
91
|
84 |
|
$this->isRoot = $isRoot; |
|
92
|
84 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
84 |
|
public function getName(): ?string |
|
98
|
|
|
{ |
|
99
|
84 |
|
return $this->name; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param null|string $name |
|
104
|
|
|
* |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
84 |
|
public function setName(?string $name): self |
|
108
|
|
|
{ |
|
109
|
84 |
|
$this->name = $name; |
|
110
|
|
|
|
|
111
|
84 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
71 |
|
public function getData() |
|
118
|
|
|
{ |
|
119
|
71 |
|
if ($this->isDataEvaluated === false) { |
|
120
|
71 |
|
$this->isDataEvaluated = true; |
|
121
|
|
|
|
|
122
|
71 |
|
if ($this->data instanceof Closure) { |
|
123
|
|
|
/** @var Closure $data */ |
|
124
|
14 |
|
$data = $this->data; |
|
125
|
14 |
|
$this->setData($data()); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
71 |
|
assert(is_array($this->data) === true || is_object($this->data) === true || $this->data === null); |
|
130
|
|
|
|
|
131
|
71 |
|
return $this->data; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param object|array|null|Closure $data |
|
136
|
|
|
* |
|
137
|
|
|
* @return RelationshipObject |
|
138
|
|
|
*/ |
|
139
|
84 |
|
public function setData($data): self |
|
140
|
|
|
{ |
|
141
|
84 |
|
assert(is_array($data) === true || $data instanceof Closure || is_object($data) === true || $data === null); |
|
142
|
|
|
|
|
143
|
84 |
|
$this->data = $data; |
|
144
|
84 |
|
$this->isDataEvaluated = false; |
|
145
|
|
|
|
|
146
|
84 |
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @inheritdoc |
|
151
|
|
|
*/ |
|
152
|
52 |
|
public function getLinks(): array |
|
153
|
|
|
{ |
|
154
|
52 |
|
return $this->links; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param array $links |
|
159
|
|
|
* |
|
160
|
|
|
* @return self |
|
161
|
|
|
*/ |
|
162
|
84 |
|
public function setLinks(array $links): self |
|
163
|
|
|
{ |
|
164
|
84 |
|
$this->links = $links; |
|
165
|
|
|
|
|
166
|
84 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @inheritdoc |
|
171
|
|
|
*/ |
|
172
|
52 |
|
public function getMeta() |
|
173
|
|
|
{ |
|
174
|
52 |
|
if ($this->isMetaEvaluated === false && $this->meta instanceof Closure) { |
|
175
|
1 |
|
$meta = $this->meta; |
|
176
|
1 |
|
$this->meta = $meta(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
52 |
|
$this->isMetaEvaluated = true; |
|
180
|
|
|
|
|
181
|
52 |
|
return $this->meta; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param mixed $meta |
|
186
|
|
|
* |
|
187
|
|
|
* @return self |
|
188
|
|
|
*/ |
|
189
|
84 |
|
public function setMeta($meta): self |
|
190
|
|
|
{ |
|
191
|
84 |
|
$this->meta = $meta; |
|
192
|
84 |
|
$this->isMetaEvaluated = false; |
|
193
|
|
|
|
|
194
|
84 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @inheritdoc |
|
199
|
|
|
*/ |
|
200
|
84 |
|
public function isShowData(): bool |
|
201
|
|
|
{ |
|
202
|
84 |
|
return $this->isShowData; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @inheritdoc |
|
207
|
|
|
*/ |
|
208
|
2 |
|
public function isRoot(): bool |
|
209
|
|
|
{ |
|
210
|
2 |
|
return $this->isRoot; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|