/**
  * Convert resource object to array.
  *
  * @param ResourceObjectInterface $resource
  * @param array                   $resourceLinks
  * @param mixed                   $meta
  * @param bool                    $isShowAttributes
  *
  * @return array
  */
 private function convertResourceToArray(ResourceObjectInterface $resource, $resourceLinks, $meta, $isShowAttributes)
 {
     $representation = [Document::KEYWORD_TYPE => $resource->getType(), Document::KEYWORD_ID => $resource->getId()];
     $attributes = $resource->getAttributes();
     // "type" and "id" are reserved keywords and cannot be used as resource object attributes
     $isOk = isset($attributes[Document::KEYWORD_TYPE]) === false;
     $isOk ?: Exceptions::throwInvalidArgument('attributes', Document::KEYWORD_TYPE);
     $isOk = isset($attributes[Document::KEYWORD_ID]) === false;
     $isOk ?: Exceptions::throwInvalidArgument('attributes', Document::KEYWORD_ID);
     if ($isShowAttributes === true && empty($attributes) === false) {
         $representation[Document::KEYWORD_ATTRIBUTES] = $attributes;
     }
     // reserve placeholder for relationships, otherwise it would be added after
     // links and meta which is not visually beautiful
     $representation[Document::KEYWORD_RELATIONSHIPS] = null;
     if (empty($resourceLinks) === false) {
         foreach ($resourceLinks as $linkName => $link) {
             /** @var LinkInterface $link */
             $representation[Document::KEYWORD_LINKS][$linkName] = $this->getLinkRepresentation($this->document->getUrlPrefix(), $link);
         }
     }
     if ($meta !== null) {
         $representation[Document::KEYWORD_META] = $meta;
     }
     return $representation;
 }
 /**
  * Convert resource object to array.
  *
  * @param ResourceObjectInterface $resource
  * @param bool                    $isShowSelf
  * @param mixed                   $meta
  * @param bool                    $isShowAttributes
  *
  * @return array
  */
 private function convertResourceToArray(ResourceObjectInterface $resource, $isShowSelf, $meta, $isShowAttributes)
 {
     $representation = [Document::KEYWORD_TYPE => $resource->getType(), Document::KEYWORD_ID => $resource->getId()];
     $attributes = $resource->getAttributes();
     assert('isset($attributes[\'' . Document::KEYWORD_TYPE . '\']) === false && ' . 'isset($attributes[\'' . Document::KEYWORD_ID . '\']) === false', '"type" and "id" are reserved keywords and cannot be used as resource object attributes');
     if ($isShowAttributes === true && empty($attributes) === false) {
         $representation[Document::KEYWORD_ATTRIBUTES] = $attributes;
     }
     // reserve placeholder for relationships, otherwise it would be added after
     // links and meta which is not visually beautiful
     $representation[Document::KEYWORD_RELATIONSHIPS] = null;
     if ($isShowSelf === true) {
         $representation[Document::KEYWORD_LINKS][Document::KEYWORD_SELF] = $this->getLinkRepresentation($this->document->getUrlPrefix(), $resource->getSelfSubLink());
     }
     if ($meta !== null) {
         $representation[Document::KEYWORD_META] = $meta;
     }
     return $representation;
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function setResourceCompleted(ResourceObjectInterface $resource)
 {
     $idx = $resource->getId();
     $type = $resource->getType();
     $foundInData = isset($this->bufferForData[$type][$idx]);
     $foundInIncluded = isset($this->bufferForIncluded[$type][$idx]);
     if ($foundInData === true) {
         $representation = $this->bufferForData[$type][$idx];
         unset($this->bufferForData[$type][$idx]);
         if (empty($representation[self::KEYWORD_RELATIONSHIPS]) === true) {
             // if no relationships have been added remove empty placeholder
             unset($representation[self::KEYWORD_RELATIONSHIPS]);
         } else {
             // relationship might have meta
             $relShipsMeta = $resource->getRelationshipsPrimaryMeta();
             if (empty($relShipsMeta) === false) {
                 $representation[self::KEYWORD_RELATIONSHIPS][self::KEYWORD_META] = $relShipsMeta;
             }
         }
         $this->data[] = $representation;
     }
     if ($foundInIncluded === true) {
         $representation = $this->bufferForIncluded[$type][$idx];
         unset($this->bufferForIncluded[$type][$idx]);
         if (empty($representation[self::KEYWORD_RELATIONSHIPS]) === true) {
             // if no relationships have been added remove empty placeholder
             unset($representation[self::KEYWORD_RELATIONSHIPS]);
         } else {
             // relationship might have meta
             $relShipsMeta = $resource->getRelationshipsInclusionMeta();
             if (empty($relShipsMeta) === false) {
                 $representation[self::KEYWORD_RELATIONSHIPS][self::KEYWORD_META] = $relShipsMeta;
             }
         }
         $this->included[] = $representation;
         // remember we added (type, id) at index
         $this->includedResources[$type][$idx] = $this->includedIndex;
         $this->includedIndex++;
     }
 }
Example #4
0
 /**
  * @param ResourceObjectInterface $resourceObject
  *
  * @return bool
  */
 private function checkCircular(ResourceObjectInterface $resourceObject)
 {
     foreach ($this->stack as $frame) {
         /** @var StackFrameReadOnlyInterface $frame */
         if (($stackResource = $frame->getResource()) !== null && $stackResource->getId() === $resourceObject->getId() && $stackResource->getType() === $resourceObject->getType()) {
             return true;
         }
     }
     return false;
 }
Example #5
0
 /**
  * @inheritdoc
  */
 public function isShouldParseRelationships(ResourceObjectInterface $resource, $isCircular, StackReadOnlyInterface $stack)
 {
     list($onTheWay, $parentIsTarget) = $this->foundInPaths($stack);
     $shouldContinue = $onTheWay || $parentIsTarget && $resource->isShowRelationshipsInIncluded();
     return $shouldContinue;
 }
Example #6
0
 /**
  * @inheritdoc
  */
 public function isShouldRelationshipBeInOutput(ResourceObjectInterface $resource, RelationshipObjectInterface $relationship)
 {
     $resourceType = $resource->getType();
     $resourceFieldSet = $this->getFieldSet($resourceType);
     return $resourceFieldSet === null ? true : array_key_exists($relationship->getName(), $resourceFieldSet);
 }
Example #7
0
 /**
  * @inheritdoc
  */
 public function setResourceCompleted(ResourceObjectInterface $resource)
 {
     $idx = $resource->getId();
     $type = $resource->getType();
     $foundInData = isset($this->bufferForData[$type][$idx]);
     $foundInIncluded = isset($this->bufferForIncluded[$type][$idx]);
     if ($foundInData === true) {
         $representation = $this->presenter->correctRelationships($this->bufferForData[$type][$idx]);
         $relShipsMeta = $resource->getRelationshipsPrimaryMeta();
         if (empty($relShipsMeta) === false && isset($representation[self::KEYWORD_RELATIONSHIPS]) === true) {
             $representation[self::KEYWORD_RELATIONSHIPS][self::KEYWORD_META] = $relShipsMeta;
         }
         $this->data[] = $representation;
         unset($this->bufferForData[$type][$idx]);
     }
     if ($foundInIncluded === true) {
         $representation = $this->presenter->correctRelationships($this->bufferForIncluded[$type][$idx]);
         $relShipsMeta = $resource->getRelationshipsInclusionMeta();
         if (empty($relShipsMeta) === false && isset($representation[self::KEYWORD_RELATIONSHIPS]) === true) {
             $representation[self::KEYWORD_RELATIONSHIPS][self::KEYWORD_META] = $relShipsMeta;
         }
         $this->included[] = $representation;
         unset($this->bufferForIncluded[$type][$idx]);
     }
 }