Passed
Pull Request — master (#269)
by Christopher
04:24
created

ODataEntry::setUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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
 */
16
class ODataEntry
17
{
18
    /**
19
     * Entry id.
20
     *
21
     * @var string|null
22
     */
23
    public $id;
24
    /**
25
     * Entry Self Link.
26
     *
27
     * @var string|null
28
     */
29
    public $selfLink;
30
    /**
31
     * Entry title.
32
     *
33
     * @var ODataTitle|null
34
     */
35
    public $title;
36
    /**
37
     * Entry Edit Link.
38
     *
39
     * @var ODataLink|null
40
     */
41
    public $editLink;
42
    /**
43
     * Entry Type. This become the value of term attribute of Category element.
44
     *
45
     * @var ODataCategory|null
46
     */
47
    public $type;
48
    /**
49
     * Instance to hold entity properties.
50
     * Properties corresponding to "m:properties" under content element
51
     * in the case of Non-MLE. For MLE "m:properties" is direct child of entry.
52
     *
53
     * @var ODataPropertyContent|null
54
     */
55
    public $propertyContent;
56
    /**
57
     * Collection of entry media links (Named Stream Links).
58
     *
59
     * @var array<ODataMediaLink>
60
     */
61
    public $mediaLinks = [];
62
    /**
63
     * media link entry (MLE Link).
64
     *
65
     * @var ODataMediaLink|null
66
     */
67
    public $mediaLink;
68
    /**
69
     * Collection of navigation links (can be expanded).
70
     *
71
     * @var array<ODataLink>
72
     */
73
    public $links = [];
74
    /**
75
     * Entry ETag.
76
     *
77
     * @var string|null
78
     */
79
    public $eTag;
80
81
    /**
82
     * True if this is a media link entry.
83
     *
84
     * @var bool|null
85
     */
86
    public $isMediaLinkEntry;
87
88
    /**
89
     * The name of the resource set this entry belongs to, use in metadata output.
90
     *
91
     * @var string|null
92
     */
93
    public $resourceSetName;
94
95
    /**
96
     * Last updated timestamp.
97
     *
98
     * @var string|null
99
     */
100
    public $updated;
101
102
    /**
103
     * Service Base URI.
104
     *
105
     * @var string|null
106
     */
107
    public $baseURI;
108
109
    /**
110
     * ODataEntry constructor.
111
     * @param string|null $id
112
     * @param string|null $selfLink
113
     * @param ODataTitle|null $title
114
     * @param ODataLink|null $editLink
115
     * @param ODataCategory|null $type
116
     * @param ODataPropertyContent|null $propertyContent
117
     * @param array $mediaLinks
118
     * @param ODataMediaLink|null $mediaLink
119
     * @param array $links
120
     * @param string|null $eTag
121
     * @param bool|null $isMediaLinkEntry
122
     * @param string|null $resourceSetName
123
     * @param string|null $updated
124
     * @param string|null $baseURI
125
     */
126
    public function __construct(?string $id = null,
127
                                ?string $selfLink = null,
128
                                ?ODataTitle $title = null,
129
                                ?ODataLink $editLink = null,
130
                                ?ODataCategory $type = null,
131
                                ?ODataPropertyContent $propertyContent = null,
132
                                array $mediaLinks = null,
133
                                ?ODataMediaLink $mediaLink = null,
134
                                array $links = null,
135
                                ?string $eTag = null,
136
                                ?bool $isMediaLinkEntry = null,
137
                                ?string $resourceSetName = null,
138
                                ?string $updated = null,
139
                                ?string $baseURI = null)
140
    {
141
        $this->id = $id;
142
        $this->selfLink = $selfLink;
143
        $this->title = $title;
144
        $this->editLink = $editLink;
145
        $this->type = $type;
146
        $this->propertyContent = $propertyContent;
147
        $this->mediaLinks = $mediaLinks;
148
        $this->mediaLink = $mediaLink;
149
        $this->links = $links;
150
        $this->eTag = $eTag;
151
        $this->isMediaLinkEntry = $isMediaLinkEntry;
152
        $this->resourceSetName = $resourceSetName;
153
        $this->updated = $updated;
154
        $this->baseURI = $baseURI;
155
    }
156
157
    /**
158
     * @return string|null
159
     */
160
    public function getId(): ?string
161
    {
162
        return $this->id;
163
    }
164
165
    /**
166
     * @param string|null $id
167
     * @return ODataEntry
168
     */
169
    public function setId(?string $id): ODataEntry
170
    {
171
        $this->id = $id;
172
        return $this;
173
    }
174
175
    /**
176
     * @return string|null
177
     */
178
    public function getSelfLink(): ?string
179
    {
180
        return $this->selfLink;
181
    }
182
183
    /**
184
     * @param string|null $selfLink
185
     * @return ODataEntry
186
     */
187
    public function setSelfLink(?string $selfLink): ODataEntry
188
    {
189
        $this->selfLink = $selfLink;
190
        return $this;
191
    }
192
193
    /**
194
     * @return ODataTitle|null
195
     */
196
    public function getTitle(): ?ODataTitle
197
    {
198
        return $this->title;
199
    }
200
201
    /**
202
     * @param ODataTitle|null $title
203
     * @return ODataEntry
204
     */
205
    public function setTitle(?ODataTitle $title): ODataEntry
206
    {
207
        $this->title = $title;
208
        return $this;
209
    }
210
211
    /**
212
     * @return string|null
213
     */
214
    public function getETag(): ?string
215
    {
216
        return $this->eTag;
217
    }
218
219
    /**
220
     * @param string|null $eTag
221
     * @return ODataEntry
222
     */
223
    public function setETag(?string $eTag): ODataEntry
224
    {
225
        $this->eTag = $eTag;
226
        return $this;
227
    }
228
229
    /**
230
     * @return bool|null
231
     */
232
    public function getIsMediaLinkEntry(): ?bool
233
    {
234
        return $this->isMediaLinkEntry;
235
    }
236
237
    /**
238
     * @param bool|null $isMediaLinkEntry
239
     * @return ODataEntry
240
     */
241
    public function setIsMediaLinkEntry(?bool $isMediaLinkEntry): ODataEntry
242
    {
243
        $this->isMediaLinkEntry = $isMediaLinkEntry;
244
        return $this;
245
    }
246
247
    /**
248
     * @return string|null
249
     */
250
    public function getResourceSetName(): ?string
251
    {
252
        return $this->resourceSetName;
253
    }
254
255
    /**
256
     * @param string|null $resourceSetName
257
     * @return ODataEntry
258
     */
259
    public function setResourceSetName(?string $resourceSetName): ODataEntry
260
    {
261
        $this->resourceSetName = $resourceSetName;
262
        return $this;
263
    }
264
265
    /**
266
     * @return string|null
267
     */
268
    public function getUpdated(): ?string
269
    {
270
        return $this->updated;
271
    }
272
273
    /**
274
     * @param string|null $updated
275
     * @return ODataEntry
276
     */
277
    public function setUpdated(?string $updated): ODataEntry
278
    {
279
        $this->updated = $updated;
280
        return $this;
281
    }
282
283
    /**
284
     * @return string|null
285
     */
286
    public function getBaseURI(): ?string
287
    {
288
        return $this->baseURI;
289
    }
290
291
    /**
292
     * @param string|null $baseURI
293
     * @return ODataEntry
294
     */
295
    public function setBaseURI(?string $baseURI): ODataEntry
296
    {
297
        $this->baseURI = $baseURI;
298
        return $this;
299
    }
300
    /**
301
     * @return AtomContent
302
     */
303
    public function getAtomContent()
304
    {
305
        if (!$this->isMediaLinkEntry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isMediaLinkEntry of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
306
            return new AtomObjectModel\AtomContent(
307
                MimeTypes::MIME_APPLICATION_XML,
308
                null,
309
                $this->propertyContent
310
            );
311
        }
312
        return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink);
313
    }
314
315
    /**
316
     * @param AtomContent $atomContent
317
     */
318
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent)
319
    {
320
        $this->setPropertyContent($atomContent->properties);
321
    }
