/**
  * @param ResourceObjectInterface     $parent
  * @param RelationshipObjectInterface $relation
  * @param ResourceObjectInterface     $resource
  *
  * @return array
  */
 private function getRelationRepresentation(ResourceObjectInterface $parent, RelationshipObjectInterface $relation, ResourceObjectInterface $resource)
 {
     assert('$relation->getName() !== \'' . Document::KEYWORD_SELF . '\'', '"self" is a reserved keyword and cannot be used as a related resource link name');
     $representation = [];
     if ($relation->isShowData() === true) {
         $representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource);
     }
     if (($meta = $relation->getMeta()) !== null) {
         $representation[Document::KEYWORD_META] = $meta;
     }
     $baseUrl = null;
     if (($selfSubLink = $parent->getSelfSubLink()) !== null) {
         $baseUrl = $selfSubLink->isTreatAsHref() === true ? $selfSubLink->getSubHref() . '/' : $this->document->getUrlPrefix() . $selfSubLink->getSubHref() . '/';
     }
     foreach ($relation->getLinks() as $name => $link) {
         $representation[Document::KEYWORD_LINKS][$name] = $this->getLinkRepresentation($baseUrl, $link);
     }
     return $representation;
 }
 /**
  * @param array                       $target
  * @param ResourceObjectInterface     $parent
  * @param RelationshipObjectInterface $relation
  * @param ResourceObjectInterface     $resource
  *
  * @return void
  */
 public function addRelationshipTo(array &$target, ResourceObjectInterface $parent, RelationshipObjectInterface $relation, ResourceObjectInterface $resource)
 {
     $parentId = $parent->getId();
     $parentType = $parent->getType();
     $parentExists = isset($target[$parentType][$parentId]);
     // parent might be already added to included to it won't be in 'target' buffer
     if ($parentExists === true) {
         $parentAlias =& $target[$parentType][$parentId];
         $name = $relation->getName();
         $alreadyGotRelation = isset($parentAlias[Document::KEYWORD_RELATIONSHIPS][$name]);
         $linkage = null;
         if ($relation->isShowData() === true) {
             $linkage = $this->getLinkageRepresentation($resource);
         }
         if ($alreadyGotRelation === false) {
             // ... add the first linkage
             $representation = [];
             if ($linkage !== null) {
                 if ($resource->isInArray() === true) {
                     // original data in array
                     $representation[Document::KEYWORD_LINKAGE_DATA][] = $linkage;
                 } else {
                     // original data not in array (just object)
                     $representation[Document::KEYWORD_LINKAGE_DATA] = $linkage;
                 }
             }
             $representation += $this->getRelationRepresentation($parent, $relation);
             $parentAlias[Document::KEYWORD_RELATIONSHIPS][$name] = $representation;
         } elseif ($alreadyGotRelation === true && $linkage !== null) {
             // Check data in '$name' relationship are marked as not arrayed otherwise
             // it's fail to add multiple data instances
             $resource->isInArray() === true ?: Exceptions::throwLogicException();
             // ... or add another linkage
             $parentAlias[Document::KEYWORD_RELATIONSHIPS][$name][Document::KEYWORD_LINKAGE_DATA][] = $linkage;
         }
     }
 }