1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace POData\ObjectModel; |
6
|
|
|
|
7
|
|
|
use AlgoWeb\ODataMetadata\MetadataManager; |
8
|
|
|
use POData\Common\MimeTypes; |
9
|
|
|
use POData\Common\ODataConstants; |
10
|
|
|
use POData\ObjectModel\AtomObjectModel\AtomAuthor; |
11
|
|
|
use POData\ObjectModel\AtomObjectModel\AtomContent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ODataEntry. |
15
|
|
|
* TODO: the methods should be rearranged to match theorder of the properties. |
16
|
|
|
* TODO: the properties are still public needs a lot of unpicking to work out as type hints maybe wrong. |
17
|
|
|
*/ |
18
|
|
|
class ODataEntry extends ODataContainerBase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Entry Edit Link. |
22
|
|
|
* |
23
|
|
|
* @var ODataLink|null |
24
|
|
|
*/ |
25
|
|
|
public $editLink; |
26
|
|
|
/** |
27
|
|
|
* Entry Type. This become the value of term attribute of Category element. |
28
|
|
|
* |
29
|
|
|
* @var ODataCategory|null |
30
|
|
|
*/ |
31
|
|
|
public $type; |
32
|
|
|
/** |
33
|
|
|
* Instance to hold entity properties. |
34
|
|
|
* Properties corresponding to "m:properties" under content element |
35
|
|
|
* in the case of Non-MLE. For MLE "m:properties" is direct child of entry. |
36
|
|
|
* |
37
|
|
|
* @var ODataPropertyContent|null |
38
|
|
|
*/ |
39
|
|
|
public $propertyContent; |
40
|
|
|
/** |
41
|
|
|
* Collection of entry media links (Named Stream Links). |
42
|
|
|
* |
43
|
|
|
* @var array<ODataMediaLink> |
44
|
|
|
*/ |
45
|
|
|
public $mediaLinks = []; |
46
|
|
|
/** |
47
|
|
|
* media link entry (MLE Link). |
48
|
|
|
* |
49
|
|
|
* @var ODataMediaLink|null |
50
|
|
|
*/ |
51
|
|
|
public $mediaLink; |
52
|
|
|
/** |
53
|
|
|
* Collection of navigation links (can be expanded). |
54
|
|
|
* |
55
|
|
|
* @var array<ODataLink> |
56
|
|
|
*/ |
57
|
|
|
public $links = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Entry ETag. |
61
|
|
|
* |
62
|
|
|
* @var string|null |
63
|
|
|
*/ |
64
|
|
|
public $eTag; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* True if this is a media link entry. |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
public $isMediaLinkEntry; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The name of the resource set this entry belongs to, use in metadata output. |
75
|
|
|
* |
76
|
|
|
* @var string|null |
77
|
|
|
*/ |
78
|
|
|
public $resourceSetName; |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* ODataEntry constructor. |
83
|
|
|
* @param string|null $id |
84
|
|
|
* @param ODataLink|null $selfLink |
85
|
|
|
* @param ODataTitle|null $title |
86
|
|
|
* @param ODataLink|null $editLink |
87
|
|
|
* @param ODataCategory|null $type |
88
|
|
|
* @param ODataPropertyContent|null $propertyContent |
89
|
|
|
* @param array $mediaLinks |
90
|
|
|
* @param ODataMediaLink|null $mediaLink |
91
|
|
|
* @param array $links |
92
|
|
|
* @param string|null $eTag |
93
|
|
|
* @param bool $isMediaLinkEntry |
94
|
|
|
* @param string|null $resourceSetName |
95
|
|
|
* @param string|null $updated |
96
|
|
|
* @param string|null $baseURI |
97
|
|
|
*/ |
98
|
|
|
public function __construct( |
99
|
|
|
?string $id = null, |
100
|
|
|
?ODataLink $selfLink = null, |
101
|
|
|
?ODataTitle $title = null, |
102
|
|
|
?ODataLink $editLink = null, |
103
|
|
|
?ODataCategory $type = null, |
104
|
|
|
?ODataPropertyContent $propertyContent = null, |
105
|
|
|
array $mediaLinks = [], |
106
|
|
|
?ODataMediaLink $mediaLink = null, |
107
|
|
|
array $links = [], |
108
|
|
|
?string $eTag = null, |
109
|
|
|
bool $isMediaLinkEntry = false, |
110
|
|
|
?string $resourceSetName = null, |
111
|
|
|
?string $updated = null, |
112
|
|
|
?string $baseURI = null |
113
|
|
|
) { |
114
|
|
|
parent::__construct($id, $title, $selfLink, $updated, $baseURI); |
115
|
|
|
$this |
116
|
|
|
->setType($type) |
117
|
|
|
->setPropertyContent($propertyContent) |
118
|
|
|
->setMediaLinks($mediaLinks) |
119
|
|
|
->setLinks($links) |
120
|
|
|
->setETag($eTag) |
121
|
|
|
->setIsMediaLinkEntry($isMediaLinkEntry) |
122
|
|
|
->setResourceSetName($resourceSetName); |
123
|
|
|
$this->editLink = $editLink; |
124
|
|
|
$this->mediaLink = $mediaLink; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
public function getETag(): ?string |
132
|
|
|
{ |
133
|
|
|
return $this->eTag; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string|null $eTag |
138
|
|
|
* @return ODataEntry |
139
|
|
|
*/ |
140
|
|
|
public function setETag(?string $eTag): ODataEntry |
141
|
|
|
{ |
142
|
|
|
$this->eTag = $eTag; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function getIsMediaLinkEntry(): bool |
150
|
|
|
{ |
151
|
|
|
return $this->isMediaLinkEntry; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param bool $isMediaLinkEntry |
156
|
|
|
* @return ODataEntry |
157
|
|
|
*/ |
158
|
|
|
public function setIsMediaLinkEntry(bool $isMediaLinkEntry): ODataEntry |
159
|
|
|
{ |
160
|
|
|
$this->isMediaLinkEntry = $isMediaLinkEntry; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string|null |
166
|
|
|
*/ |
167
|
|
|
public function getResourceSetName(): ?string |
168
|
|
|
{ |
169
|
|
|
return $this->resourceSetName; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string|null $resourceSetName |
174
|
|
|
* @return ODataEntry |
175
|
|
|
*/ |
176
|
|
|
public function setResourceSetName(?string $resourceSetName): ODataEntry |
177
|
|
|
{ |
178
|
|
|
$this->resourceSetName = $resourceSetName; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return AtomContent |
184
|
|
|
*/ |
185
|
|
|
public function getAtomContent() |
186
|
|
|
{ |
187
|
|
|
if (true !== $this->isMediaLinkEntry) { |
188
|
|
|
return new AtomObjectModel\AtomContent( |
189
|
|
|
MimeTypes::MIME_APPLICATION_XML, |
190
|
|
|
null, |
191
|
|
|
$this->propertyContent |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param AtomContent $atomContent |
199
|
|
|
* @return ODataEntry |
200
|
|
|
*/ |
201
|
|
|
public function setAtomContent(AtomObjectModel\AtomContent $atomContent): self |
202
|
|
|
{ |
203
|
|
|
$this->setPropertyContent($atomContent->properties); |
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return AtomAuthor |
209
|
|
|
*/ |
210
|
|
|
public function getAtomAuthor(): AtomAuthor |
211
|
|
|
{ |
212
|
|
|
return new AtomObjectModel\AtomAuthor(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return null|ODataPropertyContent |
217
|
|
|
*/ |
218
|
|
|
public function getPropertyContent(): ?ODataPropertyContent |
219
|
|
|
{ |
220
|
|
|
if (true !== $this->isMediaLinkEntry) { |
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
return $this->propertyContent; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param ODataPropertyContent|null $oDataPropertyContent |
228
|
|
|
* @return ODataEntry |
229
|
|
|
*/ |
230
|
|
|
public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null): self |
231
|
|
|
{ |
232
|
|
|
$this->propertyContent = $oDataPropertyContent; |
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return ODataLink|null |
238
|
|
|
*/ |
239
|
|
|
public function getEditLink(): ?ODataLink |
240
|
|
|
{ |
241
|
|
|
return $this->editLink; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return ODataCategory|null |
246
|
|
|
*/ |
247
|
|
|
public function getType(): ?ODataCategory |
248
|
|
|
{ |
249
|
|
|
return $this->type; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param ODataCategory|null $type |
254
|
|
|
* @return ODataEntry |
255
|
|
|
*/ |
256
|
|
|
public function setType(ODataCategory $type = null): self |
257
|
|
|
{ |
258
|
|
|
$this->type = $type; |
259
|
|
|
if (null !== $type) { |
260
|
|
|
$rawTerm = $type->getTerm(); |
261
|
|
|
$termArray = explode('.', $rawTerm); |
262
|
|
|
$final = $termArray[count($termArray) - 1]; |
263
|
|
|
$this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final); |
264
|
|
|
} |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return ODataLink[] |
270
|
|
|
*/ |
271
|
|
|
public function getLinks(): array |
272
|
|
|
{ |
273
|
|
|
return $this->links; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param $links ODataLink[] |
278
|
|
|
* @return ODataEntry |
279
|
|
|
*/ |
280
|
|
|
public function setLinks(array $links): self |
281
|
|
|
{ |
282
|
|
|
$this->links = []; |
283
|
|
|
foreach ($links as $link) { |
284
|
|
|
if ('edit' == $link->getName()) { |
285
|
|
|
$this->editLink = $link; |
286
|
|
|
$this->resourceSetName = explode('(', $link->getUrl())[0]; |
287
|
|
|
continue; |
288
|
|
|
} |
289
|
|
|
if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->getName(), 0, 61) |
290
|
|
|
) { |
291
|
|
|
$this->links[] = $link; |
292
|
|
|
continue; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return ODataMediaLink[] |
300
|
|
|
*/ |
301
|
|
|
public function getMediaLinks(): array |
302
|
|
|
{ |
303
|
|
|
return $this->mediaLinks; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param ODataMediaLink[] $mediaLinks |
308
|
|
|
* @return ODataEntry |
309
|
|
|
*/ |
310
|
|
|
public function setMediaLinks(array $mediaLinks): self |
311
|
|
|
{ |
312
|
|
|
$this->mediaLinks = []; |
313
|
|
|
$editLink = null; |
314
|
|
|
foreach ($mediaLinks as $mediaLink) { |
315
|
|
|
$this->handleMediaLinkEntry($mediaLink, $editLink); |
316
|
|
|
} |
317
|
|
|
$this->correctMediaLinkSrc($editLink); |
318
|
|
|
if (null === $this->mediaLink) { |
319
|
|
|
$this->isMediaLinkEntry = false; |
320
|
|
|
} |
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param ODataMediaLink $mediaLink |
326
|
|
|
* @param ODataMediaLink|null $editLink |
327
|
|
|
*/ |
328
|
|
|
private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null): void |
329
|
|
|
{ |
330
|
|
|
if ('edit-media' == $mediaLink->rel) { |
331
|
|
|
$this->isMediaLinkEntry = true; |
332
|
|
|
$this->mediaLink = $mediaLink; |
333
|
|
|
} |
334
|
|
|
if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) { |
335
|
|
|
$this->mediaLinks[] = $mediaLink; |
336
|
|
|
} |
337
|
|
|
if ('edit' == $mediaLink->rel) { |
338
|
|
|
$editLink = $mediaLink; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param ODataMediaLink|null $editLink |
344
|
|
|
*/ |
345
|
|
|
private function correctMediaLinkSrc(ODataMediaLink $editLink = null): void |
346
|
|
|
{ |
347
|
|
|
if (null !== $this->mediaLink && null !== $editLink) { |
348
|
|
|
$this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink; |
349
|
|
|
foreach ($this->mediaLinks as $mediaLink) { |
350
|
|
|
$mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name; |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return ODataMediaLink|null |
357
|
|
|
*/ |
358
|
|
|
public function getMediaLink(): ?ODataMediaLink |
359
|
|
|
{ |
360
|
|
|
return $this->mediaLink; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string|null $msg |
365
|
|
|
* @return bool |
366
|
|
|
*/ |
367
|
|
|
public function isOk(&$msg = null): bool |
368
|
|
|
{ |
369
|
|
|
if (!$this->propertyContent instanceof ODataPropertyContent) { |
370
|
|
|
$msg = 'Property content must be instanceof ODataPropertyContent.'; |
371
|
|
|
return false; |
372
|
|
|
} |
373
|
|
|
if (0 === count($this->propertyContent)) { |
374
|
|
|
$msg = 'Must have at least one property present.'; |
375
|
|
|
return false; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return true; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|