Total Complexity | 130 |
Total Lines | 941 |
Duplicated Lines | 0 % |
Changes | 29 | ||
Bugs | 0 | Features | 0 |
Complex classes like IronicSerialiser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IronicSerialiser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class IronicSerialiser implements IObjectSerialiser |
||
50 | { |
||
51 | use SerialiseDepWrapperTrait; |
||
52 | use SerialisePropertyCacheTrait; |
||
53 | use SerialiseNavigationTrait; |
||
54 | |||
55 | /** |
||
56 | * Collection of complex type instances used for cycle detection. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $complexTypeInstanceCollection; |
||
61 | |||
62 | /** |
||
63 | * Update time to insert into ODataEntry/ODataFeed fields |
||
64 | * @var Carbon |
||
65 | */ |
||
66 | private $updated; |
||
67 | |||
68 | /** |
||
69 | * Has base URI already been written out during serialisation? |
||
70 | * @var bool |
||
71 | */ |
||
72 | private $isBaseWritten = false; |
||
73 | |||
74 | /** |
||
75 | * @param IService $service Reference to the data service instance |
||
76 | * @param RequestDescription|null $request Type instance describing the client submitted request |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | public function __construct(IService $service, RequestDescription $request = null) |
||
80 | { |
||
81 | $this->service = $service; |
||
82 | $this->request = $request; |
||
83 | $this->absoluteServiceUri = $service->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
84 | $this->absoluteServiceUriWithSlash = rtrim($this->absoluteServiceUri, '/') . '/'; |
||
85 | $this->stack = new SegmentStack($request); |
||
86 | $this->complexTypeInstanceCollection = []; |
||
87 | $this->modelSerialiser = new ModelSerialiser(); |
||
88 | $this->updated = Carbon::now(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Write a top level entry resource. |
||
93 | * |
||
94 | * @param QueryResult $entryObject Reference to the entry object to be written |
||
95 | * |
||
96 | * @return ODataEntry|null |
||
97 | * @throws InvalidOperationException |
||
98 | * @throws \ReflectionException |
||
99 | * @throws ODataException |
||
100 | */ |
||
101 | public function writeTopLevelElement(QueryResult $entryObject) |
||
102 | { |
||
103 | if (!isset($entryObject->results)) { |
||
104 | array_pop($this->lightStack); |
||
105 | return null; |
||
106 | } |
||
107 | if (!$entryObject->results instanceof Model) { |
||
108 | $res = $entryObject->results; |
||
109 | $msg = is_array($res) ? 'Entry object must be single Model' : get_class($res); |
||
110 | throw new InvalidOperationException($msg); |
||
111 | } |
||
112 | |||
113 | $this->loadStackIfEmpty(); |
||
114 | $baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
||
115 | $this->isBaseWritten = true; |
||
116 | |||
117 | $stackCount = count($this->lightStack); |
||
118 | $topOfStack = $this->lightStack[$stackCount-1]; |
||
119 | $payloadClass = get_class($entryObject->results); |
||
120 | /** @var ResourceEntityType $resourceType */ |
||
121 | $resourceType = $this->getService()->getProvidersWrapper()->resolveResourceType($topOfStack['type']); |
||
122 | |||
123 | // need gubbinz to unpack an abstract resource type |
||
124 | $resourceType = $this->getConcreteTypeFromAbstractType($resourceType, $payloadClass); |
||
125 | |||
126 | // make sure we're barking up right tree |
||
127 | if (!$resourceType instanceof ResourceEntityType) { |
||
128 | throw new InvalidOperationException(get_class($resourceType)); |
||
129 | } |
||
130 | |||
131 | /** @var Model $res */ |
||
132 | $res = $entryObject->results; |
||
133 | $targClass = $resourceType->getInstanceType()->getName(); |
||
134 | if (!($res instanceof $targClass)) { |
||
135 | $msg = 'Object being serialised not instance of expected class, ' |
||
136 | . $targClass . ', is actually ' . $payloadClass; |
||
137 | throw new InvalidOperationException($msg); |
||
138 | } |
||
139 | |||
140 | $this->checkRelationPropertiesCached($targClass, $resourceType); |
||
141 | /** @var ResourceProperty[] $relProp */ |
||
142 | $relProp = $this->propertiesCache[$targClass]['rel']; |
||
143 | /** @var ResourceProperty[] $nonRelProp */ |
||
144 | $nonRelProp = $this->propertiesCache[$targClass]['nonRel']; |
||
145 | |||
146 | $resourceSet = $resourceType->getCustomState(); |
||
147 | if (!$resourceSet instanceof ResourceSet) { |
||
148 | throw new InvalidOperationException(''); |
||
149 | } |
||
150 | $title = $resourceType->getName(); |
||
151 | $type = $resourceType->getFullName(); |
||
152 | |||
153 | $relativeUri = $this->getEntryInstanceKey( |
||
154 | $res, |
||
155 | $resourceType, |
||
156 | $resourceSet->getName() |
||
157 | ); |
||
158 | $absoluteUri = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
159 | |||
160 | /** var $mediaLink ODataMediaLink|null */ |
||
161 | $mediaLink = null; |
||
162 | /** var $mediaLinks ODataMediaLink[] */ |
||
163 | $mediaLinks = []; |
||
164 | $this->writeMediaData( |
||
165 | $res, |
||
166 | $type, |
||
167 | $relativeUri, |
||
168 | $resourceType, |
||
169 | $mediaLink, |
||
170 | $mediaLinks |
||
171 | ); |
||
172 | |||
173 | $propertyContent = $this->writePrimitiveProperties($res, $nonRelProp); |
||
174 | |||
175 | $links = $this->buildLinksFromRels($entryObject, $relProp, $relativeUri); |
||
176 | |||
177 | $odata = new ODataEntry(); |
||
178 | $odata->resourceSetName = $resourceSet->getName(); |
||
179 | $odata->id = $absoluteUri; |
||
180 | $odata->title = new ODataTitle($title); |
||
181 | $odata->type = new ODataCategory($type); |
||
182 | $odata->propertyContent = $propertyContent; |
||
183 | $odata->isMediaLinkEntry = $resourceType->isMediaLinkEntry(); |
||
184 | $odata->editLink = new ODataLink(); |
||
185 | $odata->editLink->url = $relativeUri; |
||
186 | $odata->editLink->name = 'edit'; |
||
187 | $odata->editLink->title = $title; |
||
188 | $odata->mediaLink = $mediaLink; |
||
189 | $odata->mediaLinks = $mediaLinks; |
||
190 | $odata->links = $links; |
||
191 | $odata->updated = $this->getUpdated()->format(DATE_ATOM); |
||
192 | $odata->baseURI = $baseURI; |
||
193 | |||
194 | $newCount = count($this->lightStack); |
||
195 | if ($newCount != $stackCount) { |
||
196 | $msg = 'Should have ' . $stackCount . ' elements in stack, have ' . $newCount . ' elements'; |
||
197 | throw new InvalidOperationException($msg); |
||
198 | } |
||
199 | $this->lightStack[$newCount-1]['count']--; |
||
200 | if (0 == $this->lightStack[$newCount-1]['count']) { |
||
201 | array_pop($this->lightStack); |
||
202 | } |
||
203 | return $odata; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Write top level feed element. |
||
208 | * |
||
209 | * @param QueryResult &$entryObjects Array of entry resources to be written |
||
210 | * |
||
211 | * @return ODataFeed |
||
212 | * @throws InvalidOperationException |
||
213 | * @throws ODataException |
||
214 | * @throws \ReflectionException |
||
215 | */ |
||
216 | public function writeTopLevelElements(QueryResult &$entryObjects) |
||
217 | { |
||
218 | $res = $entryObjects->results; |
||
219 | if (!(is_array($res) || $res instanceof Collection)) { |
||
220 | throw new InvalidOperationException('!is_array($entryObjects->results)'); |
||
221 | } |
||
222 | if (is_array($res) && 0 == count($res)) { |
||
223 | $entryObjects->hasMore = false; |
||
224 | } |
||
225 | if ($res instanceof Collection && 0 == $res->count()) { |
||
226 | $entryObjects->hasMore = false; |
||
227 | } |
||
228 | |||
229 | $this->loadStackIfEmpty(); |
||
230 | $setName = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
231 | |||
232 | $title = $this->getRequest()->getContainerName(); |
||
233 | $relativeUri = $this->getRequest()->getIdentifier(); |
||
234 | $absoluteUri = $this->getRequest()->getRequestUrl()->getUrlAsString(); |
||
235 | |||
236 | $selfLink = new ODataLink(); |
||
237 | $selfLink->name = 'self'; |
||
238 | $selfLink->title = $relativeUri; |
||
239 | $selfLink->url = $relativeUri; |
||
240 | |||
241 | $odata = new ODataFeed(); |
||
242 | $odata->title = new ODataTitle($title); |
||
243 | $odata->id = $absoluteUri; |
||
244 | $odata->selfLink = $selfLink; |
||
245 | $odata->updated = $this->getUpdated()->format(DATE_ATOM); |
||
246 | $odata->baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
||
247 | $this->isBaseWritten = true; |
||
248 | |||
249 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
250 | $odata->rowCount = $this->getRequest()->getCountValue(); |
||
251 | } |
||
252 | foreach ($res as $entry) { |
||
253 | if (!$entry instanceof QueryResult) { |
||
254 | $query = new QueryResult(); |
||
255 | $query->results = $entry; |
||
256 | } else { |
||
257 | $query = $entry; |
||
258 | } |
||
259 | if (!$query instanceof QueryResult) { |
||
260 | throw new InvalidOperationException(get_class($query)); |
||
261 | } |
||
262 | $odata->entries[] = $this->writeTopLevelElement($query); |
||
263 | } |
||
264 | |||
265 | $resourceSet = $this->getRequest()->getTargetResourceSetWrapper()->getResourceSet(); |
||
266 | $requestTop = $this->getRequest()->getTopOptionCount(); |
||
267 | $pageSize = $this->getService()->getConfiguration()->getEntitySetPageSize($resourceSet); |
||
268 | $requestTop = (null === $requestTop) ? $pageSize+1 : $requestTop; |
||
269 | |||
270 | if (true === $entryObjects->hasMore && $requestTop > $pageSize) { |
||
271 | $stackSegment = $setName; |
||
272 | $lastObject = end($entryObjects->results); |
||
273 | $segment = $this->getNextLinkUri($lastObject); |
||
274 | $nextLink = new ODataLink(); |
||
275 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
276 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
277 | $odata->nextPageLink = $nextLink; |
||
278 | } |
||
279 | |||
280 | return $odata; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Write top level url element. |
||
285 | * |
||
286 | * @param QueryResult $entryObject The entry resource whose url to be written |
||
287 | * |
||
288 | * @return ODataURL |
||
289 | * @throws InvalidOperationException |
||
290 | * @throws ODataException |
||
291 | * @throws \ReflectionException |
||
292 | */ |
||
293 | public function writeUrlElement(QueryResult $entryObject) |
||
294 | { |
||
295 | $url = new ODataURL(); |
||
296 | /** @var Model|null $res */ |
||
297 | $res = $entryObject->results; |
||
298 | if (null !== $res) { |
||
299 | $currentResourceType = $this->getCurrentResourceSetWrapper()->getResourceType(); |
||
300 | $relativeUri = $this->getEntryInstanceKey( |
||
301 | $res, |
||
302 | $currentResourceType, |
||
303 | $this->getCurrentResourceSetWrapper()->getName() |
||
304 | ); |
||
305 | |||
306 | $url->url = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
307 | } |
||
308 | |||
309 | return $url; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Write top level url collection. |
||
314 | * |
||
315 | * @param QueryResult $entryObjects Array of entry resources whose url to be written |
||
316 | * |
||
317 | * @return ODataURLCollection |
||
318 | * @throws InvalidOperationException |
||
319 | * @throws ODataException |
||
320 | * @throws \ReflectionException |
||
321 | */ |
||
322 | public function writeUrlElements(QueryResult $entryObjects) |
||
323 | { |
||
324 | $urls = new ODataURLCollection(); |
||
325 | if (!empty($entryObjects->results)) { |
||
326 | $i = 0; |
||
327 | foreach ($entryObjects->results as $entryObject) { |
||
328 | if (!$entryObject instanceof QueryResult) { |
||
329 | $query = new QueryResult(); |
||
330 | $query->results = $entryObject; |
||
331 | } else { |
||
332 | $query = $entryObject; |
||
333 | } |
||
334 | $urls->urls[$i] = $this->writeUrlElement($query); |
||
335 | ++$i; |
||
336 | } |
||
337 | |||
338 | if ($i > 0 && true === $entryObjects->hasMore) { |
||
339 | $stackSegment = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
340 | $lastObject = end($entryObjects->results); |
||
|
|||
341 | $segment = $this->getNextLinkUri($lastObject); |
||
342 | $nextLink = new ODataLink(); |
||
343 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
344 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
345 | $urls->nextPageLink = $nextLink; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
350 | $urls->count = $this->getRequest()->getCountValue(); |
||
351 | } |
||
352 | |||
353 | return $urls; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Write top level complex resource. |
||
358 | * |
||
359 | * @param QueryResult &$complexValue The complex object to be written |
||
360 | * @param string $propertyName The name of the complex property |
||
361 | * @param ResourceType &$resourceType Describes the type of complex object |
||
362 | * |
||
363 | * @return ODataPropertyContent |
||
364 | * @throws InvalidOperationException |
||
365 | * @throws \ReflectionException |
||
366 | */ |
||
367 | public function writeTopLevelComplexObject(QueryResult &$complexValue, $propertyName, ResourceType &$resourceType) |
||
368 | { |
||
369 | $result = $complexValue->results; |
||
370 | |||
371 | $propertyContent = new ODataPropertyContent(); |
||
372 | $odataProperty = new ODataProperty(); |
||
373 | $odataProperty->name = $propertyName; |
||
374 | $odataProperty->typeName = $resourceType->getFullName(); |
||
375 | if (null != $result) { |
||
376 | $internalContent = $this->writeComplexValue($resourceType, $result); |
||
377 | $odataProperty->value = $internalContent; |
||
378 | } |
||
379 | |||
380 | $propertyContent->properties[$propertyName] = $odataProperty; |
||
381 | |||
382 | return $propertyContent; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Write top level bag resource. |
||
387 | * |
||
388 | * @param QueryResult &$BagValue The bag object to be |
||
389 | * written |
||
390 | * @param string $propertyName The name of the |
||
391 | * bag property |
||
392 | * @param ResourceType &$resourceType Describes the type of |
||
393 | * bag object |
||
394 | * |
||
395 | * @return ODataPropertyContent |
||
396 | * @throws InvalidOperationException |
||
397 | * @throws \ReflectionException |
||
398 | */ |
||
399 | public function writeTopLevelBagObject(QueryResult &$BagValue, $propertyName, ResourceType &$resourceType) |
||
400 | { |
||
401 | $result = $BagValue->results; |
||
402 | |||
403 | $propertyContent = new ODataPropertyContent(); |
||
404 | $odataProperty = new ODataProperty(); |
||
405 | $odataProperty->name = $propertyName; |
||
406 | $odataProperty->typeName = 'Collection(' . $resourceType->getFullName() . ')'; |
||
407 | $odataProperty->value = $this->writeBagValue($resourceType, $result); |
||
408 | |||
409 | $propertyContent->properties[$propertyName] = $odataProperty; |
||
410 | return $propertyContent; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Write top level primitive value. |
||
415 | * |
||
416 | * @param QueryResult &$primitiveValue The primitive value to be |
||
417 | * written |
||
418 | * @param ResourceProperty &$resourceProperty Resource property describing the |
||
419 | * primitive property to be written |
||
420 | * @return ODataPropertyContent |
||
421 | * @throws InvalidOperationException |
||
422 | * @throws \ReflectionException |
||
423 | */ |
||
424 | public function writeTopLevelPrimitive(QueryResult &$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
425 | { |
||
426 | if (null === $resourceProperty) { |
||
427 | throw new InvalidOperationException('Resource property must not be null'); |
||
428 | } |
||
429 | $propertyContent = new ODataPropertyContent(); |
||
430 | |||
431 | $odataProperty = new ODataProperty(); |
||
432 | $odataProperty->name = $resourceProperty->getName(); |
||
433 | $iType = $resourceProperty->getInstanceType(); |
||
434 | if (!$iType instanceof IType) { |
||
435 | throw new InvalidOperationException(get_class($iType)); |
||
436 | } |
||
437 | $odataProperty->typeName = $iType->getFullTypeName(); |
||
438 | if (null == $primitiveValue->results) { |
||
439 | $odataProperty->value = null; |
||
440 | } else { |
||
441 | $rType = $resourceProperty->getResourceType()->getInstanceType(); |
||
442 | if (!$rType instanceof IType) { |
||
443 | throw new InvalidOperationException(get_class($rType)); |
||
444 | } |
||
445 | $odataProperty->value = $this->primitiveToString($rType, $primitiveValue->results); |
||
446 | } |
||
447 | |||
448 | $propertyContent->properties[$odataProperty->name] = $odataProperty; |
||
449 | |||
450 | return $propertyContent; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Get update timestamp. |
||
455 | * |
||
456 | * @return Carbon |
||
457 | */ |
||
458 | public function getUpdated() |
||
459 | { |
||
460 | return $this->updated; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param Model $entityInstance |
||
465 | * @param ResourceType $resourceType |
||
466 | * @param string $containerName |
||
467 | * @return string |
||
468 | * @throws InvalidOperationException |
||
469 | * @throws ODataException |
||
470 | * @throws \ReflectionException |
||
471 | */ |
||
472 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
473 | { |
||
474 | $typeName = $resourceType->getName(); |
||
475 | $keyProperties = $resourceType->getKeyProperties(); |
||
476 | if (0 == count($keyProperties)) { |
||
477 | throw new InvalidOperationException('count($keyProperties) == 0'); |
||
478 | } |
||
479 | $keyString = $containerName . '('; |
||
480 | $comma = null; |
||
481 | foreach ($keyProperties as $keyName => $resourceProperty) { |
||
482 | $keyType = $resourceProperty->getInstanceType(); |
||
483 | if (!$keyType instanceof IType) { |
||
484 | throw new InvalidOperationException('$keyType not instanceof IType'); |
||
485 | } |
||
486 | $keyName = $resourceProperty->getName(); |
||
487 | $keyValue = $entityInstance->$keyName; |
||
488 | if (!isset($keyValue)) { |
||
489 | throw ODataException::createInternalServerError( |
||
490 | Messages::badQueryNullKeysAreNotSupported($typeName, $keyName) |
||
491 | ); |
||
492 | } |
||
493 | |||
494 | $keyValue = $keyType->convertToOData($keyValue); |
||
495 | $keyString .= $comma . $keyName . '=' . $keyValue; |
||
496 | $comma = ','; |
||
497 | } |
||
498 | |||
499 | $keyString .= ')'; |
||
500 | |||
501 | return $keyString; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param $entryObject |
||
506 | * @param $type |
||
507 | * @param $relativeUri |
||
508 | * @param ResourceType $resourceType |
||
509 | * @param ODataMediaLink|null $mediaLink |
||
510 | * @param ODataMediaLink[] $mediaLinks |
||
511 | * @return void |
||
512 | * @throws InvalidOperationException |
||
513 | */ |
||
514 | protected function writeMediaData( |
||
515 | $entryObject, |
||
516 | $type, |
||
517 | $relativeUri, |
||
518 | ResourceType $resourceType, |
||
519 | ODataMediaLink &$mediaLink = null, |
||
520 | array &$mediaLinks = [] |
||
521 | ) { |
||
522 | $context = $this->getService()->getOperationContext(); |
||
523 | $streamProviderWrapper = $this->getService()->getStreamProviderWrapper(); |
||
524 | if (null == $streamProviderWrapper) { |
||
525 | throw new InvalidOperationException('Retrieved stream provider must not be null'); |
||
526 | } |
||
527 | |||
528 | /** @var ODataMediaLink|null $mediaLink */ |
||
529 | $mediaLink = null; |
||
530 | if ($resourceType->isMediaLinkEntry()) { |
||
531 | $eTag = $streamProviderWrapper->getStreamETag2($entryObject, null, $context); |
||
532 | $mediaLink = new ODataMediaLink($type, '/$value', $relativeUri . '/$value', '*/*', $eTag, 'edit-media'); |
||
533 | } |
||
534 | /** @var ODataMediaLink[] $mediaLinks */ |
||
535 | $mediaLinks = []; |
||
536 | if ($resourceType->hasNamedStream()) { |
||
537 | $namedStreams = $resourceType->getAllNamedStreams(); |
||
538 | foreach ($namedStreams as $streamTitle => $resourceStreamInfo) { |
||
539 | $readUri = $streamProviderWrapper->getReadStreamUri2( |
||
540 | $entryObject, |
||
541 | $resourceStreamInfo, |
||
542 | $context, |
||
543 | $relativeUri |
||
544 | ); |
||
545 | $mediaContentType = $streamProviderWrapper->getStreamContentType2( |
||
546 | $entryObject, |
||
547 | $resourceStreamInfo, |
||
548 | $context |
||
549 | ); |
||
550 | $eTag = $streamProviderWrapper->getStreamETag2( |
||
551 | $entryObject, |
||
552 | $resourceStreamInfo, |
||
553 | $context |
||
554 | ); |
||
555 | |||
556 | $nuLink = new ODataMediaLink($streamTitle, $readUri, $readUri, $mediaContentType, $eTag); |
||
557 | $mediaLinks[] = $nuLink; |
||
558 | } |
||
559 | } |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Wheter next link is needed for the current resource set (feed) |
||
564 | * being serialized. |
||
565 | * |
||
566 | * @param int $resultSetCount Number of entries in the current |
||
567 | * resource set |
||
568 | * |
||
569 | * @return bool true if the feed must have a next page link |
||
570 | * @throws InvalidOperationException |
||
571 | */ |
||
572 | protected function needNextPageLink($resultSetCount) |
||
573 | { |
||
574 | $currentResourceSet = $this->getCurrentResourceSetWrapper(); |
||
575 | $recursionLevel = count($this->getStack()->getSegmentNames()); |
||
576 | $pageSize = $currentResourceSet->getResourceSetPageSize(); |
||
577 | |||
578 | if (1 == $recursionLevel) { |
||
579 | //presence of $top option affect next link for root container |
||
580 | $topValueCount = $this->getRequest()->getTopOptionCount(); |
||
581 | if (null !== $topValueCount && ($topValueCount <= $pageSize)) { |
||
582 | return false; |
||
583 | } |
||
584 | } |
||
585 | return $resultSetCount == $pageSize; |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Get next page link from the given entity instance. |
||
590 | * |
||
591 | * @param mixed &$lastObject Last object serialized to be |
||
592 | * used for generating |
||
593 | * $skiptoken |
||
594 | * @throws ODataException |
||
595 | * @return string for the link for next page |
||
596 | * @throws InvalidOperationException |
||
597 | */ |
||
598 | protected function getNextLinkUri(&$lastObject) |
||
599 | { |
||
600 | $currentExpandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
||
601 | $internalOrderByInfo = $currentExpandedProjectionNode->getInternalOrderByInfo(); |
||
602 | if (null === $internalOrderByInfo) { |
||
603 | throw new InvalidOperationException('Null'); |
||
604 | } |
||
605 | if (!$internalOrderByInfo instanceof InternalOrderByInfo) { |
||
606 | throw new InvalidOperationException(get_class($internalOrderByInfo)); |
||
607 | } |
||
608 | $numSegments = count($internalOrderByInfo->getOrderByPathSegments()); |
||
609 | $queryParameterString = $this->getNextPageLinkQueryParametersForRootResourceSet(); |
||
610 | |||
611 | $skipToken = $internalOrderByInfo->buildSkipTokenValue($lastObject); |
||
612 | if (empty($skipToken)) { |
||
613 | throw new InvalidOperationException('!is_null($skipToken)'); |
||
614 | } |
||
615 | $token = (1 < $numSegments) ? '$skiptoken=' : '$skip='; |
||
616 | $skipToken = (1 < $numSegments) ? $skipToken : intval(trim($skipToken, '\'')); |
||
617 | $skipToken = '?' . $queryParameterString . $token . $skipToken; |
||
618 | |||
619 | return $skipToken; |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Builds the string corresponding to query parameters for top level results |
||
624 | * (result set identified by the resource path) to be put in next page link. |
||
625 | * |
||
626 | * @return string|null string representing the query parameters in the URI |
||
627 | * query parameter format, NULL if there |
||
628 | * is no query parameters |
||
629 | * required for the next link of top level result set |
||
630 | * @throws InvalidOperationException |
||
631 | */ |
||
632 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
633 | { |
||
634 | /** @var string|null $queryParameterString */ |
||
635 | $queryParameterString = null; |
||
636 | foreach ([ODataConstants::HTTPQUERY_STRING_FILTER, |
||
637 | ODataConstants::HTTPQUERY_STRING_EXPAND, |
||
638 | ODataConstants::HTTPQUERY_STRING_ORDERBY, |
||
639 | ODataConstants::HTTPQUERY_STRING_INLINECOUNT, |
||
640 | ODataConstants::HTTPQUERY_STRING_SELECT, ] as $queryOption) { |
||
641 | /** @var string|null $value */ |
||
642 | $value = $this->getService()->getHost()->getQueryStringItem($queryOption); |
||
643 | if (null !== $value) { |
||
644 | if (null !== $queryParameterString) { |
||
645 | $queryParameterString = /** @scrutinizer ignore-type */$queryParameterString . '&'; |
||
646 | } |
||
647 | |||
648 | $queryParameterString .= $queryOption . '=' . $value; |
||
649 | } |
||
650 | } |
||
651 | |||
652 | $topCountValue = $this->getRequest()->getTopOptionCount(); |
||
653 | if (null !== $topCountValue) { |
||
654 | $remainingCount = $topCountValue-$this->getRequest()->getTopCount(); |
||
655 | if (0 < $remainingCount) { |
||
656 | if (null !== $queryParameterString) { |
||
657 | $queryParameterString .= '&'; |
||
658 | } |
||
659 | |||
660 | $queryParameterString .= ODataConstants::HTTPQUERY_STRING_TOP . '=' . $remainingCount; |
||
661 | } |
||
662 | } |
||
663 | |||
664 | if (null !== $queryParameterString) { |
||
665 | $queryParameterString .= '&'; |
||
666 | } |
||
667 | |||
668 | return $queryParameterString; |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * Convert the given primitive value to string. |
||
673 | * Note: This method will not handle null primitive value. |
||
674 | * |
||
675 | * @param IType &$type Type of the primitive property needing conversion |
||
676 | * @param mixed $primitiveValue Primitive value to convert |
||
677 | * |
||
678 | * @return string |
||
679 | */ |
||
680 | private function primitiveToString(IType &$type, $primitiveValue) |
||
681 | { |
||
682 | // kludge to enable switching on type of $type without getting tripped up by mocks as we would with get_class |
||
683 | // switch (true) means we unconditionally enter, and then lean on case statements to match given block |
||
684 | switch (true) { |
||
685 | case $type instanceof StringType: |
||
686 | $stringValue = utf8_encode($primitiveValue); |
||
687 | break; |
||
688 | case $type instanceof Boolean: |
||
689 | $stringValue = (true === $primitiveValue) ? 'true' : 'false'; |
||
690 | break; |
||
691 | case $type instanceof Binary: |
||
692 | $stringValue = base64_encode($primitiveValue); |
||
693 | break; |
||
694 | case $type instanceof DateTime && $primitiveValue instanceof \DateTime: |
||
695 | $stringValue = $primitiveValue->format(\DateTime::ATOM); |
||
696 | break; |
||
697 | default: |
||
698 | $stringValue = strval($primitiveValue); |
||
699 | } |
||
700 | |||
701 | return $stringValue; |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * @param $entryObject |
||
706 | * @param $nonRelProp |
||
707 | * @return ODataPropertyContent |
||
708 | * @throws InvalidOperationException |
||
709 | */ |
||
710 | private function writePrimitiveProperties(Model $entryObject, $nonRelProp) |
||
729 | } |
||
730 | |||
731 | /** |
||
732 | * @param QueryResult $entryObject |
||
733 | * @param ResourceProperty $prop |
||
734 | * @param $nuLink |
||
735 | * @param $propKind |
||
736 | * @param $propName |
||
737 | * @throws InvalidOperationException |
||
738 | * @throws ODataException |
||
739 | * @throws \ReflectionException |
||
740 | */ |
||
741 | private function expandNavigationProperty( |
||
799 | } |
||
800 | } |
||
801 | |||
802 | /** |
||
803 | * @param ResourceType $resourceType |
||
804 | * @param $result |
||
805 | * @return ODataBagContent|null |
||
806 | * @throws InvalidOperationException |
||
807 | * @throws \ReflectionException |
||
808 | */ |
||
809 | protected function writeBagValue(ResourceType &$resourceType, $result) |
||
810 | { |
||
811 | if (!(null == $result || is_array($result))) { |
||
812 | throw new InvalidOperationException('Bag parameter must be null or array'); |
||
813 | } |
||
814 | $typeKind = $resourceType->getResourceTypeKind(); |
||
815 | $kVal = $typeKind; |
||
816 | if (!(ResourceTypeKind::PRIMITIVE() == $kVal || ResourceTypeKind::COMPLEX() == $kVal)) { |
||
817 | $msg = '$bagItemResourceTypeKind != ResourceTypeKind::PRIMITIVE' |
||
818 | .' && $bagItemResourceTypeKind != ResourceTypeKind::COMPLEX'; |
||
819 | throw new InvalidOperationException($msg); |
||
820 | } |
||
821 | if (null == $result) { |
||
822 | return null; |
||
823 | } |
||
824 | $bag = new ODataBagContent(); |
||
825 | $result = array_filter($result); |
||
826 | foreach ($result as $value) { |
||
827 | if (ResourceTypeKind::PRIMITIVE() == $kVal) { |
||
828 | $instance = $resourceType->getInstanceType(); |
||
829 | if (!$instance instanceof IType) { |
||
830 | throw new InvalidOperationException(get_class($instance)); |
||
831 | } |
||
832 | $bag->propertyContents[] = $this->primitiveToString($instance, $value); |
||
833 | } elseif (ResourceTypeKind::COMPLEX() == $kVal) { |
||
834 | $bag->propertyContents[] = $this->writeComplexValue($resourceType, $value); |
||
835 | } |
||
836 | } |
||
837 | return $bag; |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * @param ResourceType $resourceType |
||
842 | * @param object $result |
||
843 | * @param string|null $propertyName |
||
844 | * @return ODataPropertyContent |
||
845 | * @throws InvalidOperationException |
||
846 | * @throws \ReflectionException |
||
847 | */ |
||
848 | protected function writeComplexValue(ResourceType &$resourceType, &$result, $propertyName = null) |
||
849 | { |
||
850 | if (!is_object($result)) { |
||
851 | throw new InvalidOperationException('Supplied $customObject must be an object'); |
||
852 | } |
||
853 | |||
854 | $count = count($this->complexTypeInstanceCollection); |
||
855 | for ($i = 0; $i < $count; ++$i) { |
||
856 | if ($this->complexTypeInstanceCollection[$i] === $result) { |
||
857 | throw new InvalidOperationException( |
||
858 | Messages::objectModelSerializerLoopsNotAllowedInComplexTypes($propertyName) |
||
859 | ); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | $this->complexTypeInstanceCollection[$count] = &$result; |
||
864 | |||
865 | $internalContent = new ODataPropertyContent(); |
||
866 | $resourceProperties = $resourceType->getAllProperties(); |
||
867 | // first up, handle primitive properties |
||
868 | foreach ($resourceProperties as $prop) { |
||
869 | $resourceKind = $prop->getKind(); |
||
870 | $propName = $prop->getName(); |
||
871 | $internalProperty = new ODataProperty(); |
||
872 | $internalProperty->name = $propName; |
||
873 | if (static::isMatchPrimitive($resourceKind)) { |
||
874 | $iType = $prop->getInstanceType(); |
||
875 | if (!$iType instanceof IType) { |
||
876 | throw new InvalidOperationException(get_class($iType)); |
||
877 | } |
||
878 | $internalProperty->typeName = $iType->getFullTypeName(); |
||
879 | |||
880 | $rType = $prop->getResourceType()->getInstanceType(); |
||
881 | if (!$rType instanceof IType) { |
||
882 | throw new InvalidOperationException(get_class($rType)); |
||
883 | } |
||
884 | |||
885 | $internalProperty->value = $this->primitiveToString($rType, $result->$propName); |
||
886 | |||
887 | $internalContent->properties[$propName] = $internalProperty; |
||
888 | } elseif (ResourcePropertyKind::COMPLEX_TYPE == $resourceKind) { |
||
889 | $rType = $prop->getResourceType(); |
||
890 | $internalProperty->typeName = $rType->getFullName(); |
||
891 | $internalProperty->value = $this->writeComplexValue($rType, $result->$propName, $propName); |
||
892 | |||
893 | $internalContent->properties[$propName] = $internalProperty; |
||
894 | } |
||
895 | } |
||
896 | |||
897 | unset($this->complexTypeInstanceCollection[$count]); |
||
898 | return $internalContent; |
||
899 | } |
||
900 | |||
901 | public static function isMatchPrimitive($resourceKind) |
||
910 | } |
||
911 | |||
912 | /** |
||
913 | * @param ResourceEntityType $resourceType |
||
914 | * @param $payloadClass |
||
915 | * @return ResourceEntityType|ResourceType |
||
916 | * @throws InvalidOperationException |
||
917 | * @throws \ReflectionException |
||
918 | */ |
||
919 | protected function getConcreteTypeFromAbstractType(ResourceEntityType $resourceType, $payloadClass) |
||
920 | { |
||
921 | if ($resourceType->isAbstract()) { |
||
922 | $derived = $this->getMetadata()->getDerivedTypes($resourceType); |
||
923 | if (0 == count($derived)) { |
||
924 | throw new InvalidOperationException('Supplied abstract type must have at least one derived type'); |
||
925 | } |
||
926 | foreach ($derived as $rawType) { |
||
927 | if (!$rawType->isAbstract()) { |
||
928 | $name = $rawType->getInstanceType()->getName(); |
||
929 | if ($payloadClass == $name) { |
||
930 | $resourceType = $rawType; |
||
931 | break; |
||
932 | } |
||
933 | } |
||
934 | } |
||
935 | } |
||
936 | // despite all set up, checking, etc, if we haven't picked a concrete resource type, |
||
937 | // wheels have fallen off, so blow up |
||
938 | if ($resourceType->isAbstract()) { |
||
939 | throw new InvalidOperationException('Concrete resource type not selected for payload ' . $payloadClass); |
||
940 | } |
||
941 | return $resourceType; |
||
942 | } |
||
943 | |||
944 | /** |
||
945 | * @param QueryResult $entryObject |
||
946 | * @param array $relProp |
||
947 | * @param $relativeUri |
||
948 | * @return array |
||
949 | * @throws InvalidOperationException |
||
950 | * @throws ODataException |
||
951 | * @throws \ReflectionException |
||
952 | */ |
||
953 | protected function buildLinksFromRels(QueryResult $entryObject, array $relProp, $relativeUri) |
||
990 | } |
||
991 | } |
||
992 |