Get resource 'self' endpoint URL.
public getSelfSubLink ( ) : Neomerx\JsonApi\Contracts\Document\LinkInterface | ||
리턴 | Neomerx\JsonApi\Contracts\Document\LinkInterface |
/** * @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; }
/** * 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; }
/** * @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 = 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; }