/** * Constructor. * * @param ErrorInterface|ErrorInterface[]|ErrorCollection $errors * @param int $httpCode * @param Exception|null $previous */ public function __construct($errors, $httpCode = self::DEFAULT_HTTP_CODE, Exception $previous = null) { parent::__construct(T::t('JSON API error'), 0, $previous); $this->errors = new ErrorCollection(); if ($errors instanceof ErrorCollection) { $this->addErrors($errors); } elseif (is_array($errors) === true) { $this->addErrorsFromArray($errors); } else { // should be ErrorInterface $this->addError($errors); } $this->httpCode = $httpCode; }
/** * @param SchemaFactoryInterface $factory */ public function __construct(SchemaFactoryInterface $factory) { $isOk = is_string($this->getResourceType()) === true && empty($this->getResourceType()) === false; if ($isOk === false) { throw new InvalidArgumentException(T::t('Resource type is not set for Schema \'%s\'.', [static::class])); } if ($this->selfSubUrl === null) { $this->selfSubUrl = '/' . $this->getResourceType(); } else { $isOk = is_string($this->selfSubUrl) === true && empty($this->selfSubUrl) === false && $this->selfSubUrl[0] === '/' && $this->selfSubUrl[strlen($this->selfSubUrl) - 1] != '/'; if ($isOk === false) { $message = T::t('\'Self\' sub-url set incorrectly for Schema \'%s\'.', [static::class]); throw new InvalidArgumentException($message); } } $this->factory = $factory; }
/** * @param ErrorCollection $errors * @param EncodingParametersInterface $parameters */ protected function checkUnrecognized(ErrorCollection $errors, EncodingParametersInterface $parameters) { if ($this->allowUnrecognized === false && empty($parameters->getUnrecognizedParameters()) === false) { foreach ($parameters->getUnrecognizedParameters() as $name => $value) { $errors->addQueryParameterError($name, T::t('Parameter is not allowed.')); } } }
/** * 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; if ($isOk === false) { throw new InvalidArgumentException(T::t('\'%s\' is a reserved keyword and cannot be used as attribute name in type \'%s\'', [Document::KEYWORD_TYPE, $resource->getType()])); } $isOk = isset($attributes[Document::KEYWORD_ID]) === false; if ($isOk === false) { throw new InvalidArgumentException(T::t('\'%s\' is a reserved keyword and cannot be used as attribute name in type \'%s\'', [Document::KEYWORD_ID, $resource->getType()])); } 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; }
/** * @param mixed $resource * @param StackFrameReadOnlyInterface $frame * * @return SchemaProviderInterface */ private function getSchema($resource, StackFrameReadOnlyInterface $frame) { try { $schema = $this->container->getSchema($resource); } catch (InvalidArgumentException $exception) { $message = T::t('Schema is not registered for a resource at path \'%s\'.', [$frame->getPath()]); throw new InvalidArgumentException($message, 0, $exception); } return $schema; }
/** * @inheritdoc */ public function getSchemaByResourceType($resourceType) { // Schema is not found among instantiated schemas for resource type $resourceType $isOk = is_string($resourceType) === true && isset($this->resourceType2Type[$resourceType]) === true; // Schema might not be found if it hasn't been searched by type (not resource type) before. // We instantiate all schemas and then find one. if ($isOk === false) { foreach ($this->providerMapping as $type => $schema) { if (isset($this->createdProviders[$type]) === false) { // it will instantiate the schema $this->getSchemaByType($type); } } } // search one more time $isOk = is_string($resourceType) === true && isset($this->resourceType2Type[$resourceType]) === true; if ($isOk === false) { throw new InvalidArgumentException(T::t('Schema is not registered for resource type \'%s\'.', [$resourceType])); } return $this->getSchemaByType($this->resourceType2Type[$resourceType]); }