/** * @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; }
/** * @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; }
/** * @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] : []; } }
/** * @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; }
/** * @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; }
/** * @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; }
/** * 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; }
/** * @inheritdoc */ public function setExtensions($extensions) { is_string($extensions) === true ?: Exceptions::throwInvalidArgument('extensions', $extensions); $this->extensions = $extensions; }
/** * @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); }
/** * @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); }
/** * @inheritdoc */ public function setMetaToDocument($meta) { is_object($meta) === true || is_array($meta) === true ?: Exceptions::throwInvalidArgument('meta', $meta); $this->meta = $meta; }
/** * @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; }
/** * @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; }
/** * @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]); }
/** * @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]; }
/** * @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()); }
/** * @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; }
/** * @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]; }
/** * @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); }
/** * @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; }