322
323
    /**
324
     * @return AtomAuthor
325
     */
326
    public function getAtomAuthor()
327
    {
328
        return new AtomObjectModel\AtomAuthor();
329
    }
330
331
    /**
332
     * @return null|ODataPropertyContent
333
     */
334
    public function getPropertyContent()
335
    {
336
        if (!$this->isMediaLinkEntry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isMediaLinkEntry of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
337
            return null;
338
        }
339
        return $this->propertyContent;
340
    }
341
342
    /**
343
     * @param ODataPropertyContent|null $oDataPropertyContent
344
     */
345
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null)
346
    {
347
        $this->propertyContent = $oDataPropertyContent;
348
    }
349
350
    /**
351
     * @return ODataLink
352
     */
353
    public function getEditLink()
354
    {
355
        return $this->editLink;
356
    }
357
358
    /**
359
     * @return ODataCategory
360
     */
361
    public function getType()
362
    {
363
        return $this->type;
364
    }
365
366
    /**
367
     * @param ODataCategory|null $type
368
     */
369
    public function setType(ODataCategory $type = null)
370
    {
371
        $this->type = $type;
372
        if (null !== $type) {
373
            $rawTerm               = $type->getTerm();
374
            $termArray             = explode('.', $rawTerm);
375
            $final                 = $termArray[count($termArray) - 1];
376
            $this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final);
377
        }
