Пример #1
0
 /**
  * @param string $sortField
  * @param bool   $isAscending
  */
 public function __construct($sortField, $isAscending)
 {
     is_string($sortField) === true ?: Exceptions::throwInvalidArgument('sortField', $sortField);
     is_bool($isAscending) === true ?: Exceptions::throwInvalidArgument('isAscending', $isAscending);
     $this->sortField = $sortField;
     $this->isAscending = $isAscending;
 }
Пример #2
0
 /**
  * @param string $subHref
  * @param mixed  $meta
  * @param bool   $treatAsHref If $subHref is a full URL and must not be concatenated with other URLs.
  */
 public function __construct($subHref, $meta = null, $treatAsHref = false)
 {
     is_string($subHref) === true ?: Exceptions::throwInvalidArgument('subHref', $subHref);
     is_bool($treatAsHref) === true ?: Exceptions::throwInvalidArgument('treatAsHref', $treatAsHref);
     $this->subHref = $subHref;
     $this->meta = $meta;
     $this->treatAsHref = $treatAsHref;
 }
Пример #3
0
 /**
  * @inheritdoc
  */
 public function getFieldSet($type)
 {
     is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type);
     if ($this->fieldSets === null) {
         return null;
     } else {
         return isset($this->fieldSets[$type]) === true ? $this->fieldSets[$type] : [];
     }
 }
Пример #4
0
 /**
  * @param int         $options
  * @param string|null $urlPrefix
  * @param int         $depth
  */
 public function __construct($options = 0, $urlPrefix = null, $depth = 512)
 {
     is_int($depth) === true ?: Exceptions::throwInvalidArgument('depth', $depth);
     is_int($options) === true ?: Exceptions::throwInvalidArgument('options', $options);
     $isOk = $urlPrefix === null || is_string($urlPrefix) === true;
     $isOk ?: Exceptions::throwInvalidArgument('urlPrefix', $urlPrefix);
     $this->options = $options;
     $this->depth = $depth;
     $this->urlPrefix = $urlPrefix;
 }
Пример #5
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;
 }
Пример #6
0
 /**
  * @param SchemaFactoryInterface $factory
  * @param ContainerInterface     $container
  */
 public function __construct(SchemaFactoryInterface $factory, ContainerInterface $container)
 {
     // Check resource type is set for the Schema
     $isOk = is_string($this->resourceType) === true && empty($this->resourceType) === false;
     $isOk ?: Exceptions::throwInvalidArgument('resourceType', $this->resourceType);
     // Check 'self' sub-URL is set for the Schema
     $isOk = is_string($this->selfSubUrl) === true && empty($this->selfSubUrl) === false;
     $isOk ?: Exceptions::throwInvalidArgument('selfSubUrl', $this->selfSubUrl);
     $this->factory = $factory;
     $this->container = $container;
 }
Пример #7
0
 /**
  * @param string                                                        $name
  * @param object|array|null|Closure                                     $data
  * @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface> $links
  * @param object|array|null|Closure                                     $meta
  * @param bool                                                          $isShowData
  * @param bool                                                          $isRoot
  */
 public function __construct($name, $data, array $links, $meta, $isShowData, $isRoot)
 {
     is_bool($isRoot) === true ?: Exceptions::throwInvalidArgument('isRoot', $isRoot);
     is_bool($isShowData) === true ?: Exceptions::throwInvalidArgument('isShowData', $isShowData);
     $isOk = $isRoot === false && is_string($name) === true || $isRoot === true && $name === null;
     $isOk ?: Exceptions::throwInvalidArgument('name', $name);
     $this->name = $name;
     $this->data = $data;
     $this->links = $links;
     $this->meta = $meta;
     $this->isShowData = $isShowData;
     $this->isRoot = $isRoot;
 }
Пример #8
0
 /**
  * Convert resource object to array.
  *
  * @param ResourceObjectInterface $resource
  * @param array                   $resourceLinks
  * @param mixed                   $meta
  * @param bool                    $isShowAttributes
  *
  * @return array
  */
 private function convertResourceToArray(ResourceObjectInterface $resource, $resourceLinks, $meta, $isShowAttributes)
 {
     $representation = [Document::KEYWORD_TYPE => $resource->getType(), Document::KEYWORD_ID => $resource->getId()];
     $attributes = $resource->getAttributes();
     // "type" and "id" are reserved keywords and cannot be used as resource object attributes
     $isOk = isset($attributes[Document::KEYWORD_TYPE]) === false;
     $isOk ?: Exceptions::throwInvalidArgument('attributes', Document::KEYWORD_TYPE);
     $isOk = isset($attributes[Document::KEYWORD_ID]) === false;
     $isOk ?: Exceptions::throwInvalidArgument('attributes', Document::KEYWORD_ID);
     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 (empty($resourceLinks) === false) {
         foreach ($resourceLinks as $linkName => $link) {
             /** @var LinkInterface $link */
             $representation[Document::KEYWORD_LINKS][$linkName] = $this->getLinkRepresentation($this->document->getUrlPrefix(), $link);
         }
     }
     if ($meta !== null) {
         $representation[Document::KEYWORD_META] = $meta;
     }
     return $representation;
 }
Пример #9
0
 /**
  * @inheritdoc
  */
 public function setExtensions($extensions)
 {
     is_string($extensions) === true ?: Exceptions::throwInvalidArgument('extensions', $extensions);
     $this->extensions = $extensions;
 }
