Example #1
0
 /**
  * Check accept header
  *
  * @param AcceptHeaderInterface $header
  * @return Error
  */
 private function checkAcceptHeader(AcceptHeaderInterface $header)
 {
     $this->matcher->matchEncoder($header);
     if (null === $this->matcher->getEncoderHeaderMatchedType()) {
         throw new JsonApiException($this->createApiError(JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE, self::UNSUPPORTED_ACCEPT_ERROR, 'Unsupported media type'), JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE);
     }
 }
 /**
  * @inheritdoc
  */
 public function getContent(Exception $exception)
 {
     $converter = $this->converter;
     $errors = $converter($exception);
     $encoder = $this->codecMatcher->getEncoder();
     $content = is_array($errors) === true ? $encoder->encodeErrors($errors) : $encoder->encodeError($errors);
     return $content;
 }
 /**
  * @param HeaderParametersInterface $parameters
  *
  * @return void
  */
 protected function checkContentTypeHeader(HeaderParametersInterface $parameters)
 {
     // Do not allow specify more than 1 media type for input data. Otherwise which one is correct?
     if (count($parameters->getContentTypeHeader()->getMediaTypes()) > 1) {
         throw new E([], E::HTTP_CODE_BAD_REQUEST);
     }
     $this->codecMatcher->matchDecoder($parameters->getContentTypeHeader());
     // From spec: Servers MUST respond with a 415 Unsupported Media Type status code
     // if a request specifies the header Content-Type: application/vnd.api+json with
     // any media type parameters.
     // We return 415 if no match found for decoder (media type with or wo parameters)
     // If no decoders were configured for media types with parameters we return 415 anyway
     if ($this->codecMatcher->getDecoderHeaderMatchedType() === null) {
         throw new E([], E::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE);
     }
 }
 /**
  * @param ParametersInterface $parameters
  *
  * @return void
  */
 protected function checkContentTypeHeader(ParametersInterface $parameters)
 {
     // Do not allow specify more than 1 media type for input data. Otherwise which one is correct?
     if (count($parameters->getContentTypeHeader()->getMediaTypes()) > 1) {
         $this->exceptionThrower->throwBadRequest();
     }
     $this->codecMatcher->findDecoder($parameters->getContentTypeHeader());
     // From spec: Servers MUST respond with a 415 Unsupported Media Type status code
     // if a request specifies the header Content-Type: application/vnd.api+json with
     // any media type parameters.
     // We return 415 if no match found for decoder (media type with or wo parameters)
     // If no decoders were configured for media types with parameters we return 415 anyway
     if ($this->codecMatcher->getDecoderHeaderMatchedType() === null) {
         $this->exceptionThrower->throwUnsupportedMediaType();
     }
 }
 /**
  * @param string $mediaType
  * @return $this
  */
 private function withMediaType($mediaType = MediaTypeInterface::JSON_API_MEDIA_TYPE)
 {
     $mediaType = MediaType::parse(0, $mediaType);
     $this->codecMatcher->registerEncoder($mediaType, function () {
         return Encoder::instance();
     });
     $this->codecMatcher->registerDecoder($mediaType, function () {
         return new DocumentDecoder();
     });
     return $this;
 }
Example #6
0
 /**
  * Register specified encoders
  *
  * @param array $encoders
  * @param CodecMatcherInterface $matcher
  */
 private function registerEncoders(array $encoders, CodecMatcherInterface $matcher)
 {
     foreach ($encoders as $mediaType => $encoderType) {
         $matcher->registerEncoder($this->parseMediaTypeString($mediaType), $this->registry->getEncoder($encoderType));
     }
 }
Example #7
0
 /**
  * @param object                                                             $resource
  * @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface>|null $links
  * @param mixed                                                              $meta
  *
  * @return Response
  */
 protected function getCreatedResponse($resource, $links = null, $meta = null)
 {
     $parameters = $this->getParameters();
     $encoder = $this->codecMatcher->getEncoder();
     $outputMediaType = $this->codecMatcher->getEncoderRegisteredMatchedType();
     $content = $encoder->encode($resource, $links, $meta, $parameters);
     $urlPrefix = $encoder->getEncoderOptions() === null ? null : $encoder->getEncoderOptions()->getUrlPrefix();
     $location = $urlPrefix . $this->schemaContainer->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
     return $this->responses->getCreatedResponse($location, $outputMediaType, $content, $this->supportedExtensions);
 }
Example #8
0
 /**
  * @param object                                                             $resource
  * @param array<string,\Neomerx\JsonApi\Contracts\Schema\LinkInterface>|null $links
  * @param mixed                                                              $meta
  *
  * @return Response
  */
 protected function getCreatedResponse($resource, $links = null, $meta = null)
 {
     $integration = $this->getIntegration();
     $parameters = $this->getParameters();
     $encoder = $this->codecMatcher->getEncoder();
     $outputMediaType = $this->codecMatcher->getEncoderRegisteredMatchedType();
     $links === null ?: $encoder->withLinks($links);
     $meta === null ?: $encoder->withMeta($meta);
     $content = $encoder->encodeData($resource, $parameters);
     /** @var ResponsesInterface $responses */
     $responses = $integration->getFromContainer(ResponsesInterface::class);
     /** @var ContainerInterface $schemaContainer */
     $schemaContainer = $integration->getFromContainer(ContainerInterface::class);
     $config = $integration->getFromContainer(C::class);
     $urlPrefix = isset($config[C::JSON][C::JSON_URL_PREFIX]) === true ? $config[C::JSON][C::JSON_URL_PREFIX] : null;
     $location = $urlPrefix . $schemaContainer->getSchema($resource)->getSelfSubLink($resource)->getSubHref();
     return $responses->getCreatedResponse($location, $outputMediaType, $content, $this->supportedExtensions);
 }