Passed
Pull Request — master (#269)
by Alex
03:43
created

ODataEntry::getResourceSetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
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
 * 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
19
{
20
    /**
21
     * Entry id.
22
     *
23
     * @var string|null
24
     */
25
    public $id;
26
    /**
27
     * Entry Self Link.
28
     *
29
     * @var string|null
30
     */
31
    public $selfLink;
32
    /**
33
     * Entry title.
34
     *
35
     * @var ODataTitle|null
36
     */
37
    public $title;
38
    /**
39
     * Entry Edit Link.
40
     *
41
     * @var ODataLink|null
42
     */
43
    public $editLink;
44
    /**
45
     * Entry Type. This become the value of term attribute of Category element.
46
     *
47
     * @var ODataCategory|null
48
     */
49
    public $type;
50
    /**
51
     * Instance to hold entity properties.
52
     * Properties corresponding to "m:properties" under content element
53
     * in the case of Non-MLE. For MLE "m:properties" is direct child of entry.
54
     *
55
     * @var ODataPropertyContent|null
56
     */
57
    public $propertyContent;
58
    /**
59
     * Collection of entry media links (Named Stream Links).
60
     *
61
     * @var array<ODataMediaLink>
62
     */
63
    public $mediaLinks = [];
64
    /**
65
     * media link entry (MLE Link).
66
     *
67
     * @var ODataMediaLink|null
68
     */
69
    public $mediaLink;
70
    /**
71
     * Collection of navigation links (can be expanded).
72
     *
73
     * @var array<ODataLink>
74
     */
75
    public $links = [];
76
    /**
77
     * Entry ETag.
78
     *
79
     * @var string|null
80
     */
81
    public $eTag;
82
83
    /**
84
     * True if this is a media link entry.
85
     *
86
     * @var bool|null
87
     */
88
    public $isMediaLinkEntry;
89
90
    /**
91
     * The name of the resource set this entry belongs to, use in metadata output.
92
     *
93
     * @var string|null
94
     */
95
    public $resourceSetName;
96
97
    /**
98
     * Last updated timestamp.
99
     *
100
     * @var string|null
101
     */
102
    public $updated;
103
104
    /**
105
     * Service Base URI.
106
     *
107
     * @var string|null
108
     */
109
    public $baseURI;
110
111
    /**
112
     * ODataEntry constructor.
113
     * @param string|null $id
114
     * @param string|null $selfLink
115
     * @param ODataTitle|null $title
116
     * @param ODataLink|null $editLink
117
     * @param ODataCategory|null $type
118
     * @param ODataPropertyContent|null $propertyContent
119
     * @param array $mediaLinks
120
     * @param ODataMediaLink|null $mediaLink
121
     * @param array $links
122
     * @param string|null $eTag
123
     * @param bool|null $isMediaLinkEntry
124
     * @param string|null $resourceSetName
125
     * @param string|null $updated
126
     * @param string|null $baseURI
127
     */
128
    public function __construct(
129
        ?string $id = null,
130
        ?string $selfLink = null,
131
        ?ODataTitle $title = null,
132
        ?ODataLink $editLink = null,
133
        ?ODataCategory $type = null,
134
        ?ODataPropertyContent $propertyContent = null,
135
        array $mediaLinks = [],
136
        ?ODataMediaLink $mediaLink = null,
137
        array $links = [],
138
        ?string $eTag = null,
139
        ?bool $isMediaLinkEntry = null,
140
        ?string $resourceSetName = null,
141
        ?string $updated = null,
142
        ?string $baseURI = null
143
    ) {
144
        $this->id = $id;
145
        $this->selfLink = $selfLink;
146
        $this->title = $title;
147
        $this->editLink = $editLink;
148
        $this->type = $type;
149
        $this->propertyContent = $propertyContent;
150
        $this->mediaLinks = $mediaLinks;
151
        $this->mediaLink = $mediaLink;
152
        $this->links = $links;
153
        $this->eTag = $eTag;
154
        $this->isMediaLinkEntry = $isMediaLinkEntry;
155
        $this->resourceSetName = $resourceSetName;
156
        $this->updated = $updated;
157
        $this->baseURI = $baseURI;
158
    }
159
160
    /**
161
     * @return string|null
162
     */
163
    public function getId(): ?string
164
    {
165
        return $this->id;
166
    }
167
168
    /**
169
     * @param string|null $id
170
     * @return ODataEntry
171
     */
172
    public function setId(?string $id): ODataEntry
173
    {
174
        $this->id = $id;
175
        return $this;
176
    }
177
178
    /**
179
     * @return string|null
180
     */
181
    public function getSelfLink(): ?string
182
    {
183
        return $this->selfLink;
184
    }
185
186
    /**
187
     * @param string|null $selfLink
188
     * @return ODataEntry
189
     */
190
    public function setSelfLink(?string $selfLink): ODataEntry
191
    {
192
        $this->selfLink = $selfLink;
193
        return $this;
194
    }
195
196
    /**
197
     * @return ODataTitle|null
198
     */
199
    public function getTitle(): ?ODataTitle
200
    {
201
        return $this->title;
202
    }
203
204
    /**
205
     * @param ODataTitle|null $title
206
     * @return ODataEntry
207
     */
208
    public function setTitle(?ODataTitle $title): ODataEntry
209
    {
210
        $this->title = $title;
211
        return $this;
212
    }
213
214
    /**
215
     * @return string|null
216
     */
217
    public function getETag(): ?string
218
    {
219
        return $this->eTag;
220
    }
221
222
    /**
223
     * @param string|null $eTag
224
     * @return ODataEntry
225
     */
226
    public function setETag(?string $eTag): ODataEntry
227
    {
228
        $this->eTag = $eTag;
229
        return $this;
230
    }
231
232
    /**
233
     * @return bool|null
234
     */
235
    public function getIsMediaLinkEntry(): ?bool
236
    {
237
        return $this->isMediaLinkEntry;
238
    }
239
240
    /**
241
     * @param bool|null $isMediaLinkEntry
242
     * @return ODataEntry
243
     */
244
    public function setIsMediaLinkEntry(?bool $isMediaLinkEntry): ODataEntry
245
    {
246
        $this->isMediaLinkEntry = $isMediaLinkEntry;
247
        return $this;
248
    }
249
250
    /**
251
     * @return string|null
252
     */
253
    public function getResourceSetName(): ?string
254
    {
255
        return $this->resourceSetName;
256
    }
257
258
    /**
259
     * @param string|null $resourceSetName
260
     * @return ODataEntry
261
     */
262
    public function setResourceSetName(?string $resourceSetName): ODataEntry
263
    {
264
        $this->resourceSetName = $resourceSetName;
265
        return $this;
266
    }
267
268
    /**
269
     * @return string|null
270
     */
271
    public function getUpdated(): ?string
272
    {
273
        return $this->updated;
274
    }
275
276
    /**
277
     * @param string|null $updated
278
     * @return ODataEntry
279
     */
280
    public function setUpdated(?string $updated): ODataEntry
281
    {
282
        $this->updated = $updated;
283
        return $this;
284
    }
285
286
    /**
287
     * @return string|null
288
     */
289
    public function getBaseURI(): ?string
290
    {
291
        return $this->baseURI;
292
    }
293
294
    /**
295
     * @param string|null $baseURI
296
     * @return ODataEntry
297
     */
298
    public function setBaseURI(?string $baseURI): ODataEntry
299
    {
300
        $this->baseURI = $baseURI;
301
        return $this;
302
    }
303
    /**
304
     * @return AtomContent
305
     */
306
    public function getAtomContent()
307
    {
308
        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...
309
            return new AtomObjectModel\AtomContent(
310
                MimeTypes::MIME_APPLICATION_XML,
311
                null,
312
                $this->propertyContent
313
            );
314
        }
315
        return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink);
316
    }