378
    }
379
380
    /**
381
     * @return ODataLink[]
382
     */
383
    public function getLinks()
384
    {
385
        return $this->links;
386
    }
387
388
    /**
389
     * @param $links ODataLink[]
390
     */
391
    public function setLinks(array $links)
392
    {
393
        $this->links = [];
394
        foreach ($links as $link) {
395
            if ('edit' == $link->name) {
396
                $this->editLink        = $link;
397
                $this->resourceSetName = explode('(', $link->url)[0];
398
                continue;
399
            }
400
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->name, 0, 61)
401
            ) {
402
                $this->links[] = $link;
403
                continue;
404
            }
405
        }
406
    }
407
408
    /**
409
     * @return ODataMediaLink[]
410
     */
411
    public function getMediaLinks()
412
    {
413
        return $this->mediaLinks;
414
    }
415
416
    /**
417
     * @param ODataMediaLink[] $mediaLinks
418
     */
419
    public function setMediaLinks(array $mediaLinks)
420
    {
421
        $this->mediaLinks = [];
422
        $editLink         = null;
423
        foreach ($mediaLinks as $mediaLink) {
424
            $this->handleMediaLinkEntry($mediaLink, $editLink);
425
        }
426
        $this->correctMediaLinkSrc($editLink);
427
        if (null === $this->mediaLink) {
428
            $this->isMediaLinkEntry = false;
429
        }
430
    }
431
432
    /**
433
     * @param ODataMediaLink      $mediaLink
434
     * @param ODataMediaLink|null $editLink
435
     */
436
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null)
437
    {
438
        if ('edit-media' == $mediaLink->rel) {
439
            $this->isMediaLinkEntry = true;
440
            $this->mediaLink        = $mediaLink;
441
        }
442
        if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) {
443
            $this->mediaLinks[] = $mediaLink;
444
        }
445
        if ('edit' == $mediaLink->rel) {
446
            $editLink = $mediaLink;
447
        }
448
    }
449
450
    /**
451
     * @param ODataMediaLink|null $editLink
452
     */
453
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null)
454
    {
455
        if (null !== $this->mediaLink && null !== $editLink) {
456
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
457
            foreach ($this->mediaLinks as $mediaLink) {
458
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
459
            }
460
        }
461
    }
462
463
    /**
464
     * @return ODataMediaLink
465
     */
466
    public function getMediaLink()
467
    {
468
        return $this->mediaLink;
469
    }
470
471
    /**
472
     * @param  string|null $msg
473
     * @return bool
474
     */
475
    public function isOk(&$msg = null)
476
    {
477
        if (!$this->propertyContent instanceof ODataPropertyContent) {
478
            $msg = 'Property content must be instanceof ODataPropertyContent.';
479
            return false;
480
        }
481
        if (0 === count($this->propertyContent->properties)) {
482
            $msg = 'Must have at least one property present.';
483
            return false;
484
        }
485
486
        return true;
487
    }
488
}
489