Completed
Push — master ( 561959...44ef42 )
by Alex
02:58
created

ODataEntry::setType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace POData\ObjectModel;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class ODataEntry.
9
 */
10
class ODataEntry
11
{
12
    /**
13
     * Entry id.
14
     *
15
     * @var string
16
     */
17
    public $id;
18
    /**
19
     * Entry Self Link.
20
     *
21
     * @var string
22
     */
23
    public $selfLink;
24
    /**
25
     * Entry title.
26
     *
27
     * @var ODataTitle
28
     */
29
    public $title;
30
    /**
31
     * Entry Edit Link.
32
     *
33
     * @var ODataLink
34
     */
35
    public $editLink;
36
    /**
37
     * Entry Type. This become the value of term attribute of Category element.
38
     *
39
     * @var ODataCategory
40
     */
41
    public $type;
42
    /**
43
     * Instance to hold entity properties.
44
     * Properties corresponding to "m:properties" under content element
45
     * in the case of Non-MLE. For MLE "m:properties" is direct child of entry.
46
     *
47
     * @var ODataPropertyContent
48
     */
49
    public $propertyContent;
50
    /**
51
     * Collection of entry media links (Named Stream Links).
52
     *
53
     * @var array<ODataMediaLink>
54
     */
55
    public $mediaLinks = [];
56
    /**
57
     * media link entry (MLE Link).
58
     *
59
     * @var ODataMediaLink
60
     */
61
    public $mediaLink;
62
    /**
63
     * Collection of navigation links (can be expanded).
64
     *
65
     * @var array<ODataLink>
66
     */
67
    public $links = [];
68
    /**
69
     * Entry ETag.
70
     *
71
     * @var string
72
     */
73
    public $eTag;
74
75
    /**
76
     * True if this is a media link entry.
77
     *
78
     * @var bool
79
     */
80
    public $isMediaLinkEntry;
81
82
    /**
83
     * The name of the resource set this entry belongs to, use in metadata output.
84
     *
85
     * @var string
86
     */
87
    public $resourceSetName;
88
89
    /**
90
     * Last updated timestamp.
91
     *
92
     * @var string
93
     */
94
    public $updated;
95
96
    /**
97
     * Service Base URI.
98
     *
99
     * @var string
100
     */
101
    public $baseURI;
102
103
    /**
104
     * @var AtomObjectModel\AtomContent
105
     */
106
    public $atomContent;
107
108
    /**
109
     * @return \POData\ObjectModel\AtomObjectModel\AtomContent
110
     */
111
    public function getAtomContent()
112
    {
113
        if (!$this->isMediaLinkEntry) {
114
            return new AtomObjectModel\AtomContent(
115
                \POData\Common\MimeTypes::MIME_APPLICATION_XML,
116
                null, $this->propertyContent
117
            );
118
        }
119
        return new AtomObjectModel\AtomContent($this->mediaLink->contentType, $this->mediaLink->srcLink);
120
    }
121
122
    /**
123
     * @param \POData\ObjectModel\AtomObjectModel\AtomContent $atomContent
124
     */
125
    public function setAtomContent(AtomObjectModel\AtomContent $atomContent)
126
    {
127
        $this->setPropertyContent($atomContent->properties);
128
    }
129
130
    /**
131
     * @var AtomObjectModel\AtomAuthor
132
     */
133
    public $atomAuthor;
134
135
    /**
136
     * @return \POData\ObjectModel\AtomObjectModel\AtomAuthor
137
     */
138
    public function getAtomAuthor()
139
    {
140
        return new AtomObjectModel\AtomAuthor();
141
    }
142
143
    /**
144
     * @return null|\POData\ObjectModel\ODataPropertyContent
145
     */
146
    public function getPropertyContent()
147
    {
148
        if (!$this->isMediaLinkEntry) {
149
            return null;
150
        }
151
        return $this->propertyContent;
152
    }
153
154
    /**
155
     * @param \POData\ObjectModel\ODataPropertyContent|null $oDataPropertyContent
156
     */
157
    public function setPropertyContent(ODataPropertyContent $oDataPropertyContent = null)
158
    {
159
        $this->propertyContent = $oDataPropertyContent;
160
    }
161
162
    /**
163
     * @return \POData\ObjectModel\ODataLink
164
     */
165
    public function getEditLink()
166
    {
167
        return $this->editLink;
168
    }
169
170
    public function setType(ODataCategory $type = null)
171
    {
172
        $this->type = $type;
173
        if (null !== $type) {
174
            $rawTerm = $type->term;
175
            $termArray = explode('.', $rawTerm);
176
            $this->resourceSetName = Str::plural($termArray[count($termArray)-1]);
177
        }
178
    }
179
180
    public function getType()
181
    {
182
        return $this->type;
183
    }
184
185
    /**
186
     * @return \POData\ObjectModel\ODataLink[]
187
     */
188
    public function getLinks()
189
    {
190
        return $this->links;
191
    }
192
193
    /**
194
     * @param $links \POData\ObjectModel\ODataLink[]
195
     */
196
    public function setLinks(array $links)
197
    {
198
        $this->links = [];
199
        foreach ($links as $link) {
200
            if ('edit' == $link->name) {
201
                $this->editLink = $link;
202
                $this->resourceSetName = explode('(', $link->url)[0];
203
                continue;
204
            }
205
            if ('http://schemas.microsoft.com/ado/2007/08/dataservices/related' == substr($link->name, 0, 61)
206
            ) {
207
                $this->links[] = $link;
208
                continue;
209
            }
210
        }
211
    }
212
213
    /**
214
     * @return ODataMediaLink[]
215
     */
216
    public function getMediaLinks()
217
    {
218
        return $this->mediaLinks;
219
    }
220
221
    /**
222
     * @param ODataMediaLink[] $mediaLinks
223
     */
224
    public function setMediaLinks(array $mediaLinks)
225
    {
226
        $this->mediaLinks = [];
227
        $editLink = null;
228
        foreach ($mediaLinks as $mediaLink) {
229
            $this->handleMediaLinkEntry($mediaLink, $editLink);
230
        }
231
        $this->correctMediaLinkSrc($editLink);
232
        if (null === $this->mediaLink) {
233
            $this->isMediaLinkEntry = false;
234
        }
235
    }
236
237
    /**
238
     * @param \POData\ObjectModel\ODataMediaLink      $mediaLink
239
     * @param \POData\ObjectModel\ODataMediaLink|null $editLink
240
     */
241
    private function handleMediaLinkEntry(ODataMediaLink $mediaLink, ODataMediaLink &$editLink = null)
242
    {
243
        if ('edit-media' == $mediaLink->rel) {
244
            $this->isMediaLinkEntry = true;
245
            $this->mediaLink = $mediaLink;
246
        }
247
        if (ODataMediaLink::MEDIARESOURCE_BASE == substr($mediaLink->rel, 0, 68)) {
248
            $this->mediaLinks[] = $mediaLink;
249
        }
250
        if ('edit' == $mediaLink->rel) {
251
            $editLink = $mediaLink;
252
        }
253
    }
254
255
    /**
256
     * @param \POData\ObjectModel\ODataMediaLink|null $editLink
257
     */
258
    private function correctMediaLinkSrc(ODataMediaLink $editLink = null)
259
    {
260
        if (null !== $this->mediaLink && null !== $editLink) {
261
            $this->mediaLink->srcLink = $editLink->editLink . $this->mediaLink->editLink;
262
            foreach ($this->mediaLinks as $mediaLink) {
263
                $mediaLink->srcLink = $editLink->editLink . '/' . $mediaLink->name;
264
            }
265
        }
266
    }
267
268
    /**
269
     * @return \POData\ObjectModel\ODataMediaLink
270
     */
271
    public function getMediaLink()
272
    {
273
        return $this->mediaLink;
274
    }
275
276
    public function isOk(&$msg = null)
277
    {
278
        if (!$this->propertyContent instanceof ODataPropertyContent) {
279
            $msg = 'Property content must be instanceof ODataPropertyContent';
280
            return false;
281
        }
282
        if (0 === count($this->propertyContent->properties)) {
283
            $msg = 'Must have at least one property present';
284
            return false;
285
        }
286
287
        return true;
288
    }
289
}
290