317
318
    /**
319
     * @param AtomContent $atomContent
320
     */
321
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent)
322
    {
323
        $this->setPropertyContent($atomContent->properties);
324
    }
325
326
    /**
327
     * @return AtomAuthor
328
     */
329
    public function getAtomAuthor()
330
    {
331
        return new AtomObjectModel\AtomAuthor();
332
    }
333
334
    /**
335
     * @return null|ODataPropertyContent
336
     */
337
    public function getPropertyContent()
338
    {
339
        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...
340
            return null;
341
        }
342
        return $this->propertyContent;
343
    }
344
345
    /**
346
     * @param ODataPropertyContent|null $oDataPropertyContent
347
     */
348
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null)
349
    {
350
        $this->propertyContent = $oDataPropertyContent;
351
    }
352
353
    /**
354
     * @return ODataLink
355
     */
356
    public function getEditLink()
357
    {
358
        return $this->editLink;
359
    }
360
361
    /**
362
     * @return ODataCategory
363
     */
364
    public function getType()
365
    {
366
        return $this->type;
367
    }
368
369
    /**
370
     * @param ODataCategory|null $type
371
     */
372
    public function setType(ODataCategory $type = null)
373
    {
374
        $this->type = $type;
375
        if (null !== $type) {
376
            $rawTerm               = $type->getTerm();
377
            $termArray             = explode('.', $rawTerm);
378
            $final                 = $termArray[count($termArray) - 1];
379
            $this->resourceSetName = MetadataManager::getResourceSetNameFromResourceType($final);
380
        }
381
    }
