Neomerx\JsonApi\Document\Document::setResourceCompleted PHP Method

setResourceCompleted() public method

public setResourceCompleted ( Neomerx\JsonApi\Contracts\Schema\ResourceObjectInterface $resource )
$resource Neomerx\JsonApi\Contracts\Schema\ResourceObjectInterface
    public function setResourceCompleted(ResourceObjectInterface $resource)
    {
        $idx = $resource->getId();
        $type = $resource->getType();
        $foundInData = isset($this->bufferForData[$type][$idx]);
        $foundInIncluded = isset($this->bufferForIncluded[$type][$idx]);
        $addMeta = function (array $representation, Closure $getMetaClosure) {
            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 = $getMetaClosure();
                if (empty($relShipsMeta) === false) {
                    $representation[self::KEYWORD_RELATIONSHIPS][self::KEYWORD_META] = $relShipsMeta;
                }
            }
            return $representation;
        };
        if ($foundInData === true) {
            $representation = $this->bufferForData[$type][$idx];
            unset($this->bufferForData[$type][$idx]);
            $this->data[] = $addMeta($representation, function () use($resource) {
                return $resource->getRelationshipsPrimaryMeta();
            });
        }
        if ($foundInIncluded === true) {
            $representation = $this->bufferForIncluded[$type][$idx];
            unset($this->bufferForIncluded[$type][$idx]);
            $this->included[] = $addMeta($representation, function () use($resource) {
                return $resource->getRelationshipsInclusionMeta();
            });
            // remember we added (type, id) at index
            $this->includedResources[$type][$idx] = $this->includedIndex;
            $this->includedIndex++;
        }
    }