matchDecoder() public method

Find best decoder match for 'Content-Type' header.
public matchDecoder ( Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface $contentTypeHeader ) : void
$contentTypeHeader Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface
return void
Beispiel #1
0
 /**
  * Check content type header
  *
  * @param HeaderInterface $header
  */
 private function checkContentTypeHeader(HeaderInterface $header)
 {
     if (count($header->getMediaTypes()) > 1) {
         throw new JsonApiException($this->createApiError(JsonApiException::HTTP_CODE_BAD_REQUEST, self::INVALID_CONTENT_TYPE_ERROR, "Invalid content type"), JsonApiException::HTTP_CODE_BAD_REQUEST);
     }
     $this->matcher->matchDecoder($header);
     if (null === $this->matcher->getDecoderHeaderMatchedType()) {
         throw new JsonApiException($this->createApiError(JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE, self::UNSUPPORTED_CONTENT_TYPE_ERROR, 'Unsupported content type'), JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE);
     }
 }
 /**
  * @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);
     }
 }