1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CloudPlayDev\ConfluenceClient\Entity; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use CloudPlayDev\ConfluenceClient\Api\Content; |
8
|
|
|
use CloudPlayDev\ConfluenceClient\Exception\HydrationException; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
abstract class AbstractContent implements Hydratable |
12
|
|
|
{ |
13
|
|
|
private ?int $id = null; |
14
|
|
|
private ?string $title = null; |
15
|
|
|
private ?string $space = null; |
16
|
|
|
|
17
|
|
|
private ?string $content = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int[] |
21
|
|
|
*/ |
22
|
|
|
private array $ancestors = []; |
23
|
|
|
|
24
|
|
|
private int $version = 1; |
25
|
|
|
/** |
26
|
|
|
* @var array<string, string> $children |
27
|
|
|
*/ |
28
|
|
|
private array $children = []; |
29
|
|
|
private ?string $url = null; |
30
|
|
|
protected string $type = Content::CONTENT_TYPE_GLOBAL; |
31
|
|
|
|
32
|
|
|
private ?int $containerId = null; |
33
|
|
|
private string $containerType = Content::CONTENT_TYPE_PAGE; |
34
|
|
|
|
35
|
|
|
private bool $isLatest = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getType(): string |
41
|
|
|
{ |
42
|
|
|
return $this->type; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $type |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function setType(string $type): self |
50
|
|
|
{ |
51
|
|
|
$this->type = $type; |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return int|null |
57
|
|
|
*/ |
58
|
|
|
public function getId(): ?int |
59
|
|
|
{ |
60
|
|
|
return $this->id; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $id |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
public function setId(int $id): self |
68
|
|
|
{ |
69
|
|
|
$this->id = $id; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getTitle(): ?string |
74
|
|
|
{ |
75
|
|
|
return $this->title; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $title |
80
|
|
|
* @return self |
81
|
|
|
*/ |
82
|
|
|
public function setTitle(string $title): self |
83
|
|
|
{ |
84
|
|
|
$this->title = $title; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return null|string |
90
|
|
|
*/ |
91
|
|
|
public function getSpace(): ?string |
92
|
|
|
{ |
93
|
|
|
return $this->space; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string|null $space |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
|
|
public function setSpace(?string $space): self |
101
|
|
|
{ |
102
|
|
|
$this->space = $space; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return null|string |
108
|
|
|
*/ |
109
|
|
|
public function getContent(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->content; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $content |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
|
|
public function setContent(string $content): self |
119
|
|
|
{ |
120
|
|
|
$this->content = $content; |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function getVersion(): int |
128
|
|
|
{ |
129
|
|
|
return $this->version; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int $version |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
|
|
public function setVersion(int $version): self |
137
|
|
|
{ |
138
|
|
|
$this->version = $version; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return array<string, string> |
144
|
|
|
*/ |
145
|
|
|
public function getChildren(): array |
146
|
|
|
{ |
147
|
|
|
return $this->children; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array<string, string> $children |
152
|
|
|
* @return self |
153
|
|
|
*/ |
154
|
|
|
public function setChildren(array $children): self |
155
|
|
|
{ |
156
|
|
|
$this->children = $children; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return null|string |
162
|
|
|
*/ |
163
|
|
|
public function getUrl(): ?string |
164
|
|
|
{ |
165
|
|
|
return $this->url; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $url |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
public function setUrl(string $url): self |
173
|
|
|
{ |
174
|
|
|
$this->url = $url; |
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $comment |
180
|
|
|
* @return ContentComment |
181
|
|
|
*/ |
182
|
|
|
public function createComment(string $comment): ContentComment |
183
|
|
|
{ |
184
|
|
|
$contentComment = new ContentComment(); |
185
|
|
|
$contentComment->setContainerId($this->getId()); |
186
|
|
|
$contentComment->setContainerType($this->getType()); |
187
|
|
|
$contentComment->setContent($comment); |
188
|
|
|
$contentComment->setSpace($this->getSpace()); |
189
|
|
|
return $contentComment; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $title |
194
|
|
|
* @param string $body |
195
|
|
|
* @return ContentPage |
196
|
|
|
*/ |
197
|
|
|
public function createSubpage(string $title, string $body): ContentPage |
198
|
|
|
{ |
199
|
|
|
$contentPage = new ContentPage(); |
200
|
|
|
$contentPage->setContent($body); |
201
|
|
|
$contentPage->setTitle($title); |
202
|
|
|
$contentPage->setSpace($this->getSpace()); |
203
|
|
|
if ($this->id) { |
|
|
|
|
204
|
|
|
$contentPage->addAncestor($this->id); |
205
|
|
|
} |
206
|
|
|
return $contentPage; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return int|null |
211
|
|
|
*/ |
212
|
|
|
public function getContainerId(): ?int |
213
|
|
|
{ |
214
|
|
|
return $this->containerId; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param int|null $containerId |
219
|
|
|
*/ |
220
|
|
|
public function setContainerId(?int $containerId): void |
221
|
|
|
{ |
222
|
|
|
$this->containerId = $containerId; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getContainerType(): string |
229
|
|
|
{ |
230
|
|
|
return $this->containerType; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $containerType |
235
|
|
|
*/ |
236
|
|
|
public function setContainerType(string $containerType): void |
237
|
|
|
{ |
238
|
|
|
$this->containerType = $containerType; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return int[] |
243
|
|
|
*/ |
244
|
|
|
public function getAncestors(): array |
245
|
|
|
{ |
246
|
|
|
return $this->ancestors; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param int[] $ancestors |
251
|
|
|
* @return self |
252
|
|
|
*/ |
253
|
|
|
public function setAncestors(array $ancestors): self |
254
|
|
|
{ |
255
|
|
|
$this->ancestors = $ancestors; |
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param int $id |
261
|
|
|
* @return self |
262
|
|
|
*/ |
263
|
|
|
public function addAncestor(int $id): self |
264
|
|
|
{ |
265
|
|
|
$this->ancestors[] = $id; |
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param mixed[] $data |
271
|
|
|
* @return AbstractContent|ContentPage|ContentComment |
272
|
|
|
* @throws HydrationException |
273
|
|
|
*/ |
274
|
|
|
public static function load(array $data): ContentComment|AbstractContent|ContentPage |
275
|
|
|
{ |
276
|
|
|
/* handle older content versions */ |
277
|
|
|
if(isset($data['content'], $data['when'])) { |
278
|
|
|
return self::load($data['content']); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
Assert::true(isset($data['id'], |
282
|
|
|
$data['type'], |
283
|
|
|
$data['title'], |
284
|
|
|
$data['_links']['self'])); |
285
|
|
|
Assert::string($data['type']); |
286
|
|
|
|
287
|
|
|
$content = match ($data['type']) { |
288
|
|
|
Content::CONTENT_TYPE_PAGE => new ContentPage(), |
289
|
|
|
Content::CONTENT_TYPE_COMMENT => new ContentComment(), |
290
|
|
|
default => throw new HydrationException('Invalid content type: ' . $data['type']), |
291
|
|
|
}; |
292
|
|
|
|
293
|
|
|
$content->setId((int)$data['id']); |
294
|
|
|
$content->setTitle((string)$data['title']); |
295
|
|
|
$content->setUrl((string)$data['_links']['self']); |
296
|
|
|
if (isset($data['space']['key'])) { |
297
|
|
|
$content->setSpace((string)$data['space']['key']); |
298
|
|
|
} |
299
|
|
|
if (isset($data['version']['number'])) { |
300
|
|
|
$content->setVersion((int)$data['version']['number']); |
301
|
|
|
} |
302
|
|
|
if (isset($data['body']['storage']['value'])) { |
303
|
|
|
Assert::isArray($data['body']['storage']); |
304
|
|
|
$content->setContent((string)$data['body']['storage']['value']); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
if(isset($data['status'])) { |
308
|
|
|
Assert::string($data['status']); |
309
|
|
|
$content->setLatest($data['status'] === 'current'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $content; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function isLatest(): bool |
316
|
|
|
{ |
317
|
|
|
return $this->isLatest; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
protected function setLatest(bool $isLatest): AbstractContent |
321
|
|
|
{ |
322
|
|
|
$this->isLatest = $isLatest; |
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: