1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace POData\ObjectModel; |
6
|
|
|
|
7
|
|
|
use POData\Common\InvalidOperationException; |
8
|
|
|
use POData\Common\Messages; |
9
|
|
|
use POData\Common\ODataConstants; |
10
|
|
|
use POData\Common\ODataException; |
11
|
|
|
use POData\IService; |
12
|
|
|
use POData\Providers\Metadata\ResourceComplexType; |
13
|
|
|
use POData\Providers\Metadata\ResourceEntityType; |
14
|
|
|
use POData\Providers\Metadata\ResourcePrimitiveType; |
15
|
|
|
use POData\Providers\Metadata\ResourceProperty; |
16
|
|
|
use POData\Providers\Metadata\ResourcePropertyKind; |
17
|
|
|
use POData\Providers\Metadata\ResourceSet; |
18
|
|
|
use POData\Providers\Metadata\ResourceSetWrapper; |
19
|
|
|
use POData\Providers\Metadata\ResourceType; |
20
|
|
|
use POData\Providers\Metadata\ResourceTypeKind; |
21
|
|
|
use POData\Providers\Metadata\Type\Binary; |
22
|
|
|
use POData\Providers\Metadata\Type\Boolean; |
23
|
|
|
use POData\Providers\Metadata\Type\DateTime; |
24
|
|
|
use POData\Providers\Metadata\Type\IType; |
25
|
|
|
use POData\Providers\Metadata\Type\StringType; |
26
|
|
|
use POData\Providers\Query\QueryResult; |
27
|
|
|
use POData\Providers\Query\QueryType; |
28
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\ExpandedProjectionNode; |
29
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\ProjectionNode; |
30
|
|
|
use POData\UriProcessor\QueryProcessor\ExpandProjectionParser\RootProjectionNode; |
31
|
|
|
use POData\UriProcessor\QueryProcessor\OrderByParser\InternalOrderByInfo; |
32
|
|
|
use POData\UriProcessor\RequestDescription; |
33
|
|
|
use POData\UriProcessor\SegmentStack; |
34
|
|
|
use ReflectionException; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class CynicSerialiser. |
38
|
|
|
* @package POData\ObjectModel |
39
|
|
|
*/ |
40
|
|
|
class CynicSerialiser implements IObjectSerialiser |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* The service implementation. |
44
|
|
|
* |
45
|
|
|
* @var IService |
46
|
|
|
*/ |
47
|
|
|
protected $service; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Request description instance describes OData request the |
51
|
|
|
* the client has submitted and result of the request. |
52
|
|
|
* |
53
|
|
|
* @var RequestDescription |
54
|
|
|
*/ |
55
|
|
|
protected $request; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Collection of complex type instances used for cycle detection. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $complexTypeInstanceCollection; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Absolute service Uri. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $absoluteServiceUri; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Absolute service Uri with slash. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $absoluteServiceUriWithSlash; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Holds reference to segment stack being processed. |
80
|
|
|
* |
81
|
|
|
* @var SegmentStack |
82
|
|
|
*/ |
83
|
|
|
protected $stack; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Lightweight stack tracking for recursive descent fill. |
87
|
|
|
*/ |
88
|
|
|
private $lightStack = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Update time to insert into ODataEntry/ODataFeed fields. |
92
|
|
|
* @var \DateTime; |
93
|
|
|
*/ |
94
|
|
|
private $updated; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Has base URI already been written out during serialisation? |
98
|
|
|
* @var bool; |
99
|
|
|
*/ |
100
|
|
|
private $isBaseWritten = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param IService $service Reference to the data service instance |
104
|
|
|
* @param RequestDescription|null $request Type instance describing the client submitted request |
105
|
|
|
* @throws \Exception |
106
|
|
|
*/ |
107
|
|
|
public function __construct(IService $service, RequestDescription $request = null) |
108
|
|
|
{ |
109
|
|
|
$this->service = $service; |
110
|
|
|
$this->request = $request; |
111
|
|
|
$this->absoluteServiceUri = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
112
|
|
|
$this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/'; |
113
|
|
|
$this->stack = new SegmentStack($request); |
114
|
|
|
$this->complexTypeInstanceCollection = []; |
115
|
|
|
$this->updated = DateTime::now(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Write top level feed element. |
120
|
|
|
* |
121
|
|
|
* @param QueryResult &$entryObjects Results property contains array of entry resources to be written |
122
|
|
|
* |
123
|
|
|
* @throws ODataException |
124
|
|
|
* @throws InvalidOperationException |
125
|
|
|
* @return ODataFeed |
126
|
|
|
*/ |
127
|
|
|
public function writeTopLevelElements(QueryResult &$entryObjects) |
128
|
|
|
{ |
129
|
|
|
$res = $entryObjects->results; |
130
|
|
|
if (!(is_array($res))) { |
131
|
|
|
throw new InvalidOperationException('!is_array($entryObjects->results)'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (is_array($res) && 0 == count($entryObjects->results)) { |
135
|
|
|
$entryObjects->hasMore = false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->loadStackIfEmpty(); |
139
|
|
|
$setName = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
140
|
|
|
|
141
|
|
|
$title = $this->getRequest()->getContainerName(); |
142
|
|
|
$relativeUri = $this->getRequest()->getIdentifier(); |
143
|
|
|
$absoluteUri = $this->getRequest()->getRequestUrl()->getUrlAsString(); |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
$selfLink = new ODataLink('self', $title, null, $relativeUri); |
147
|
|
|
$title = new ODataTitle($title); |
148
|
|
|
$id = $absoluteUri; |
149
|
|
|
$updated = $this->getUpdated()->format(DATE_ATOM); |
150
|
|
|
$baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
151
|
|
|
$entries = []; |
152
|
|
|
$this->isBaseWritten = true; |
153
|
|
|
$nextLink = null; |
154
|
|
|
|
155
|
|
|
$rowCount = $this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT() ? |
156
|
|
|
$this->getRequest()->getCountValue() : |
157
|
|
|
null; |
158
|
|
|
foreach ($res as $entry) { |
159
|
|
|
$query = $entry instanceof QueryResult ? $entry : new QueryResult($entry); |
160
|
|
|
$entries[] = $this->writeTopLevelElement($query); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$resourceSet = $this->getRequest()->getTargetResourceSetWrapper()->getResourceSet(); |
164
|
|
|
$requestTop = $this->getRequest()->getTopOptionCount(); |
165
|
|
|
$pageSize = $this->getService()->getConfiguration()->getEntitySetPageSize($resourceSet); |
166
|
|
|
$requestTop = (null === $requestTop) ? $pageSize + 1 : $requestTop; |
167
|
|
|
|
168
|
|
|
if (true === $entryObjects->hasMore && $requestTop > $pageSize) { |
169
|
|
|
$stackSegment = $setName; |
170
|
|
|
$lastObject = end($entryObjects->results); |
171
|
|
|
$segment = $this->getNextLinkUri($lastObject); |
172
|
|
|
$nextLink = new ODataNextPageLink( |
173
|
|
|
rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return new ODataFeed($id, $title, $selfLink, $rowCount, $nextLink, $entries, $updated, $baseURI); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Load processing stack if it's currently empty. |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
private function loadStackIfEmpty() |
186
|
|
|
{ |
187
|
|
|
if (0 == count($this->lightStack)) { |
188
|
|
|
$typeName = $this->getRequest()->getTargetResourceType()->getName(); |
189
|
|
|
array_push($this->lightStack, ['type' => $typeName, 'property' => $typeName, 'count' => 1]); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Gets reference to the request submitted by client. |
195
|
|
|
* |
196
|
|
|
* @return RequestDescription |
197
|
|
|
*/ |
198
|
|
|
public function getRequest() |
199
|
|
|
{ |
200
|
|
|
assert(null !== $this->request, 'Request not yet set'); |
201
|
|
|
|
202
|
|
|
return $this->request; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Sets reference to the request submitted by client. |
207
|
|
|
* |
208
|
|
|
* @param RequestDescription $request |
209
|
|
|
*/ |
210
|
|
|
public function setRequest(RequestDescription $request) |
211
|
|
|
{ |
212
|
|
|
$this->request = $request; |
213
|
|
|
$this->stack->setRequest($request); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get update timestamp. |
218
|
|
|
* |
219
|
|
|
* @return \DateTime |
220
|
|
|
*/ |
221
|
|
|
public function getUpdated() |
222
|
|
|
{ |
223
|
|
|
return $this->updated; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Write a top level entry resource. |
228
|
|
|
* |
229
|
|
|
* @param QueryResult $entryObject Results property contains reference to the entry object to be written |
230
|
|
|
* |
231
|
|
|
* @throws ODataException |
232
|
|
|
* @throws ReflectionException |
233
|
|
|
* @throws InvalidOperationException |
234
|
|
|
* @return ODataEntry|null |
235
|
|
|
*/ |
236
|
|
|
public function writeTopLevelElement(QueryResult $entryObject) |
237
|
|
|
{ |
238
|
|
|
if (!isset($entryObject->results)) { |
239
|
|
|
array_pop($this->lightStack); |
240
|
|
|
|
241
|
|
|
return null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
assert(is_object($entryObject->results)); |
245
|
|
|
$this->loadStackIfEmpty(); |
246
|
|
|
|
247
|
|
|
$baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
248
|
|
|
$this->isBaseWritten = true; |
249
|
|
|
|
250
|
|
|
$stackCount = count($this->lightStack); |
251
|
|
|
$topOfStack = $this->lightStack[$stackCount - 1]; |
252
|
|
|
$resourceType = $this->getService()->getProvidersWrapper()->resolveResourceType($topOfStack['type']); |
253
|
|
|
assert($resourceType instanceof ResourceType, get_class($resourceType)); |
254
|
|
|
$rawProp = $resourceType->getAllProperties(); |
255
|
|
|
$relProp = []; |
256
|
|
|
$nonRelProp = []; |
257
|
|
|
$last = end($this->lightStack); |
258
|
|
|
$projNodes = ($last['type'] == $last['property']) ? $this->getProjectionNodes() : null; |
259
|
|
|
|
260
|
|
|
foreach ($rawProp as $prop) { |
261
|
|
|
$propName = $prop->getName(); |
262
|
|
|
if ($prop->getResourceType() instanceof ResourceEntityType) { |
263
|
|
|
$relProp[$propName] = $prop; |
264
|
|
|
} else { |
265
|
|
|
$nonRelProp[$propName] = $prop; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
$rawCount = count($rawProp); |
269
|
|
|
$relCount = count($relProp); |
270
|
|
|
$nonRelCount = count($nonRelProp); |
271
|
|
|
assert( |
272
|
|
|
$rawCount == $relCount + $nonRelCount, |
273
|
|
|
'Raw property count ' . $rawCount . ', does not equal sum of relProp count, ' . $relCount |
274
|
|
|
. ', and nonRelPropCount,' . $nonRelCount |
275
|
|
|
); |
276
|
|
|
|
277
|
|
|
// now mask off against projNodes |
278
|
|
|
if (null !== $projNodes) { |
279
|
|
|
$keys = []; |
280
|
|
|
foreach ($projNodes as $node) { |
281
|
|
|
$keys[$node->getPropertyName()] = ''; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$relProp = array_intersect_key($relProp, $keys); |
285
|
|
|
$nonRelProp = array_intersect_key($nonRelProp, $keys); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$resourceSet = $resourceType->getCustomState(); |
289
|
|
|
assert($resourceSet instanceof ResourceSet); |
290
|
|
|
$type = $resourceType->getFullName(); |
291
|
|
|
|
292
|
|
|
$relativeUri = $this->getEntryInstanceKey( |
293
|
|
|
$entryObject->results, |
294
|
|
|
$resourceType, |
295
|
|
|
$resourceSet->getName() |
296
|
|
|
); |
297
|
|
|
$absoluteUri = rtrim(strval($this->absoluteServiceUri), '/') . '/' . $relativeUri; |
298
|
|
|
$mediaLinks = $this->writeMediaData( |
299
|
|
|
$entryObject->results, |
300
|
|
|
$type, |
301
|
|
|
$relativeUri, |
302
|
|
|
$resourceType |
303
|
|
|
); |
304
|
|
|
$mediaLink = array_shift($mediaLinks); |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
$propertyContent = $this->writeProperties($entryObject->results, $nonRelProp); |
308
|
|
|
|
309
|
|
|
$links = []; |
310
|
|
|
foreach ($relProp as $prop) { |
311
|
|
|
$propKind = $prop->getKind(); |
312
|
|
|
|
313
|
|
|
assert( |
314
|
|
|
ResourcePropertyKind::RESOURCESET_REFERENCE() == $propKind |
315
|
|
|
|| ResourcePropertyKind::RESOURCE_REFERENCE() == $propKind, |
316
|
|
|
'$propKind != ResourcePropertyKind::RESOURCESET_REFERENCE &&' |
317
|
|
|
. ' $propKind != ResourcePropertyKind::RESOURCE_REFERENCE' |
318
|
|
|
); |
319
|
|
|
$propTail = ResourcePropertyKind::RESOURCE_REFERENCE() == $propKind ? 'entry' : 'feed'; |
320
|
|
|
$propType = 'application/atom+xml;type=' . $propTail; |
321
|
|
|
$propName = $prop->getName(); |
322
|
|
|
$nuLink = new ODataLink( |
323
|
|
|
ODataConstants::ODATA_RELATED_NAMESPACE . $propName, |
324
|
|
|
$propName, |
325
|
|
|
$propType, |
326
|
|
|
$relativeUri . '/' . $propName, |
327
|
|
|
'feed' === $propTail |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
$shouldExpand = $this->shouldExpandSegment($propName); |
332
|
|
|
|
333
|
|
|
if ($shouldExpand) { |
334
|
|
|
$this->expandNavigationProperty($entryObject, $prop, $nuLink, $propKind, $propName); |
335
|
|
|
} |
336
|
|
|
$nuLink->setIsExpanded(null !== $nuLink->getExpandedResult() && null !== $nuLink->getExpandedResult()->getData()); |
337
|
|
|
assert(null !== $nuLink->isCollection()); |
338
|
|
|
|
339
|
|
|
$links[] = $nuLink; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$odata = new ODataEntry( |
343
|
|
|
$absoluteUri, |
344
|
|
|
null, |
345
|
|
|
new ODataTitle($resourceType->getName()), |
346
|
|
|
new ODataLink('edit', $resourceType->getName(), null, $relativeUri), |
347
|
|
|
new ODataCategory($type), |
348
|
|
|
$propertyContent, |
349
|
|
|
$mediaLinks, |
350
|
|
|
$mediaLink, |
351
|
|
|
$links, |
352
|
|
|
null, |
353
|
|
|
true === $resourceType->isMediaLinkEntry() , |
354
|
|
|
$resourceSet->getName(), |
355
|
|
|
$this->getUpdated()->format(DATE_ATOM), |
356
|
|
|
$baseURI |
357
|
|
|
|
358
|
|
|
); |
359
|
|
|
|
360
|
|
|
$newCount = count($this->lightStack); |
361
|
|
|
assert( |
362
|
|
|
$newCount == $stackCount, |
363
|
|
|
'Should have ' . $stackCount . 'elements in stack, have ' . $newCount . 'elements' |
364
|
|
|
); |
365
|
|
|
--$this->lightStack[$newCount - 1]['count']; |
366
|
|
|
if (0 == $this->lightStack[$newCount - 1]['count']) { |
367
|
|
|
array_pop($this->lightStack); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $odata; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Gets the data service instance. |
375
|
|
|
* |
376
|
|
|
* @return IService |
377
|
|
|
*/ |
378
|
|
|
public function getService() |
379
|
|
|
{ |
380
|
|
|
return $this->service; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Sets the data service instance. |
385
|
|
|
* |
386
|
|
|
* @param IService $service |
387
|
|
|
*/ |
388
|
|
|
public function setService(IService $service) |
389
|
|
|
{ |
390
|
|
|
$this->service = $service; |
391
|
|
|
$this->absoluteServiceUri = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
392
|
|
|
$this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/'; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Gets collection of projection nodes under the current node. |
397
|
|
|
* |
398
|
|
|
* @throws InvalidOperationException |
399
|
|
|
* @return ProjectionNode[]|ExpandedProjectionNode[]|null List of nodes describing projections for the current |
400
|
|
|
* segment, If this method returns null it means no |
401
|
|
|
* projections are to be applied and the entire resource for |
402
|
|
|
* the current segment should be serialized, If it returns |
403
|
|
|
* non-null only the properties described by the returned |
404
|
|
|
* projection segments should be serialized |
405
|
|
|
*/ |
406
|
|
|
protected function getProjectionNodes() |
407
|
|
|
{ |
408
|
|
|
$expandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
409
|
|
|
if (null === $expandedProjectionNode || $expandedProjectionNode->canSelectAllProperties()) { |
410
|
|
|
return null; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $expandedProjectionNode->getChildNodes(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Find a 'ExpandedProjectionNode' instance in the projection tree |
418
|
|
|
* which describes the current segment. |
419
|
|
|
* |
420
|
|
|
* @throws InvalidOperationException |
421
|
|
|
* @return RootProjectionNode|ExpandedProjectionNode|null |
422
|
|
|
*/ |
423
|
|
|
protected function getCurrentExpandedProjectionNode() |
424
|
|
|
{ |
425
|
|
|
/** @var RootProjectionNode|null $expandedProjectionNode */ |
426
|
|
|
$expandedProjectionNode = $this->getRequest()->getRootProjectionNode(); |
427
|
|
|
if (null === $expandedProjectionNode) { |
428
|
|
|
return null; |
429
|
|
|
} else { |
430
|
|
|
$segmentNames = $this->getStack()->getSegmentNames(); |
431
|
|
|
$depth = count($segmentNames); |
432
|
|
|
// $depth == 1 means serialization of root entry |
433
|
|
|
//(the resource identified by resource path) is going on, |
434
|
|
|
//so control won't get into the below for loop. |
435
|
|
|
//we will directly return the root node, |
436
|
|
|
//which is 'ExpandedProjectionNode' |
437
|
|
|
// for resource identified by resource path. |
438
|
|
|
if (0 != $depth) { |
439
|
|
|
for ($i = 1; $i < $depth; ++$i) { |
440
|
|
|
$expandedProjectionNode = $expandedProjectionNode->findNode($segmentNames[$i]); |
441
|
|
|
if (null === $expandedProjectionNode) { |
442
|
|
|
throw new InvalidOperationException('is_null($expandedProjectionNode)'); |
443
|
|
|
} |
444
|
|
|
if (!$expandedProjectionNode instanceof ExpandedProjectionNode) { |
445
|
|
|
$msg = '$expandedProjectionNode not instanceof ExpandedProjectionNode'; |
446
|
|
|
throw new InvalidOperationException($msg); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
return $expandedProjectionNode; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Gets the segment stack instance. |
457
|
|
|
* |
458
|
|
|
* @return SegmentStack |
459
|
|
|
*/ |
460
|
|
|
public function getStack() |
461
|
|
|
{ |
462
|
|
|
return $this->stack; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @param object $entityInstance |
467
|
|
|
* @param ResourceType $resourceType |
468
|
|
|
* @param string $containerName |
469
|
|
|
* @throws ReflectionException |
470
|
|
|
* @throws ODataException |
471
|
|
|
* @return string |
472
|
|
|
*/ |
473
|
|
|
protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
474
|
|
|
{ |
475
|
|
|
assert(is_object($entityInstance)); |
476
|
|
|
$typeName = $resourceType->getName(); |
477
|
|
|
$keyProperties = $resourceType->getKeyProperties(); |
478
|
|
|
assert(0 != count($keyProperties), 'count($keyProperties) == 0'); |
479
|
|
|
$keyString = $containerName . '('; |
480
|
|
|
$comma = null; |
481
|
|
|
foreach ($keyProperties as $keyName => $resourceProperty) { |
482
|
|
|
$keyType = $resourceProperty->getInstanceType(); |
483
|
|
|
assert($keyType instanceof IType, '$keyType not instanceof IType'); |
484
|
|
|
$keyName = $resourceProperty->getName(); |
485
|
|
|
$keyValue = $entityInstance->{$keyName}; |
486
|
|
|
if (!isset($keyValue)) { |
487
|
|
|
$msg = Messages::badQueryNullKeysAreNotSupported($typeName, $keyName); |
488
|
|
|
throw ODataException::createInternalServerError($msg); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
$keyValue = $keyType->convertToOData(strval($keyValue)); |
492
|
|
|
$keyString .= $comma . $keyName . '=' . $keyValue; |
493
|
|
|
$comma = ','; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
$keyString .= ')'; |
497
|
|
|
|
498
|
|
|
return $keyString; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param $entryObject |
503
|
|
|
* @param $type |
504
|
|
|
* @param $relativeUri |
505
|
|
|
* @param $resourceType |
506
|
|
|
* |
507
|
|
|
* @return ODataMediaLink[]|null[] |
508
|
|
|
*/ |
509
|
|
|
protected function writeMediaData($entryObject, $type, $relativeUri, ResourceType $resourceType) |
510
|
|
|
{ |
511
|
|
|
$context = $this->getService()->getOperationContext(); |
512
|
|
|
$streamProviderWrapper = $this->getService()->getStreamProviderWrapper(); |
513
|
|
|
assert(null != $streamProviderWrapper, 'Retrieved stream provider must not be null'); |
514
|
|
|
|
515
|
|
|
$mediaLink = null; |
516
|
|
|
if ($resourceType->isMediaLinkEntry()) { |
517
|
|
|
$eTag = $streamProviderWrapper->getStreamETag2($entryObject, $context, null); |
|
|
|
|
518
|
|
|
$mediaLink = new ODataMediaLink($type, '/$value', $relativeUri . '/$value', '*/*', $eTag, 'edit-media'); |
519
|
|
|
} |
520
|
|
|
$mediaLinks = []; |
521
|
|
|
if ($resourceType->hasNamedStream()) { |
522
|
|
|
$namedStreams = $resourceType->getAllNamedStreams(); |
523
|
|
|
foreach ($namedStreams as $streamTitle => $resourceStreamInfo) { |
524
|
|
|
$readUri = $streamProviderWrapper->getReadStreamUri2( |
|
|
|
|
525
|
|
|
$entryObject, |
526
|
|
|
$context, |
527
|
|
|
$resourceStreamInfo, |
528
|
|
|
$relativeUri |
529
|
|
|
); |
530
|
|
|
$mediaContentType = $streamProviderWrapper->getStreamContentType2( |
|
|
|
|
531
|
|
|
$entryObject, |
532
|
|
|
$context, |
533
|
|
|
$resourceStreamInfo |
534
|
|
|
); |
535
|
|
|
$eTag = $streamProviderWrapper->getStreamETag2( |
536
|
|
|
$entryObject, |
537
|
|
|
$context, |
538
|
|
|
$resourceStreamInfo |
539
|
|
|
); |
540
|
|
|
|
541
|
|
|
$nuLink = new ODataMediaLink($streamTitle, $readUri, $readUri, $mediaContentType, $eTag); |
542
|
|
|
$mediaLinks[] = $nuLink; |
543
|
|
|
} |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
return array_merge([$mediaLink], $mediaLinks); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* @param $entryObject |
551
|
|
|
* @param array<string, ResourceProperty> $nonRelProp |
552
|
|
|
* |
553
|
|
|
* @throws ReflectionException |
554
|
|
|
* @throws InvalidOperationException |
555
|
|
|
* @return ODataPropertyContent |
556
|
|
|
*/ |
557
|
|
|
private function writeProperties($entryObject, $nonRelProp) |
558
|
|
|
{ |
559
|
|
|
$properties = []; |
560
|
|
|
foreach ($nonRelProp as $corn => $flake) { |
561
|
|
|
/** @var ResourceType $resource */ |
562
|
|
|
$resource = $nonRelProp[$corn]->getResourceType(); |
563
|
|
|
if ($resource instanceof ResourceEntityType) { |
564
|
|
|
continue; |
565
|
|
|
} |
566
|
|
|
$result = $entryObject->{$corn}; |
567
|
|
|
$isBag = $flake->isKindOf(ResourcePropertyKind::BAG()); |
568
|
|
|
$typePrepend = $isBag ? 'Collection(' : ''; |
569
|
|
|
$typeAppend = $isBag ? ')' : ''; |
570
|
|
|
$nonNull = null !== $result; |
571
|
|
|
$name = strval($corn); |
572
|
|
|
$typeName = $typePrepend . $resource->getFullName() . $typeAppend; |
573
|
|
|
$value = null; |
574
|
|
|
if ($nonNull && is_array($result)) { |
575
|
|
|
$value = $this->writeBagValue($resource, $result); |
576
|
|
|
} elseif ($resource instanceof ResourcePrimitiveType && $nonNull) { |
577
|
|
|
$rType = $resource->getInstanceType(); |
578
|
|
|
if (!$rType instanceof IType) { |
579
|
|
|
throw new InvalidOperationException(get_class($rType)); |
580
|
|
|
} |
581
|
|
|
$value = $this->primitiveToString($rType, $result); |
582
|
|
|
} elseif ($resource instanceof ResourceComplexType && $nonNull) { |
583
|
|
|
$value = $this->writeComplexValue($resource, $result, $flake->getName()); |
584
|
|
|
} |
585
|
|
|
$properties[$corn] = new ODataProperty($name, $typeName, $value); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
return new ODataPropertyContent($properties); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* @param ResourceType $resourceType |
593
|
|
|
* @param $result |
594
|
|
|
* |
595
|
|
|
* @throws ReflectionException |
596
|
|
|
* @throws InvalidOperationException |
597
|
|
|
* @return ODataBagContent|null |
598
|
|
|
*/ |
599
|
|
|
protected function writeBagValue(ResourceType &$resourceType, $result) |
600
|
|
|
{ |
601
|
|
|
$bagNullOrArray = null === $result || is_array($result); |
602
|
|
|
if (!$bagNullOrArray) { |
603
|
|
|
throw new InvalidOperationException('Bag parameter must be null or array'); |
604
|
|
|
} |
605
|
|
|
$typeKind = $resourceType->getResourceTypeKind(); |
606
|
|
|
$typePrimitiveOrComplex = ResourceTypeKind::PRIMITIVE() == $typeKind |
607
|
|
|
|| ResourceTypeKind::COMPLEX() == $typeKind; |
608
|
|
|
if (!$typePrimitiveOrComplex) { |
609
|
|
|
throw new InvalidOperationException('$bagItemResourceTypeKind != ResourceTypeKind::PRIMITIVE' |
610
|
|
|
. ' && $bagItemResourceTypeKind != ResourceTypeKind::COMPLEX'); |
611
|
|
|
} |
612
|
|
|
if (null == $result) { |
613
|
|
|
return null; |
614
|
|
|
} |
615
|
|
|
$bag = new ODataBagContent(); |
616
|
|
|
foreach ($result as $value) { |
617
|
|
|
if (isset($value)) { |
618
|
|
|
if (ResourceTypeKind::PRIMITIVE() == $typeKind) { |
619
|
|
|
$instance = $resourceType->getInstanceType(); |
620
|
|
|
if (!$instance instanceof IType) { |
621
|
|
|
throw new InvalidOperationException(get_class($instance)); |
622
|
|
|
} |
623
|
|
|
$bag->addPropertyContent($this->primitiveToString($instance, $value)); |
624
|
|
|
} elseif (ResourceTypeKind::COMPLEX() == $typeKind) { |
625
|
|
|
$bag->addPropertyContent($this->writeComplexValue($resourceType, $value)); |
626
|
|
|
} |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
return $bag; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* Convert the given primitive value to string. |
635
|
|
|
* Note: This method will not handle null primitive value. |
636
|
|
|
* |
637
|
|
|
* @param IType &$type Type of the primitive property |
638
|
|
|
* whose value need to be converted |
639
|
|
|
* @param mixed $primitiveValue Primitive value to convert |
640
|
|
|
* |
641
|
|
|
* @return string |
642
|
|
|
*/ |
643
|
|
|
private function primitiveToString(IType &$type, $primitiveValue) |
644
|
|
|
{ |
645
|
|
|
if ($type instanceof Boolean) { |
646
|
|
|
$stringValue = (true === $primitiveValue) ? 'true' : 'false'; |
647
|
|
|
} elseif ($type instanceof Binary) { |
648
|
|
|
$stringValue = base64_encode($primitiveValue); |
649
|
|
|
} elseif ($type instanceof DateTime && $primitiveValue instanceof \DateTime) { |
650
|
|
|
$stringValue = $primitiveValue->format(\DateTime::ATOM); |
651
|
|
|
} elseif ($type instanceof StringType) { |
652
|
|
|
$stringValue = mb_convert_encoding(strval($primitiveValue), 'UTF-8'); |
653
|
|
|
} else { |
654
|
|
|
$stringValue = strval($primitiveValue); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
return $stringValue; |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* @param ResourceType $resourceType |
662
|
|
|
* @param object $result |
663
|
|
|
* @param string|null $propertyName |
664
|
|
|
* |
665
|
|
|
* @throws ReflectionException |
666
|
|
|
* @throws InvalidOperationException |
667
|
|
|
* @return ODataPropertyContent |
668
|
|
|
*/ |
669
|
|
|
protected function writeComplexValue(ResourceType &$resourceType, &$result, $propertyName = null) |
670
|
|
|
{ |
671
|
|
|
assert(is_object($result), 'Supplied $customObject must be an object'); |
672
|
|
|
|
673
|
|
|
$count = count($this->complexTypeInstanceCollection); |
674
|
|
|
for ($i = 0; $i < $count; ++$i) { |
675
|
|
|
if ($this->complexTypeInstanceCollection[$i] === $result) { |
676
|
|
|
throw new InvalidOperationException( |
677
|
|
|
Messages::objectModelSerializerLoopsNotAllowedInComplexTypes($propertyName) |
678
|
|
|
); |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
$this->complexTypeInstanceCollection[$count] = &$result; |
683
|
|
|
|
684
|
|
|
$properties = []; |
685
|
|
|
$resourceProperties = $resourceType->getAllProperties(); |
686
|
|
|
// first up, handle primitive properties |
687
|
|
|
foreach ($resourceProperties as $prop) { |
688
|
|
|
$resourceKind = $prop->getKind(); |
689
|
|
|
$propName = $prop->getName(); |
690
|
|
|
$name = $propName; |
691
|
|
|
$typeName = null; |
692
|
|
|
$value = null; |
693
|
|
|
$raw = $result->{$propName}; |
694
|
|
|
if (static::isMatchPrimitive($resourceKind)) { |
695
|
|
|
$iType = $prop->getInstanceType(); |
696
|
|
|
if (!$iType instanceof IType) { |
697
|
|
|
throw new InvalidOperationException(get_class($iType)); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
$typeName = $iType->getFullTypeName(); |
701
|
|
|
|
702
|
|
|
$rType = $prop->getResourceType()->getInstanceType(); |
703
|
|
|
if (!$rType instanceof IType) { |
704
|
|
|
throw new InvalidOperationException(get_class($rType)); |
705
|
|
|
} |
706
|
|
|
if (null !== $raw) { |
707
|
|
|
$value = $this->primitiveToString($rType, $raw); |
708
|
|
|
} |
709
|
|
|
} elseif (ResourcePropertyKind::COMPLEX_TYPE() == $resourceKind) { |
710
|
|
|
$rType = $prop->getResourceType(); |
711
|
|
|
$typeName = $rType->getFullName(); |
712
|
|
|
if (null !== $raw) { |
713
|
|
|
$value = $this->writeComplexValue($rType, $raw, $propName); |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
$properties[$propName] = new ODataProperty($name, $typeName, $value); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
unset($this->complexTypeInstanceCollection[$count]); |
720
|
|
|
|
721
|
|
|
return new ODataPropertyContent($properties); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
/** |
725
|
|
|
* Is the supplied resourceKind representing a primitive value? |
726
|
|
|
* |
727
|
|
|
* @param int|ResourcePropertyKind $resourceKind |
728
|
|
|
* @return bool |
729
|
|
|
*/ |
730
|
|
|
public static function isMatchPrimitive($resourceKind): bool |
731
|
|
|
{ |
732
|
|
|
$value = $resourceKind instanceof ResourcePropertyKind ? $resourceKind->getValue() : $resourceKind; |
733
|
|
|
if (16 > $value) { |
734
|
|
|
return false; |
735
|
|
|
} |
736
|
|
|
if (28 < $value) { |
737
|
|
|
return false; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
return 0 == ($value % 4); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Check whether to expand a navigation property or not. |
745
|
|
|
* |
746
|
|
|
* @param string $navigationPropertyName Name of navigation property in question |
747
|
|
|
* |
748
|
|
|
* @throws InvalidOperationException |
749
|
|
|
* @return bool True if the given navigation should be expanded, otherwise false |
750
|
|
|
*/ |
751
|
|
|
protected function shouldExpandSegment($navigationPropertyName) |
752
|
|
|
{ |
753
|
|
|
$expandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
754
|
|
|
if (null === $expandedProjectionNode) { |
755
|
|
|
return false; |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
$expandedProjectionNode = $expandedProjectionNode->findNode($navigationPropertyName); |
759
|
|
|
|
760
|
|
|
// null is a valid input to an instanceof call as of PHP 5.6 - will always return false |
761
|
|
|
return $expandedProjectionNode instanceof ExpandedProjectionNode; |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* @param QueryResult $entryObject |
766
|
|
|
* @param ResourceProperty $prop |
767
|
|
|
* @param ODataLink $nuLink |
768
|
|
|
* @param ResourcePropertyKind $propKind |
769
|
|
|
* @param string $propName |
770
|
|
|
* @throws InvalidOperationException |
771
|
|
|
* @throws ODataException |
772
|
|
|
* @throws ReflectionException |
773
|
|
|
*/ |
774
|
|
|
private function expandNavigationProperty( |
775
|
|
|
QueryResult $entryObject, |
776
|
|
|
ResourceProperty $prop, |
777
|
|
|
ODataLink $nuLink, |
778
|
|
|
ResourcePropertyKind $propKind, |
779
|
|
|
string $propName |
780
|
|
|
) { |
781
|
|
|
$nextName = $prop->getResourceType()->getName(); |
782
|
|
|
$nuLink->setIsExpanded(true); |
783
|
|
|
$value = $entryObject->results->{$propName}; |
784
|
|
|
$isCollection = ResourcePropertyKind::RESOURCESET_REFERENCE() == $propKind; |
785
|
|
|
$nuLink->setIsCollection($isCollection); |
786
|
|
|
$nullResult = null === $value; |
787
|
|
|
$object = (is_object($value)); |
788
|
|
|
$resultCount = ($nullResult) ? 0 : ($object ? 1 : count($value)); |
789
|
|
|
|
790
|
|
|
if (0 < $resultCount) { |
791
|
|
|
$result = new QueryResult(); |
792
|
|
|
$result->results = $value; |
793
|
|
|
if (!$nullResult) { |
794
|
|
|
$newStackLine = ['type' => $nextName, 'property' => $propName, 'count' => $resultCount]; |
795
|
|
|
array_push($this->lightStack, $newStackLine); |
796
|
|
|
if (isset($value)) { |
797
|
|
|
if (!$isCollection) { |
798
|
|
|
$nuLink->setType('application/atom+xml;type=entry'); |
799
|
|
|
$expandedResult = $this->writeTopLevelElement($result); |
800
|
|
|
} else { |
801
|
|
|
$nuLink->setType('application/atom+xml;type=feed'); |
802
|
|
|
$expandedResult = $this->writeTopLevelElements($result); |
803
|
|
|
} |
804
|
|
|
if (null !== $expandedResult) { |
805
|
|
|
$nuLink->setExpandedResult(new ODataExpandedResult($expandedResult)); |
806
|
|
|
} |
807
|
|
|
} |
808
|
|
|
} |
809
|
|
|
} else { |
810
|
|
|
$type = $this->getService()->getProvidersWrapper()->resolveResourceType($nextName); |
811
|
|
|
if ($isCollection) { |
812
|
|
|
$result = new ODataFeed(null, null, new ODataLink(ODataConstants::ATOM_SELF_RELATION_ATTRIBUTE_VALUE)); |
813
|
|
|
} else { |
814
|
|
|
$result = new ODataEntry(); |
815
|
|
|
$result->resourceSetName = $type->getName(); |
816
|
|
|
} |
817
|
|
|
$nuLink->setExpandedResult(new ODataExpandedResult($result)); |
818
|
|
|
} |
819
|
|
|
if (null !== $nuLink->getExpandedResult() && null !== $nuLink->getExpandedResult()->getData() && null !== $nuLink->getExpandedResult()->getData()->getSelfLink()) { |
820
|
|
|
$nuLink->getExpandedResult()->getData()->getSelfLink()->setTitle($propName); |
821
|
|
|
$nuLink->getExpandedResult()->getData()->getSelfLink()->setUrl($nuLink->getUrl()); |
822
|
|
|
$nuLink->getExpandedResult()->getData()->setTitle(new ODataTitle($propName)); |
823
|
|
|
$nuLink->getExpandedResult()->getData()->id = rtrim($this->absoluteServiceUri, '/') . '/' . $nuLink->getUrl(); |
824
|
|
|
} |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
/** |
828
|
|
|
* Get next page link from the given entity instance. |
829
|
|
|
* |
830
|
|
|
* @param mixed &$lastObject Last object serialized to be |
831
|
|
|
* used for generating $skiptoken |
832
|
|
|
* |
833
|
|
|
* @throws ODataException |
834
|
|
|
* @throws InvalidOperationException |
835
|
|
|
* @return string for the link for next page |
836
|
|
|
*/ |
837
|
|
|
protected function getNextLinkUri($lastObject) |
838
|
|
|
{ |
839
|
|
|
$currentExpandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
840
|
|
|
$internalOrderByInfo = $currentExpandedProjectionNode->getInternalOrderByInfo(); |
841
|
|
|
assert(null !== $internalOrderByInfo); |
842
|
|
|
assert(is_object($internalOrderByInfo)); |
843
|
|
|
assert($internalOrderByInfo instanceof InternalOrderByInfo, get_class($internalOrderByInfo)); |
844
|
|
|
$numSegments = count($internalOrderByInfo->getOrderByPathSegments()); |
845
|
|
|
$queryParameterString = $this->getNextPageLinkQueryParametersForRootResourceSet(); |
846
|
|
|
|
847
|
|
|
$skipToken = $internalOrderByInfo->buildSkipTokenValue($lastObject); |
848
|
|
|
assert(null !== $skipToken, '!is_null($skipToken)'); |
849
|
|
|
$token = (1 < $numSegments) ? '$skiptoken=' : '$skip='; |
850
|
|
|
$skipToken = '?' . $queryParameterString . $token . $skipToken; |
851
|
|
|
|
852
|
|
|
return $skipToken; |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
/** |
856
|
|
|
* Builds the string corresponding to query parameters for top level results |
857
|
|
|
* (result set identified by the resource path) to be put in next page link. |
858
|
|
|
* |
859
|
|
|
* @return string|null string representing the query parameters in the URI |
860
|
|
|
* query parameter format, NULL if there |
861
|
|
|
* is no query parameters |
862
|
|
|
* required for the next link of top level result set |
863
|
|
|
*/ |
864
|
|
|
protected function getNextPageLinkQueryParametersForRootResourceSet() |
865
|
|
|
{ |
866
|
|
|
$queryParameterString = null; |
867
|
|
|
foreach ([ODataConstants::HTTPQUERY_STRING_FILTER, |
868
|
|
|
ODataConstants::HTTPQUERY_STRING_EXPAND, |
869
|
|
|
ODataConstants::HTTPQUERY_STRING_ORDERBY, |
870
|
|
|
ODataConstants::HTTPQUERY_STRING_INLINECOUNT, |
871
|
|
|
ODataConstants::HTTPQUERY_STRING_SELECT,] as $queryOption) { |
872
|
|
|
$value = $this->getService()->getHost()->getQueryStringItem($queryOption); |
873
|
|
|
if (null !== $value) { |
874
|
|
|
if (null !== $queryParameterString) { |
875
|
|
|
$queryParameterString = $queryParameterString . '&'; |
|
|
|
|
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
$queryParameterString .= $queryOption . '=' . $value; |
879
|
|
|
} |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
$topCountValue = $this->getRequest()->getTopOptionCount(); |
883
|
|
|
if (null !== $topCountValue) { |
884
|
|
|
$remainingCount = $topCountValue - $this->getRequest()->getTopCount(); |
885
|
|
|
if (0 < $remainingCount) { |
886
|
|
|
if (null !== $queryParameterString) { |
887
|
|
|
$queryParameterString .= '&'; |
888
|
|
|
} |
889
|
|
|
|
890
|
|
|
$queryParameterString .= ODataConstants::HTTPQUERY_STRING_TOP . '=' . $remainingCount; |
891
|
|
|
} |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
if (null !== $queryParameterString) { |
895
|
|
|
$queryParameterString .= '&'; |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
return $queryParameterString; |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
/** |
902
|
|
|
* Write top level url collection. |
903
|
|
|
* |
904
|
|
|
* @param QueryResult $entryObjects Results property contains the array of entry resources whose urls are |
905
|
|
|
* to be written |
906
|
|
|
* |
907
|
|
|
* @throws ODataException |
908
|
|
|
* @throws ReflectionException |
909
|
|
|
* @throws InvalidOperationException |
910
|
|
|
* @return ODataURLCollection |
911
|
|
|
*/ |
912
|
|
|
public function writeUrlElements(QueryResult $entryObjects) |
913
|
|
|
{ |
914
|
|
|
$urls = []; |
915
|
|
|
$count = null; |
916
|
|
|
$nextPageLink = null; |
917
|
|
|
if (!empty($entryObjects->results)) { |
918
|
|
|
$i = 0; |
919
|
|
|
foreach ($entryObjects->results as $entryObject) { |
920
|
|
|
if (!$entryObject instanceof QueryResult) { |
921
|
|
|
$query = new QueryResult(); |
922
|
|
|
$query->results = $entryObject; |
923
|
|
|
} else { |
924
|
|
|
$query = $entryObject; |
925
|
|
|
} |
926
|
|
|
$urls[$i] = $this->writeUrlElement($query); |
927
|
|
|
++$i; |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
if ($i > 0 && true === $entryObjects->hasMore) { |
931
|
|
|
$stackSegment = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
932
|
|
|
$lastObject = end($entryObjects->results); |
|
|
|
|
933
|
|
|
$segment = $this->getNextLinkUri($lastObject); |
934
|
|
|
$nextLink = new ODataLink( |
935
|
|
|
ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING, |
936
|
|
|
null, |
937
|
|
|
null, |
938
|
|
|
ltrim(rtrim(strval($this->absoluteServiceUri), '/') . '/' . $stackSegment . $segment, '/') |
939
|
|
|
); |
940
|
|
|
$nextPageLink = $nextLink; |
941
|
|
|
} |
942
|
|
|
} |
943
|
|
|
|
944
|
|
|
if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
945
|
|
|
$count = $this->getRequest()->getCountValue(); |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
return new ODataURLCollection($urls, $nextPageLink, $count); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Write top level url element. |
953
|
|
|
* |
954
|
|
|
* @param QueryResult $entryObject Results property contains the entry resource whose url to be written |
955
|
|
|
* |
956
|
|
|
* @throws ReflectionException |
957
|
|
|
* @throws ODataException |
958
|
|
|
* @return ODataURL |
959
|
|
|
*/ |
960
|
|
|
public function writeUrlElement(QueryResult $entryObject) |
961
|
|
|
{ |
962
|
|
|
$url = null; |
963
|
|
|
|
964
|
|
|
/** @var object|null $results */ |
965
|
|
|
$results = $entryObject->results; |
966
|
|
|
if (null !== $results) { |
967
|
|
|
$currentResourceType = $this->getCurrentResourceSetWrapper()->getResourceType(); |
968
|
|
|
$relativeUri = $this->getEntryInstanceKey( |
969
|
|
|
$results, |
970
|
|
|
$currentResourceType, |
971
|
|
|
$this->getCurrentResourceSetWrapper()->getName() |
972
|
|
|
); |
973
|
|
|
|
974
|
|
|
$url = new ODataURL(rtrim(strval($this->absoluteServiceUri), '/') . '/' . $relativeUri); |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
return $url; |
978
|
|
|
} |
979
|
|
|
|
980
|
|
|
/** |
981
|
|
|
* Resource set wrapper for the resource being serialized. |
982
|
|
|
* |
983
|
|
|
* @return ResourceSetWrapper |
984
|
|
|
*/ |
985
|
|
|
protected function getCurrentResourceSetWrapper() |
986
|
|
|
{ |
987
|
|
|
$segmentWrappers = $this->getStack()->getSegmentWrappers(); |
988
|
|
|
$count = count($segmentWrappers); |
989
|
|
|
|
990
|
|
|
return 0 == $count ? $this->getRequest()->getTargetResourceSetWrapper() : $segmentWrappers[$count - 1]; |
991
|
|
|
} |
992
|
|
|
|
993
|
|
|
/** |
994
|
|
|
* Write top level complex resource. |
995
|
|
|
* |
996
|
|
|
* @param QueryResult &$complexValue Results property contains the complex object to be written |
997
|
|
|
* @param string $propertyName The name of the complex property |
998
|
|
|
* @param ResourceType &$resourceType Describes the type of complex object |
999
|
|
|
* |
1000
|
|
|
* @throws ReflectionException |
1001
|
|
|
* @throws InvalidOperationException |
1002
|
|
|
* @return ODataPropertyContent |
1003
|
|
|
*/ |
1004
|
|
|
public function writeTopLevelComplexObject(QueryResult &$complexValue, $propertyName, ResourceType &$resourceType) |
1005
|
|
|
{ |
1006
|
|
|
$result = $complexValue->results; |
1007
|
|
|
|
1008
|
|
|
$name = $propertyName; |
1009
|
|
|
$typeName = $resourceType->getFullName(); |
1010
|
|
|
$value = null; |
1011
|
|
|
if (null !== $result) { |
1012
|
|
|
if (!is_object($result)) { |
1013
|
|
|
throw new InvalidOperationException('Supplied $customObject must be an object'); |
1014
|
|
|
} |
1015
|
|
|
$internalContent = $this->writeComplexValue($resourceType, $result); |
1016
|
|
|
$value = $internalContent; |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
|
|
return new ODataPropertyContent( |
1020
|
|
|
[ |
1021
|
|
|
$propertyName => new ODataProperty($name, $typeName, $value) |
1022
|
|
|
] |
1023
|
|
|
); |
1024
|
|
|
} |
1025
|
|
|
|
1026
|
|
|
/** |
1027
|
|
|
* Write top level bag resource. |
1028
|
|
|
* |
1029
|
|
|
* @param QueryResult $bagValue |
1030
|
|
|
* @param string $propertyName The name of the bag property |
1031
|
|
|
* @param ResourceType &$resourceType Describes the type of bag object |
1032
|
|
|
* |
1033
|
|
|
* @throws ReflectionException |
1034
|
|
|
* @throws InvalidOperationException |
1035
|
|
|
* @return ODataPropertyContent |
1036
|
|
|
*/ |
1037
|
|
|
public function writeTopLevelBagObject(QueryResult &$bagValue, $propertyName, ResourceType &$resourceType) |
1038
|
|
|
{ |
1039
|
|
|
$result = $bagValue->results; |
1040
|
|
|
|
1041
|
|
|
$odataProperty = new ODataProperty( |
1042
|
|
|
$propertyName, |
1043
|
|
|
'Collection(' . $resourceType->getFullName() . ')', |
1044
|
|
|
$this->writeBagValue($resourceType, $result) |
1045
|
|
|
); |
1046
|
|
|
|
1047
|
|
|
return new ODataPropertyContent([$propertyName => $odataProperty]); |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
|
|
/** |
1051
|
|
|
* Write top level primitive value. |
1052
|
|
|
* |
1053
|
|
|
* @param QueryResult &$primitiveValue Results property contains the primitive value to be written |
1054
|
|
|
* @param ResourceProperty &$resourceProperty Resource property describing the primitive property to be written |
1055
|
|
|
* |
1056
|
|
|
* @throws ReflectionException |
1057
|
|
|
* @throws InvalidOperationException |
1058
|
|
|
* @return ODataPropertyContent |
1059
|
|
|
*/ |
1060
|
|
|
public function writeTopLevelPrimitive(QueryResult &$primitiveValue, ResourceProperty &$resourceProperty = null) |
1061
|
|
|
{ |
1062
|
|
|
if (null === $resourceProperty) { |
1063
|
|
|
throw new InvalidOperationException('Resource property must not be null'); |
1064
|
|
|
} |
1065
|
|
|
$name = $resourceProperty->getName(); |
1066
|
|
|
$value = null; |
1067
|
|
|
$iType = $resourceProperty->getInstanceType(); |
1068
|
|
|
if (!$iType instanceof IType) { |
1069
|
|
|
throw new InvalidOperationException(get_class($iType)); |
1070
|
|
|
} |
1071
|
|
|
$typeName = $iType->getFullTypeName(); |
1072
|
|
|
if (null !== $primitiveValue->results) { |
1073
|
|
|
$rType = $resourceProperty->getResourceType()->getInstanceType(); |
1074
|
|
|
if (!$rType instanceof IType) { |
1075
|
|
|
throw new InvalidOperationException(get_class($rType)); |
1076
|
|
|
} |
1077
|
|
|
$value = $this->primitiveToString($rType, $primitiveValue->results); |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
return new ODataPropertyContent( |
1081
|
|
|
[ |
1082
|
|
|
$name => new ODataProperty($name, $typeName, $value) |
1083
|
|
|
] |
1084
|
|
|
); |
1085
|
|
|
} |
1086
|
|
|
|
1087
|
|
|
/** |
1088
|
|
|
* Wheter next link is needed for the current resource set (feed) |
1089
|
|
|
* being serialized. |
1090
|
|
|
* |
1091
|
|
|
* @param int $resultSetCount Number of entries in the current |
1092
|
|
|
* resource set |
1093
|
|
|
* |
1094
|
|
|
* @return bool true if the feed must have a next page link |
1095
|
|
|
*/ |
1096
|
|
|
protected function needNextPageLink($resultSetCount) |
1097
|
|
|
{ |
1098
|
|
|
$currentResourceSet = $this->getCurrentResourceSetWrapper(); |
1099
|
|
|
$recursionLevel = count($this->getStack()->getSegmentNames()); |
1100
|
|
|
$pageSize = $currentResourceSet->getResourceSetPageSize(); |
1101
|
|
|
|
1102
|
|
|
if (1 == $recursionLevel) { |
1103
|
|
|
//presence of $top option affect next link for root container |
1104
|
|
|
$topValueCount = $this->getRequest()->getTopOptionCount(); |
1105
|
|
|
if (null !== $topValueCount && ($topValueCount <= $pageSize)) { |
1106
|
|
|
return false; |
1107
|
|
|
} |
1108
|
|
|
} |
1109
|
|
|
|
1110
|
|
|
return $resultSetCount == $pageSize; |
1111
|
|
|
} |
1112
|
|
|
} |
1113
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.