382
383
    /**
384
     * @return ODataLink[]
385
     */
386
    public function getLinks()
387
    {
388
        return $this->links;
389
    }
390
391
    /**
392
     * @param $links ODataLink[]
393
     */
394
    public function setLinks(array $links)
395
    {
396
        $this->links = [];
397
        foreach ($links as $link) {
398
            if ('edit' == $link->getName()) {
399
                $this->editLink        = $link;
400
                $this->resourceSetName = explode('(', $link->url)[0];
401
                continue;
402
            }
403
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->getName(), 0, 61)
404
            ) {
405
                $this->links[] = $link;
406
                continue;
407
            }
408
        }
409
    }
410
411
    /**
412
     * @return ODataMediaLink[]
413
     */
414
    public function getMediaLinks()
415
    {
416
        return $this->mediaLinks;
417
    }
418
419
    /**
420
     * @param ODataMediaLink[] $mediaLinks
421
     */
422
    public function setMediaLinks(array $mediaLinks)
423
    {
424
        $this->mediaLinks = [];
425
        $editLink         = null;
426
        foreach ($mediaLinks as $mediaLink) {
427
            $this->handleMediaLinkEntry($mediaLink, $editLink);
428
        }
429
        $this->correctMediaLinkSrc($editLink);
430
        if (null === $this->mediaLink) {
431
            $this->isMediaLinkEntry = false;
432
        }
433
    }
434
435
    /**
436
     * @param ODataMediaLink      $mediaLink
437
     * @param ODataMediaLink|null $editLink
438
     */
439
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null)
440
    {
441
        if ('edit-media' == $mediaLink->rel) {
442
            $this->isMediaLinkEntry = true;
443
            $this->mediaLink        = $mediaLink;
444
        }
445
        if (ODataConstants::ATOM_MEDIA_RESOURCE_RELATION_ATTRIBUTE_VALUE == substr($mediaLink->rel, 0, 68)) {
446
            $this->mediaLinks[] = $mediaLink;
447
        }
448
        if ('edit' == $mediaLink->rel) {
449
            $editLink = $mediaLink;
450
        }
451
    }
452
453
    /**
454
     * @param ODataMediaLink|null $editLink
455
     */
456
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null)
457
    {
458
        if (null !== $this->mediaLink && null !== $editLink) {
459
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
460
            foreach ($this->mediaLinks as $mediaLink) {
461
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
462
            }
463
        }
464
    }
465
466
    /**
467
     * @return ODataMediaLink
468
     */
469
    public function getMediaLink()
470
    {
471
        return $this->mediaLink;
472
    }
473
474
    /**
475
     * @param  string|null $msg
476
     * @return bool
477
     */
478
    public function isOk(&$msg = null)
479
    {
480
        if (!$this->propertyContent instanceof ODataPropertyContent) {
481
            $msg = 'Property content must be instanceof ODataPropertyContent.';
482
            return false;
483
        }
484
        if (0 === count($this->propertyContent->properties)) {
485
            $msg = 'Must have at least one property present.';
486
            return false;
487
        }
488
489
        return true;
490
    }
491
}
492