1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace POData\ObjectModel; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ODataLink represents an OData Navigation Link. |
9
|
|
|
*/ |
10
|
|
|
class ODataLink |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Name of the link. This becomes last segment of rel attribute value. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $name; |
18
|
|
|
/** |
19
|
|
|
* Title of the link. This become value of title attribute. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $title; |
24
|
|
|
/** |
25
|
|
|
* Type of link. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $type; |
30
|
|
|
/** |
31
|
|
|
* Url to the navigation property. This become value of href attribute. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
private $url; |
36
|
|
|
/** |
37
|
|
|
* Checks is Expand result contains single entity or collection of |
38
|
|
|
* entities i.e. feed. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $isCollection; |
43
|
|
|
/** |
44
|
|
|
* The expanded result. This becomes the inline content of the link. |
45
|
|
|
* |
46
|
|
|
* @var ODataExpandedResult|null |
47
|
|
|
*/ |
48
|
|
|
private $expandedResult; |
49
|
|
|
/** |
50
|
|
|
* True if Link is Expanded, False if not. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $isExpanded; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* ODataLink constructor. |
58
|
|
|
* @param string $name |
59
|
|
|
* @param string $title |
60
|
|
|
* @param string $type |
61
|
|
|
* @param string $url |
62
|
|
|
* @param bool $isCollection |
63
|
|
|
* @param ODataExpandedResult|null $expandedResult |
64
|
|
|
* @param bool|null $isExpanded |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
string $name = null, |
68
|
|
|
string $title = null, |
69
|
|
|
string $type = null, |
70
|
|
|
string $url = null, |
71
|
|
|
bool $isCollection = null, |
72
|
|
|
ODataExpandedResult $expandedResult = null, |
73
|
|
|
bool $isExpanded = null |
74
|
|
|
) { |
75
|
|
|
$this |
76
|
|
|
->setName($name) |
77
|
|
|
->setTitle($title) |
78
|
|
|
->setType($type) |
79
|
|
|
->setUrl($url) |
80
|
|
|
->setIsCollection($isCollection) |
81
|
|
|
->setExpandedResult($expandedResult) |
82
|
|
|
->setIsExpanded($isExpanded); |
83
|
|
|
} |
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getName(): ?string |
88
|
|
|
{ |
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @return ODataLink |
95
|
|
|
*/ |
96
|
|
|
public function setName(?string $name): ODataLink |
97
|
|
|
{ |
98
|
|
|
$this->name = $name; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getTitle(): ?string |
106
|
|
|
{ |
107
|
|
|
return $this->title; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $title |
112
|
|
|
* @return ODataLink |
113
|
|
|
*/ |
114
|
|
|
public function setTitle(?string $title): ODataLink |
115
|
|
|
{ |
116
|
|
|
$this->title = $title; |
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string|null |
122
|
|
|
*/ |
123
|
|
|
public function getType(): ?string |
124
|
|
|
{ |
125
|
|
|
return $this->type; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $type |
130
|
|
|
* @return ODataLink |
131
|
|
|
*/ |
132
|
|
|
public function setType(?string $type): ODataLink |
133
|
|
|
{ |
134
|
|
|
$this->type = $type; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getUrl(): ?string |
142
|
|
|
{ |
143
|
|
|
return $this->url; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $url |
148
|
|
|
* @return ODataLink |
149
|
|
|
*/ |
150
|
|
|
public function setUrl(?string $url): ODataLink |
151
|
|
|
{ |
152
|
|
|
$this->url = $url; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isCollection(): ?bool |
160
|
|
|
{ |
161
|
|
|
return $this->isCollection; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param bool $isCollection |
166
|
|
|
* @return ODataLink |
167
|
|
|
*/ |
168
|
|
|
public function setIsCollection(?bool $isCollection): ODataLink |
169
|
|
|
{ |
170
|
|
|
$this->isCollection = $isCollection; |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function isExpanded(): ?bool |
178
|
|
|
{ |
179
|
|
|
return $this->isExpanded; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param bool $isExpanded |
184
|
|
|
* @return ODataLink |
185
|
|
|
*/ |
186
|
|
|
public function setIsExpanded(?bool $isExpanded): ODataLink |
187
|
|
|
{ |
188
|
|
|
$this->isExpanded = $isExpanded; |
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return null|ODataExpandedResult |
194
|
|
|
*/ |
195
|
|
|
public function getExpandedResult(): ?ODataExpandedResult |
196
|
|
|
{ |
197
|
|
|
if (!$this->isExpanded) { |
198
|
|
|
return null; |
199
|
|
|
} |
200
|
|
|
if ($this->expandedResult === null) { |
201
|
|
|
return null; |
202
|
|
|
} |
203
|
|
|
return $this->expandedResult; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param ODataExpandedResult $eResult |
208
|
|
|
* @return ODataLink |
209
|
|
|
*/ |
210
|
|
|
public function setExpandedResult(?ODataExpandedResult $eResult): self |
211
|
|
|
{ |
212
|
|
|
if (null === $eResult) { |
213
|
|
|
$this->expandedResult = $eResult; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
if (null !== $eResult->getFeed()) { |
217
|
|
|
$this->isExpanded = true; |
218
|
|
|
$this->isCollection = true; |
219
|
|
|
$this->expandedResult = $eResult; |
220
|
|
|
} |
221
|
|
|
if (null !== $eResult->getEntry()) { |
222
|
|
|
$this->isExpanded = true; |
223
|
|
|
$this->isCollection = false; |
224
|
|
|
$this->expandedResult = $eResult; |
225
|
|
|
} |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function isEmpty(): bool |
233
|
|
|
{ |
234
|
|
|
return !array_reduce( |
235
|
|
|
['isExpanded', 'isCollection', 'expandedResult', 'title', 'type', 'name', 'url'], |
236
|
|
|
function ($carry, $value) { |
237
|
|
|
return $carry || isset($this->{$value}); |
238
|
|
|
}, |
239
|
|
|
false |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|