Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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] : [];
     }
 }
Exemplo n.º 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;
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
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;
 }
Exemplo n.º 7
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;
 }
Exemplo n.º 8
0
 /**
  * @inheritdoc
  */
 public function setExtensions($extensions)
 {
     is_string($extensions) === true ?: Exceptions::throwInvalidArgument('extensions', $extensions);
     $this->extensions = $extensions;
 }
Exemplo n.º 9
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);
 }
Exemplo n.º 10
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);
 }
Exemplo n.º 11
0
 /**
  * @inheritdoc
  */
 public function setMetaToDocument($meta)
 {
     is_object($meta) === true || is_array($meta) === true ?: Exceptions::throwInvalidArgument('meta', $meta);
     $this->meta = $meta;
 }
Exemplo n.º 12
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;
 }
Exemplo n.º 13
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;
 }
Exemplo n.º 14
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]);
 }
Exemplo n.º 15
0
 /**
  * @param array|null|object $data
  *
  * @return array
  */
 protected function analyzeData($data)
 {
     $isCollection = true;
     $isEmpty = true;
     $traversableData = null;
     $isOk = is_array($data) === true || is_object($data) === true || $data === null;
     $isOk ?: Exceptions::throwInvalidArgument('data', $data);
     if (is_array($data) === true) {
         /** @var array $data */
         $isEmpty = empty($data);
         $traversableData = $data;
     } elseif ($data instanceof Iterator && ($iterator = $data) !== null || $data instanceof IteratorAggregate && ($iterator = $data->getIterator()) !== null) {
         /** @var Iterator $iterator */
         $iterator->rewind();
         $isEmpty = $iterator->valid() === false;
         if ($isEmpty === false) {
             $traversableData = $data;
         } else {
             $traversableData = [];
         }
     } elseif (is_object($data) === true) {
         /** @var object $data */
         $isEmpty = $data === null;
         $isCollection = false;
         $traversableData = [$data];
     } elseif ($data === null) {
         $isCollection = false;
         $isEmpty = true;
     }
     return [$isEmpty, $isCollection, $traversableData];
 }
Exemplo n.º 16
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());
 }
Exemplo n.º 17
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;
 }
Exemplo n.º 18
0
 /**
  * @return array
  */
 protected function analyzeCurrentData()
 {
     $relationship = $this->stack->end()->getRelationship();
     $data = $relationship->isShowData() === true ? $relationship->getData() : null;
     $isCollection = true;
     $isEmpty = true;
     $traversableData = null;
     $isOk = is_array($data) === true || is_object($data) === true || $data === null || $data instanceof Iterator;
     $isOk ?: Exceptions::throwInvalidArgument('data', $data);
     if (is_array($data) === true) {
         /** @var array $data */
         $isEmpty = empty($data);
         $traversableData = $data;
     } elseif ($data instanceof Iterator) {
         /** @var Iterator $data */
         $data->rewind();
         $isEmpty = $data->valid() === false;
         if ($isEmpty === false) {
             $traversableData = $data;
         } else {
             $traversableData = [];
         }
     } elseif (is_object($data) === true) {
         /** @var object $data */
         $isEmpty = $data === null;
         $isCollection = false;
         $traversableData = [$data];
     } elseif ($data === null) {
         $isCollection = false;
         $isEmpty = true;
     }
     return [$isEmpty, $isCollection, $traversableData];
 }
Exemplo n.º 19
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);
 }
Exemplo n.º 20
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;
 }