Total Complexity | 131 |
Total Lines | 944 |
Duplicated Lines | 0 % |
Changes | 30 | ||
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) |
||
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) |
||
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 | $isArray = is_array($res); |
||
220 | $isColl = !$isArray && $res instanceof Collection; |
||
221 | if (!($isArray || $isColl)) { |
||
222 | throw new InvalidOperationException('!is_array($entryObjects->results)'); |
||
223 | } |
||
224 | if ($isArray && 0 == count($res)) { |
||
225 | $entryObjects->hasMore = false; |
||
226 | } |
||
227 | if ($isColl && 0 == $res->count()) { |
||
|
|||
228 | $entryObjects->hasMore = false; |
||
229 | } |
||
230 | |||
231 | $this->loadStackIfEmpty(); |
||
232 | $setName = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
233 | |||
234 | $title = $this->getRequest()->getContainerName(); |
||
235 | $relativeUri = $this->getRequest()->getIdentifier(); |
||
236 | $absoluteUri = $this->getRequest()->getRequestUrl()->getUrlAsString(); |
||
237 | |||
238 | $selfLink = new ODataLink(); |
||
239 | $selfLink->name = 'self'; |
||
240 | $selfLink->title = $relativeUri; |
||
241 | $selfLink->url = $relativeUri; |
||
242 | |||
243 | $odata = new ODataFeed(); |
||
244 | $odata->title = new ODataTitle($title); |
||
245 | $odata->id = $absoluteUri; |
||
246 | $odata->selfLink = $selfLink; |
||
247 | $odata->updated = $this->getUpdated()->format(DATE_ATOM); |
||
248 | $odata->baseURI = $this->isBaseWritten ? null : $this->absoluteServiceUriWithSlash; |
||
249 | $this->isBaseWritten = true; |
||
250 | |||
251 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
252 | $odata->rowCount = $this->getRequest()->getCountValue(); |
||
253 | } |
||
254 | foreach ($res as $entry) { |
||
255 | if (!$entry instanceof QueryResult) { |
||
256 | $query = new QueryResult(); |
||
257 | $query->results = $entry; |
||
258 | } else { |
||
259 | $query = $entry; |
||
260 | } |
||
261 | if (!$query instanceof QueryResult) { |
||
262 | throw new InvalidOperationException(get_class($query)); |
||
263 | } |
||
264 | $odata->entries[] = $this->writeTopLevelElement($query); |
||
265 | } |
||
266 | |||
267 | $resourceSet = $this->getRequest()->getTargetResourceSetWrapper()->getResourceSet(); |
||
268 | $requestTop = $this->getRequest()->getTopOptionCount(); |
||
269 | $pageSize = $this->getService()->getConfiguration()->getEntitySetPageSize($resourceSet); |
||
270 | $requestTop = (null === $requestTop) ? $pageSize+1 : $requestTop; |
||
271 | |||
272 | if (true === $entryObjects->hasMore && $requestTop > $pageSize) { |
||
273 | $stackSegment = $setName; |
||
274 | $lastObject = end($entryObjects->results); |
||
275 | $segment = $this->getNextLinkUri($lastObject); |
||
276 | $nextLink = new ODataLink(); |
||
277 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
278 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
279 | $odata->nextPageLink = $nextLink; |
||
280 | } |
||
281 | |||
282 | return $odata; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Write top level url element. |
||
287 | * |
||
288 | * @param QueryResult $entryObject The entry resource whose url to be written |
||
289 | * |
||
290 | * @return ODataURL |
||
291 | * @throws InvalidOperationException |
||
292 | * @throws ODataException |
||
293 | * @throws \ReflectionException |
||
294 | */ |
||
295 | public function writeUrlElement(QueryResult $entryObject) |
||
296 | { |
||
297 | $url = new ODataURL(); |
||
298 | /** @var Model|null $res */ |
||
299 | $res = $entryObject->results; |
||
300 | if (null !== $res) { |
||
301 | $currentResourceType = $this->getCurrentResourceSetWrapper()->getResourceType(); |
||
302 | $relativeUri = $this->getEntryInstanceKey( |
||
303 | $res, |
||
304 | $currentResourceType, |
||
305 | $this->getCurrentResourceSetWrapper()->getName() |
||
306 | ); |
||
307 | |||
308 | $url->url = rtrim($this->absoluteServiceUri, '/') . '/' . $relativeUri; |
||
309 | } |
||
310 | |||
311 | return $url; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Write top level url collection. |
||
316 | * |
||
317 | * @param QueryResult $entryObjects Array of entry resources whose url to be written |
||
318 | * |
||
319 | * @return ODataURLCollection |
||
320 | * @throws InvalidOperationException |
||
321 | * @throws ODataException |
||
322 | * @throws \ReflectionException |
||
323 | */ |
||
324 | public function writeUrlElements(QueryResult $entryObjects) |
||
325 | { |
||
326 | $urls = new ODataURLCollection(); |
||
327 | if (!empty($entryObjects->results)) { |
||
328 | $i = 0; |
||
329 | foreach ($entryObjects->results as $entryObject) { |
||
330 | if (!$entryObject instanceof QueryResult) { |
||
331 | $query = new QueryResult(); |
||
332 | $query->results = $entryObject; |
||
333 | } else { |
||
334 | $query = $entryObject; |
||
335 | } |
||
336 | $urls->urls[$i] = $this->writeUrlElement($query); |
||
337 | ++$i; |
||
338 | } |
||
339 | |||
340 | if ($i > 0 && true === $entryObjects->hasMore) { |
||
341 | $stackSegment = $this->getRequest()->getTargetResourceSetWrapper()->getName(); |
||
342 | $lastObject = end($entryObjects->results); |
||
343 | $segment = $this->getNextLinkUri($lastObject); |
||
344 | $nextLink = new ODataLink(); |
||
345 | $nextLink->name = ODataConstants::ATOM_LINK_NEXT_ATTRIBUTE_STRING; |
||
346 | $nextLink->url = rtrim($this->absoluteServiceUri, '/') . '/' . $stackSegment . $segment; |
||
347 | $urls->nextPageLink = $nextLink; |
||
348 | } |
||
349 | } |
||
350 | |||
351 | if ($this->getRequest()->queryType == QueryType::ENTITIES_WITH_COUNT()) { |
||
352 | $urls->count = $this->getRequest()->getCountValue(); |
||
353 | } |
||
354 | |||
355 | return $urls; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Write top level complex resource. |
||
360 | * |
||
361 | * @param QueryResult &$complexValue The complex object to be written |
||
362 | * @param string $propertyName The name of the complex property |
||
363 | * @param ResourceType &$resourceType Describes the type of complex object |
||
364 | * |
||
365 | * @return ODataPropertyContent |
||
366 | * @throws InvalidOperationException |
||
367 | * @throws \ReflectionException |
||
368 | */ |
||
369 | public function writeTopLevelComplexObject(QueryResult &$complexValue, $propertyName, ResourceType &$resourceType) |
||
370 | { |
||
371 | $result = $complexValue->results; |
||
372 | |||
373 | $propertyContent = new ODataPropertyContent(); |
||
374 | $odataProperty = new ODataProperty(); |
||
375 | $odataProperty->name = $propertyName; |
||
376 | $odataProperty->typeName = $resourceType->getFullName(); |
||
377 | if (null != $result) { |
||
378 | $internalContent = $this->writeComplexValue($resourceType, $result); |
||
379 | $odataProperty->value = $internalContent; |
||
380 | } |
||
381 | |||
382 | $propertyContent->properties[$propertyName] = $odataProperty; |
||
383 | |||
384 | return $propertyContent; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Write top level bag resource. |
||
389 | * |
||
390 | * @param QueryResult &$BagValue The bag object to be |
||
391 | * written |
||
392 | * @param string $propertyName The name of the |
||
393 | * bag property |
||
394 | * @param ResourceType &$resourceType Describes the type of |
||
395 | * bag object |
||
396 | * |
||
397 | * @return ODataPropertyContent |
||
398 | * @throws InvalidOperationException |
||
399 | * @throws \ReflectionException |
||
400 | */ |
||
401 | public function writeTopLevelBagObject(QueryResult &$BagValue, $propertyName, ResourceType &$resourceType) |
||
402 | { |
||
403 | $result = $BagValue->results; |
||
404 | |||
405 | $propertyContent = new ODataPropertyContent(); |
||
406 | $odataProperty = new ODataProperty(); |
||
407 | $odataProperty->name = $propertyName; |
||
408 | $odataProperty->typeName = 'Collection(' . $resourceType->getFullName() . ')'; |
||
409 | $odataProperty->value = $this->writeBagValue($resourceType, $result); |
||
410 | |||
411 | $propertyContent->properties[$propertyName] = $odataProperty; |
||
412 | return $propertyContent; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Write top level primitive value. |
||
417 | * |
||
418 | * @param QueryResult &$primitiveValue The primitive value to be |
||
419 | * written |
||
420 | * @param ResourceProperty &$resourceProperty Resource property describing the |
||
421 | * primitive property to be written |
||
422 | * @return ODataPropertyContent |
||
423 | * @throws InvalidOperationException |
||
424 | * @throws \ReflectionException |
||
425 | */ |
||
426 | public function writeTopLevelPrimitive(QueryResult &$primitiveValue, ResourceProperty &$resourceProperty = null) |
||
427 | { |
||
428 | if (null === $resourceProperty) { |
||
429 | throw new InvalidOperationException('Resource property must not be null'); |
||
430 | } |
||
431 | $propertyContent = new ODataPropertyContent(); |
||
432 | |||
433 | $odataProperty = new ODataProperty(); |
||
434 | $odataProperty->name = $resourceProperty->getName(); |
||
435 | $iType = $resourceProperty->getInstanceType(); |
||
436 | if (!$iType instanceof IType) { |
||
437 | throw new InvalidOperationException(get_class($iType)); |
||
438 | } |
||
439 | $odataProperty->typeName = $iType->getFullTypeName(); |
||
440 | if (null == $primitiveValue->results) { |
||
441 | $odataProperty->value = null; |
||
442 | } else { |
||
443 | $rType = $resourceProperty->getResourceType()->getInstanceType(); |
||
444 | if (!$rType instanceof IType) { |
||
445 | throw new InvalidOperationException(get_class($rType)); |
||
446 | } |
||
447 | $odataProperty->value = $this->primitiveToString($rType, $primitiveValue->results); |
||
448 | } |
||
449 | |||
450 | $propertyContent->properties[$odataProperty->name] = $odataProperty; |
||
451 | |||
452 | return $propertyContent; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Get update timestamp. |
||
457 | * |
||
458 | * @return Carbon |
||
459 | */ |
||
460 | public function getUpdated() |
||
461 | { |
||
462 | return $this->updated; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * @param Model $entityInstance |
||
467 | * @param ResourceType $resourceType |
||
468 | * @param string $containerName |
||
469 | * @return string |
||
470 | * @throws InvalidOperationException |
||
471 | * @throws ODataException |
||
472 | * @throws \ReflectionException |
||
473 | */ |
||
474 | protected function getEntryInstanceKey($entityInstance, ResourceType $resourceType, $containerName) |
||
475 | { |
||
476 | $typeName = $resourceType->getName(); |
||
477 | $keyProperties = $resourceType->getKeyProperties(); |
||
478 | if (0 == count($keyProperties)) { |
||
479 | throw new InvalidOperationException('count($keyProperties) == 0'); |
||
480 | } |
||
481 | $keyString = $containerName . '('; |
||
482 | $comma = null; |
||
483 | foreach ($keyProperties as $keyName => $resourceProperty) { |
||
484 | $keyType = $resourceProperty->getInstanceType(); |
||
485 | if (!$keyType instanceof IType) { |
||
486 | throw new InvalidOperationException('$keyType not instanceof IType'); |
||
487 | } |
||
488 | $keyName = $resourceProperty->getName(); |
||
489 | $keyValue = $entityInstance->$keyName; |
||
490 | if (!isset($keyValue)) { |
||
491 | throw ODataException::createInternalServerError( |
||
492 | Messages::badQueryNullKeysAreNotSupported($typeName, $keyName) |
||
493 | ); |
||
494 | } |
||
495 | |||
496 | $keyValue = $keyType->convertToOData($keyValue); |
||
497 | $keyString .= $comma . $keyName . '=' . $keyValue; |
||
498 | $comma = ','; |
||
499 | } |
||
500 | |||
501 | $keyString .= ')'; |
||
502 | |||
503 | return $keyString; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @param $entryObject |
||
508 | * @param $type |
||
509 | * @param $relativeUri |
||
510 | * @param ResourceType $resourceType |
||
511 | * @param ODataMediaLink|null $mediaLink |
||
512 | * @param ODataMediaLink[] $mediaLinks |
||
513 | * @return void |
||
514 | * @throws InvalidOperationException |
||
515 | */ |
||
516 | protected function writeMediaData( |
||
517 | $entryObject, |
||
518 | $type, |
||
519 | $relativeUri, |
||
520 | ResourceType $resourceType, |
||
521 | ODataMediaLink &$mediaLink = null, |
||
522 | array &$mediaLinks = [] |
||
523 | ) { |
||
524 | $context = $this->getService()->getOperationContext(); |
||
525 | $streamProviderWrapper = $this->getService()->getStreamProviderWrapper(); |
||
526 | if (null == $streamProviderWrapper) { |
||
527 | throw new InvalidOperationException('Retrieved stream provider must not be null'); |
||
528 | } |
||
529 | |||
530 | /** @var ODataMediaLink|null $mediaLink */ |
||
531 | $mediaLink = null; |
||
532 | if ($resourceType->isMediaLinkEntry()) { |
||
533 | $eTag = $streamProviderWrapper->getStreamETag2($entryObject, null, $context); |
||
534 | $mediaLink = new ODataMediaLink($type, '/$value', $relativeUri . '/$value', '*/*', $eTag, 'edit-media'); |
||
535 | } |
||
536 | /** @var ODataMediaLink[] $mediaLinks */ |
||
537 | $mediaLinks = []; |
||
538 | if ($resourceType->hasNamedStream()) { |
||
539 | $namedStreams = $resourceType->getAllNamedStreams(); |
||
540 | foreach ($namedStreams as $streamTitle => $resourceStreamInfo) { |
||
541 | $readUri = $streamProviderWrapper->getReadStreamUri2( |
||
542 | $entryObject, |
||
543 | $resourceStreamInfo, |
||
544 | $context, |
||
545 | $relativeUri |
||
546 | ); |
||
547 | $mediaContentType = $streamProviderWrapper->getStreamContentType2( |
||
548 | $entryObject, |
||
549 | $resourceStreamInfo, |
||
550 | $context |
||
551 | ); |
||
552 | $eTag = $streamProviderWrapper->getStreamETag2( |
||
553 | $entryObject, |
||
554 | $resourceStreamInfo, |
||
555 | $context |
||
556 | ); |
||
557 | |||
558 | $nuLink = new ODataMediaLink($streamTitle, $readUri, $readUri, $mediaContentType, $eTag); |
||
559 | $mediaLinks[] = $nuLink; |
||
560 | } |
||
561 | } |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Wheter next link is needed for the current resource set (feed) |
||
566 | * being serialized. |
||
567 | * |
||
568 | * @param int $resultSetCount Number of entries in the current |
||
569 | * resource set |
||
570 | * |
||
571 | * @return bool true if the feed must have a next page link |
||
572 | * @throws InvalidOperationException |
||
573 | */ |
||
574 | protected function needNextPageLink($resultSetCount) |
||
575 | { |
||
576 | $currentResourceSet = $this->getCurrentResourceSetWrapper(); |
||
577 | $recursionLevel = count($this->getStack()->getSegmentNames()); |
||
578 | $pageSize = $currentResourceSet->getResourceSetPageSize(); |
||
579 | |||
580 | if (1 == $recursionLevel) { |
||
581 | //presence of $top option affect next link for root container |
||
582 | $topValueCount = $this->getRequest()->getTopOptionCount(); |
||
583 | if (null !== $topValueCount && ($topValueCount <= $pageSize)) { |
||
584 | return false; |
||
585 | } |
||
586 | } |
||
587 | return $resultSetCount == $pageSize; |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Get next page link from the given entity instance. |
||
592 | * |
||
593 | * @param mixed &$lastObject Last object serialized to be |
||
594 | * used for generating |
||
595 | * $skiptoken |
||
596 | * @throws ODataException |
||
597 | * @return string for the link for next page |
||
598 | * @throws InvalidOperationException |
||
599 | */ |
||
600 | protected function getNextLinkUri(&$lastObject) |
||
601 | { |
||
602 | $currentExpandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
||
603 | $internalOrderByInfo = $currentExpandedProjectionNode->getInternalOrderByInfo(); |
||
604 | if (null === $internalOrderByInfo) { |
||
605 | throw new InvalidOperationException('Null'); |
||
606 | } |
||
607 | if (!$internalOrderByInfo instanceof InternalOrderByInfo) { |
||
608 | throw new InvalidOperationException(get_class($internalOrderByInfo)); |
||
609 | } |
||
610 | $numSegments = count($internalOrderByInfo->getOrderByPathSegments()); |
||
611 | $queryParameterString = $this->getNextPageLinkQueryParametersForRootResourceSet(); |
||
612 | |||
613 | $skipToken = $internalOrderByInfo->buildSkipTokenValue($lastObject); |
||
614 | if (empty($skipToken)) { |
||
615 | throw new InvalidOperationException('!is_null($skipToken)'); |
||
616 | } |
||
617 | $token = (1 < $numSegments) ? '$skiptoken=' : '$skip='; |
||
618 | $skipToken = (1 < $numSegments) ? $skipToken : intval(trim($skipToken, '\'')); |
||
619 | $skipToken = '?' . $queryParameterString . $token . $skipToken; |
||
620 | |||
621 | return $skipToken; |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * Builds the string corresponding to query parameters for top level results |
||
626 | * (result set identified by the resource path) to be put in next page link. |
||
627 | * |
||
628 | * @return string|null string representing the query parameters in the URI |
||
629 | * query parameter format, NULL if there |
||
630 | * is no query parameters |
||
631 | * required for the next link of top level result set |
||
632 | * @throws InvalidOperationException |
||
633 | */ |
||
634 | protected function getNextPageLinkQueryParametersForRootResourceSet() |
||
635 | { |
||
636 | /** @var string|null $queryParameterString */ |
||
637 | $queryParameterString = null; |
||
638 | foreach ([ODataConstants::HTTPQUERY_STRING_FILTER, |
||
639 | ODataConstants::HTTPQUERY_STRING_EXPAND, |
||
640 | ODataConstants::HTTPQUERY_STRING_ORDERBY, |
||
641 | ODataConstants::HTTPQUERY_STRING_INLINECOUNT, |
||
642 | ODataConstants::HTTPQUERY_STRING_SELECT, ] as $queryOption) { |
||
643 | /** @var string|null $value */ |
||
644 | $value = $this->getService()->getHost()->getQueryStringItem($queryOption); |
||
645 | if (null !== $value) { |
||
646 | if (null !== $queryParameterString) { |
||
647 | $queryParameterString = /** @scrutinizer ignore-type */$queryParameterString . '&'; |
||
648 | } |
||
649 | |||
650 | $queryParameterString .= $queryOption . '=' . $value; |
||
651 | } |
||
652 | } |
||
653 | |||
654 | $topCountValue = $this->getRequest()->getTopOptionCount(); |
||
655 | if (null !== $topCountValue) { |
||
656 | $remainingCount = $topCountValue-$this->getRequest()->getTopCount(); |
||
657 | if (0 < $remainingCount) { |
||
658 | if (null !== $queryParameterString) { |
||
659 | $queryParameterString .= '&'; |
||
660 | } |
||
661 | |||
662 | $queryParameterString .= ODataConstants::HTTPQUERY_STRING_TOP . '=' . $remainingCount; |
||
663 | } |
||
664 | } |
||
665 | |||
666 | if (null !== $queryParameterString) { |
||
667 | $queryParameterString .= '&'; |
||
668 | } |
||
669 | |||
670 | return $queryParameterString; |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * Convert the given primitive value to string. |
||
675 | * Note: This method will not handle null primitive value. |
||
676 | * |
||
677 | * @param IType &$type Type of the primitive property needing conversion |
||
678 | * @param mixed $primitiveValue Primitive value to convert |
||
679 | * |
||
680 | * @return string |
||
681 | */ |
||
682 | private function primitiveToString(IType &$type, $primitiveValue) |
||
683 | { |
||
684 | // kludge to enable switching on type of $type without getting tripped up by mocks as we would with get_class |
||
685 | // switch (true) means we unconditionally enter, and then lean on case statements to match given block |
||
686 | switch (true) { |
||
687 | case $type instanceof StringType: |
||
688 | $stringValue = utf8_encode($primitiveValue); |
||
689 | break; |
||
690 | case $type instanceof Boolean: |
||
691 | $stringValue = (true === $primitiveValue) ? 'true' : 'false'; |
||
692 | break; |
||
693 | case $type instanceof Binary: |
||
694 | $stringValue = base64_encode($primitiveValue); |
||
695 | break; |
||
696 | case $type instanceof DateTime && $primitiveValue instanceof \DateTime: |
||
697 | $stringValue = $primitiveValue->format(\DateTime::ATOM); |
||
698 | break; |
||
699 | default: |
||
700 | $stringValue = strval($primitiveValue); |
||
701 | } |
||
702 | |||
703 | return $stringValue; |
||
704 | } |
||
705 | |||
706 | /** |
||
707 | * @param $entryObject |
||
708 | * @param $nonRelProp |
||
709 | * @return ODataPropertyContent |
||
710 | * @throws InvalidOperationException |
||
711 | */ |
||
712 | private function writePrimitiveProperties(Model $entryObject, $nonRelProp) |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * @param QueryResult $entryObject |
||
735 | * @param ResourceProperty $prop |
||
736 | * @param $nuLink |
||
737 | * @param $propKind |
||
738 | * @param $propName |
||
739 | * @throws InvalidOperationException |
||
740 | * @throws ODataException |
||
741 | * @throws \ReflectionException |
||
742 | */ |
||
743 | private function expandNavigationProperty( |
||
801 | } |
||
802 | } |
||
803 | |||
804 | /** |
||
805 | * @param ResourceType $resourceType |
||
806 | * @param $result |
||
807 | * @return ODataBagContent|null |
||
808 | * @throws InvalidOperationException |
||
809 | * @throws \ReflectionException |
||
810 | */ |
||
811 | protected function writeBagValue(ResourceType &$resourceType, $result) |
||
812 | { |
||
813 | $isNull = null == $result; |
||
814 | if (!($isNull || is_array($result))) { |
||
815 | throw new InvalidOperationException('Bag parameter must be null or array'); |
||
816 | } |
||
817 | $typeKind = $resourceType->getResourceTypeKind(); |
||
818 | $kVal = $typeKind; |
||
819 | if (!(ResourceTypeKind::PRIMITIVE() == $kVal || ResourceTypeKind::COMPLEX() == $kVal)) { |
||
820 | $msg = '$bagItemResourceTypeKind != ResourceTypeKind::PRIMITIVE' |
||
821 | .' && $bagItemResourceTypeKind != ResourceTypeKind::COMPLEX'; |
||
822 | throw new InvalidOperationException($msg); |
||
823 | } |
||
824 | if ($isNull) { |
||
825 | return null; |
||
826 | } |
||
827 | $bag = new ODataBagContent(); |
||
828 | $result = array_filter($result); |
||
829 | foreach ($result as $value) { |
||
830 | if (ResourceTypeKind::PRIMITIVE() == $kVal) { |
||
831 | $instance = $resourceType->getInstanceType(); |
||
832 | if (!$instance instanceof IType) { |
||
833 | throw new InvalidOperationException(get_class($instance)); |
||
834 | } |
||
835 | $bag->propertyContents[] = $this->primitiveToString($instance, $value); |
||
836 | } elseif (ResourceTypeKind::COMPLEX() == $kVal) { |
||
837 | $bag->propertyContents[] = $this->writeComplexValue($resourceType, $value); |
||
838 | } |
||
839 | } |
||
840 | return $bag; |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * @param ResourceType $resourceType |
||
845 | * @param object $result |
||
846 | * @param string|null $propertyName |
||
847 | * @return ODataPropertyContent |
||
848 | * @throws InvalidOperationException |
||
849 | * @throws \ReflectionException |
||
850 | */ |
||
851 | protected function writeComplexValue(ResourceType &$resourceType, &$result, $propertyName = null) |
||
852 | { |
||
853 | if (!is_object($result)) { |
||
854 | throw new InvalidOperationException('Supplied $customObject must be an object'); |
||
855 | } |
||
856 | |||
857 | $count = count($this->complexTypeInstanceCollection); |
||
858 | for ($i = 0; $i < $count; ++$i) { |
||
859 | if ($this->complexTypeInstanceCollection[$i] === $result) { |
||
860 | throw new InvalidOperationException( |
||
861 | Messages::objectModelSerializerLoopsNotAllowedInComplexTypes($propertyName) |
||
862 | ); |
||
863 | } |
||
864 | } |
||
865 | |||
866 | $this->complexTypeInstanceCollection[$count] = &$result; |
||
867 | |||
868 | $internalContent = new ODataPropertyContent(); |
||
869 | $resourceProperties = $resourceType->getAllProperties(); |
||
870 | // first up, handle primitive properties |
||
871 | foreach ($resourceProperties as $prop) { |
||
872 | $resourceKind = $prop->getKind(); |
||
873 | $propName = $prop->getName(); |
||
874 | $internalProperty = new ODataProperty(); |
||
875 | $internalProperty->name = $propName; |
||
876 | if (static::isMatchPrimitive($resourceKind)) { |
||
877 | $iType = $prop->getInstanceType(); |
||
878 | if (!$iType instanceof IType) { |
||
879 | throw new InvalidOperationException(get_class($iType)); |
||
880 | } |
||
881 | $internalProperty->typeName = $iType->getFullTypeName(); |
||
882 | |||
883 | $rType = $prop->getResourceType()->getInstanceType(); |
||
884 | if (!$rType instanceof IType) { |
||
885 | throw new InvalidOperationException(get_class($rType)); |
||
886 | } |
||
887 | |||
888 | $internalProperty->value = $this->primitiveToString($rType, $result->$propName); |
||
889 | |||
890 | $internalContent->properties[$propName] = $internalProperty; |
||
891 | } elseif (ResourcePropertyKind::COMPLEX_TYPE == $resourceKind) { |
||
892 | $rType = $prop->getResourceType(); |
||
893 | $internalProperty->typeName = $rType->getFullName(); |
||
894 | $internalProperty->value = $this->writeComplexValue($rType, $result->$propName, $propName); |
||
895 | |||
896 | $internalContent->properties[$propName] = $internalProperty; |
||
897 | } |
||
898 | } |
||
899 | |||
900 | unset($this->complexTypeInstanceCollection[$count]); |
||
901 | return $internalContent; |
||
902 | } |
||
903 | |||
904 | public static function isMatchPrimitive($resourceKind) |
||
913 | } |
||
914 | |||
915 | /** |
||
916 | * @param ResourceEntityType $resourceType |
||
917 | * @param $payloadClass |
||
918 | * @return ResourceEntityType|ResourceType |
||
919 | * @throws InvalidOperationException |
||
920 | * @throws \ReflectionException |
||
921 | */ |
||
922 | protected function getConcreteTypeFromAbstractType(ResourceEntityType $resourceType, $payloadClass) |
||
923 | { |
||
924 | if ($resourceType->isAbstract()) { |
||
925 | $derived = $this->getMetadata()->getDerivedTypes($resourceType); |
||
926 | if (0 == count($derived)) { |
||
927 | throw new InvalidOperationException('Supplied abstract type must have at least one derived type'); |
||
928 | } |
||
929 | foreach ($derived as $rawType) { |
||
930 | if (!$rawType->isAbstract()) { |
||
931 | $name = $rawType->getInstanceType()->getName(); |
||
932 | if ($payloadClass == $name) { |
||
933 | $resourceType = $rawType; |
||
934 | break; |
||
935 | } |
||
936 | } |
||
937 | } |
||
938 | } |
||
939 | // despite all set up, checking, etc, if we haven't picked a concrete resource type, |
||
940 | // wheels have fallen off, so blow up |
||
941 | if ($resourceType->isAbstract()) { |
||
942 | throw new InvalidOperationException('Concrete resource type not selected for payload ' . $payloadClass); |
||
943 | } |
||
944 | return $resourceType; |
||
945 | } |
||
946 | |||
947 | /** |
||
948 | * @param QueryResult $entryObject |
||
949 | * @param array $relProp |
||
950 | * @param $relativeUri |
||
951 | * @return array |
||
952 | * @throws InvalidOperationException |
||
953 | * @throws ODataException |
||
954 | * @throws \ReflectionException |
||
955 | */ |
||
956 | protected function buildLinksFromRels(QueryResult $entryObject, array $relProp, $relativeUri) |
||
993 | } |
||
994 | } |
||
995 |
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.