Neomerx\JsonApi\Document\Presenters\ElementPresenter::setRelationshipTo PHP Method

setRelationshipTo() public method

public setRelationshipTo ( array &$target, Neomerx\JsonApi\Contracts\Schema\ResourceObjectInterface $parent, Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface $relation, mixed $value ) : void
$target array
$parent Neomerx\JsonApi\Contracts\Schema\ResourceObjectInterface
$relation Neomerx\JsonApi\Contracts\Schema\RelationshipObjectInterface
$value mixed
return void
    public function setRelationshipTo(array &$target, ResourceObjectInterface $parent, RelationshipObjectInterface $relation, $value)
    {
        $parentId = $parent->getId();
        $parentType = $parent->getType();
        $name = $relation->getName();
        $parentExists = isset($target[$parentType][$parentId]);
        // parent object might be already fully parsed (with children) so
        // - it won't exist in $target
        // - it won't make any sense to parse it again (we'll got exactly the same result and it will be thrown away
        //   as duplicate relations/included resources are not allowed)
        if ($parentExists === true) {
            $isOk = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]) === false;
            $isOk ?: Exceptions::throwLogicException();
            $representation = [];
            if ($relation->isShowData() === true) {
                $representation[Document::KEYWORD_LINKAGE_DATA] = $value;
            }
            $representation += $this->getRelationRepresentation($parent, $relation);
            $target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation;
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @inheritdoc
  */
 public function addNullRelationshipToIncluded(ResourceObjectInterface $parent, RelationshipObjectInterface $relationship)
 {
     $this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $relationship, null);
 }