/** * @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) { assert('$resource->isInArray() === true', "Data in '{$name}' relationship are marked as not arrayed however " . 'you try to add multiple data instances'); // ... or add another linkage $parentAlias[Document::KEYWORD_RELATIONSHIPS][$name][Document::KEYWORD_LINKAGE_DATA][] = $linkage; } } }
/** * Set path to current frame. */ private function setCurrentPath() { if ($this->previous === null || $this->previous->getPath() === null) { $this->path = $this->relationship->getName(); } else { $this->path = $this->previous->getPath() . '.' . $this->relationship->getName(); } }
/** * Set path to current frame. */ private function setCurrentPath() { if ($this->previous === null || $this->previous->getPath() === null) { $this->path = $this->relationship->getName(); } else { $this->path = $this->previous->getPath() . DocumentInterface::PATH_SEPARATOR . $this->relationship->getName(); } }
/** * @param array $target * @param ResourceObjectInterface $parent * @param RelationshipObjectInterface $relationship * @param ResourceObjectInterface $resource * * @return void */ public function addRelationshipTo(array &$target, ResourceObjectInterface $parent, RelationshipObjectInterface $relationship, 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) { $name = $relationship->getName(); $alreadyGotData = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]); if ($alreadyGotData === false) { // ... add the first one $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $this->getRelationRepresentation($parent, $relationship, $resource); } else { // ... or add another relation $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name][Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource); } } }
/** * @param ResourceObjectInterface $parent * @param RelationshipObjectInterface $relation * * @return array */ private function getRelationRepresentation(ResourceObjectInterface $parent, RelationshipObjectInterface $relation) { // "self" is a reserved keyword and cannot be used as a related resource link name $isOk = $relation->getName() !== Document::KEYWORD_SELF; $isOk ?: Exceptions::throwInvalidArgument('relation.name', $relation->getName()); $representation = []; 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 ResourceObjectInterface $parent * @param RelationshipObjectInterface $relation * * @return array */ private function getRelationRepresentation(ResourceObjectInterface $parent, RelationshipObjectInterface $relation) { $isOk = $relation->getName() !== Document::KEYWORD_SELF; if ($isOk === false) { throw new InvalidArgumentException(T::t('\'%s\' is a reserved keyword and cannot be used as a relationship name in type \'%s\'', [Document::KEYWORD_SELF, $parent->getType()])); } $representation = []; if (($meta = $relation->getMeta()) !== null) { $representation[Document::KEYWORD_META] = $meta; } $baseUrl = $this->document->getUrlPrefix(); foreach ($relation->getLinks() as $name => $link) { $representation[Document::KEYWORD_LINKS][$name] = $this->getLinkRepresentation($baseUrl, $link); } return $representation; }
/** * @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); }