Exemplo n.º 1
0
 /**
  * @param StackFrameReadOnlyInterface|null $previous
  */
 public function __construct(StackFrameReadOnlyInterface $previous = null)
 {
     settype($level, 'int');
     $level = $previous === null ? 1 : $previous->getLevel() + 1;
     // debug check
     $isOk = $level <= 2 || $previous !== null && $previous->getRelationship() !== null;
     $isOk ?: Exceptions::throwLogicException();
     $this->level = $level;
     $this->previous = $previous;
 }
Exemplo n.º 2
0
 /**
  * @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;
         }
     }
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function addToData(ResourceObjectInterface $resource)
 {
     // check if 'not-arrayed' data were added you cannot add to 'non-array' data section anymore
     $this->isDataArrayed === true || $this->isDataArrayed === null ?: Exceptions::throwLogicException();
     $this->isDataArrayed !== null ?: ($this->isDataArrayed = $resource->isInArray());
     // check all resources have the same isInArray flag
     $this->isDataArrayed === $resource->isInArray() ?: Exceptions::throwLogicException();
     $idx = $resource->getId();
     $type = $resource->getType();
     isset($this->bufferForData[$type][$idx]) === false ?: Exceptions::throwLogicException();
     $this->bufferForData[$type][$idx] = $this->presenter->convertDataResourceToArray($resource, true);
     $this->hasBeenMetAlready[$type][$idx] = true;
     // check if resource has already been added to included
     // (for example as related resource of one of the previous main resources)
     if (isset($this->includedResources[$type][$idx]) === true) {
         $includedIndex = $this->includedResources[$type][$idx];
         // remove duplicate from 'included' (leave only in main resources)
         unset($this->included[$includedIndex]);
     }
 }
Exemplo n.º 4
0
 /**
  * @param array|null $data
  *
  * @return ParserReplyInterface
  */
 private function createReplyForEmptyData($data)
 {
     $data === null || is_array($data) === true && empty($data) === true ?: Exceptions::throwLogicException();
     $replyType = $data === null ? ParserReplyInterface::REPLY_TYPE_NULL_RESOURCE_STARTED : ParserReplyInterface::REPLY_TYPE_EMPTY_RESOURCE_STARTED;
     return $this->parserFactory->createEmptyReply($replyType, $this->stack);
 }