Пример #10
0
 /**
  * @param object $resource
  * @param bool   $isInArray
  */
 private function checkInput($resource, $isInArray)
 {
     is_bool($isInArray) === true ?: Exceptions::throwInvalidArgument('isInArray', $isInArray);
     is_object($resource) === true ?: Exceptions::throwInvalidArgument('resource', $resource);
 }
Пример #11
0
 /**
  * @param int                    $replyType
  * @param StackReadOnlyInterface $stack
  */
 public function __construct($replyType, StackReadOnlyInterface $stack)
 {
     $isOk = $replyType === self::REPLY_TYPE_RESOURCE_STARTED || $replyType === self::REPLY_TYPE_RESOURCE_COMPLETED;
     $isOk ?: Exceptions::throwInvalidArgument('replyType', $replyType);
     parent::__construct($replyType, $stack);
 }
Пример #12
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]);
     }
 }
Пример #13
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;
         }
     }
 }
Пример #14
0
 /**
  * @param string $location
  * @param array  $headers
  *
  * @return array
  */
 private function setLocationHeader($location, array $headers)
 {
     is_string($location) === true ?: Exceptions::throwInvalidArgument('location', $location);
     $headers[self::HEADER_LOCATION] = $location;
     return $headers;
 }
Пример #15
0
 /**
  * @inheritdoc
  */
 public function getSchemaByType($type)
 {
     is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type);
     if (isset($this->createdProviders[$type])) {
         return $this->createdProviders[$type];
     }
     if (isset($this->providerMapping[$type]) === false) {
         // todo make this better and less yolo
         // inject standard schema
         $this->providerMapping[$type] = function (SchemaFactoryInterface $factory, ContainerInterface $container) use($type) {
             return new ResourceSchema($factory, $container, $type);
         };
         //throw new InvalidArgumentException("Schema is not registered for type '{$type}'.");
     }
     $classNameOrClosure = $this->providerMapping[$type];
     if ($classNameOrClosure instanceof Closure) {
         $this->createdProviders[$type] = $schema = $classNameOrClosure($this->factory, $this);
     } else {
         $this->createdProviders[$type] = $schema = new $classNameOrClosure($this->factory, $this);
     }
     /** @var SchemaProviderInterface $schema */
     $this->resourceType2Type[$schema->getResourceType()] = $type;
     return $schema;
 }
Пример #16
0
 /**
  * @inheritdoc
  */
 public function getSchemaByResourceType($resourceType)
 {
     // Schema is not registered for resource type $resourceType
     $isOk = is_string($resourceType) === true && isset($this->resourceType2Type[$resourceType]) === true;
     $isOk ?: Exceptions::throwInvalidArgument('resourceType', $resourceType);
     return $this->getSchemaByType($this->resourceType2Type[$resourceType]);
 }
Пример #17
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);
 }
Пример #18
0
 /**
  * @inheritdoc
  */
 public function render(Exception $exception)
 {
     $mediaType = $this->getMediaType();
     // Media type should be specified for exception renderers
     $mediaType !== null ?: Exceptions::throwInvalidArgument('mediaType', $mediaType);
     return $this->responses->getResponse($this->getStatusCode(), $mediaType, $this->getContent($exception), $this->getSupportedExtensions(), $this->getHeaders());
 }
Пример #19
0
 /**
  * @inheritdoc
  */
 public function getSchemaByType($type)
 {
     is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type);
     if (isset($this->createdProviders[$type])) {
         return $this->createdProviders[$type];
     }
     if (isset($this->providerMapping[$type]) === false) {
         throw new InvalidArgumentException(T::t('Schema is not registered for type \'%s\'.', [$type]));
     }
     $classNameOrClosure = $this->providerMapping[$type];
     if ($classNameOrClosure instanceof Closure) {
         $this->createdProviders[$type] = $schema = $classNameOrClosure($this->factory, $this);
     } else {
         $this->createdProviders[$type] = $schema = new $classNameOrClosure($this->factory, $this);
     }
     /** @var SchemaProviderInterface $schema */
     $this->resourceType2Type[$schema->getResourceType()] = $type;
     return $schema;
 }
Пример #20
0
 /**
  * @param int|string|null $code
  */
 private function checkCode($code)
 {
     $isOk = $code === null || is_int($code) === true || is_string($code) === true;
     $isOk ?: Exceptions::throwInvalidArgument('code', $code);
 }
Пример #21
0
 /**
  * @inheritdoc
  */
 public function getSchemaByType($type)
 {
     is_string($type) === true ?: Exceptions::throwInvalidArgument('type', $type);
     if ($this->hasCreatedProvider($type) === true) {
         return $this->getCreatedProvider($type);
     }
     if ($this->hasProviderMapping($type) === false) {
         throw new InvalidArgumentException(T::t('Schema is not registered for type \'%s\'.', [$type]));
     }
     $classNameOrClosure = $this->getProviderMapping($type);
     if ($classNameOrClosure instanceof Closure) {
         $schema = $this->createSchemaFromClosure($classNameOrClosure);
     } else {
         $schema = $this->createSchemaFromClassName($classNameOrClosure);
     }
     $this->setCreatedProvider($type, $schema);
     /** @var SchemaProviderInterface $schema */
     $this->setResourceToJsonTypeMapping($schema->getResourceType(), $type);
     return $schema;
 }