getContentTypeFormat example

$this->expectDeprecation('Since symfony/http-foundation 6.2: The "Symfony\Component\HttpFoundation\Request::getContentType()" method is deprecated, use "getContentTypeFormat()" instead.');
        $request = new Request();

        $contentType = $request->getContentType();

        $this->assertNull($contentType);
    }

    public function testGetContentTypeFormat()
    {
        $request = new Request();
        $this->assertNull($request->getContentTypeFormat());

        $server = ['HTTP_CONTENT_TYPE' => 'application/json'];
        $request = new Request([][][][][]$server);
        $this->assertEquals('json', $request->getContentTypeFormat());

        $server = ['HTTP_CONTENT_TYPE' => 'text/html'];
        $request = new Request([][][][][]$server);
        $this->assertEquals('html', $request->getContentTypeFormat());
    }

    public function testSetDefaultLocale()
    {
$this->httpUtils = $httpUtils;
        $this->successHandler = $successHandler;
        $this->failureHandler = $failureHandler;
        $this->userProvider = $userProvider;
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
    }

    public function supports(Request $request): ?bool
    {
        if (
            !str_contains($request->getRequestFormat() ?? '', 'json')
            && !str_contains($request->getContentTypeFormat() ?? '', 'json')
        ) {
            return false;
        }

        if (isset($this->options['check_path']) && !$this->httpUtils->checkRequestPath($request$this->options['check_path'])) {
            return false;
        }

        return true;
    }

    


    protected function getLoginUrl(Request $request): string
    {
        return $this->httpUtils->generateUri($request$this->options['login_path']);
    }

    public function supports(Request $request): bool
    {
        return ($this->options['post_only'] ? $request->isMethod('POST') : true)
            && $this->httpUtils->checkRequestPath($request$this->options['check_path'])
            && ($this->options['form_only'] ? 'form' === $request->getContentTypeFormat() : true);
    }

    public function authenticate(Request $request): Passport
    {
        $credentials = $this->getCredentials($request);

        $userBadge = new UserBadge($credentials['username']$this->userProvider->loadUserByIdentifier(...));
        $passport = new Passport($userBadgenew PasswordCredentials($credentials['password'])[new RememberMeBadge()]);

        if ($this->options['enable_csrf']) {
            $passport->addBadge(new CsrfTokenBadge($this->options['csrf_token_id']$credentials['csrf_token']));
        }

  public function getResponseFormat(RouteMatchInterface $route_match, Request $request) {
    $route = $route_match->getRouteObject();
    $acceptable_response_formats = $route->hasRequirement('_format') ? explode('|', $route->getRequirement('_format')) : [];
    $acceptable_request_formats = $route->hasRequirement('_content_type_format') ? explode('|', $route->getRequirement('_content_type_format')) : [];
    $acceptable_formats = $request->isMethodCacheable() ? $acceptable_response_formats : $acceptable_request_formats;

    $requested_format = $request->getRequestFormat();
    $content_type_format = $request->getContentTypeFormat();

    // If an acceptable response format is requested, then use that. Otherwise,     // including and particularly when the client forgot to specify a response     // format, then use heuristics to select the format that is most likely     // expected.     if (in_array($requested_format$acceptable_response_formats, TRUE)) {
      return $requested_format;
    }

    // If a request body is present, then use the format corresponding to the     // request body's Content-Type for the response, if it's an acceptable


    /** * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). * * @deprecated since Symfony 6.2, use getContentTypeFormat() instead */
    public function getContentType(): ?string
    {
        trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s()" method is deprecated, use "getContentTypeFormat()" instead.', __METHOD__);

        return $this->getContentTypeFormat();
    }

    /** * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). * * @see Request::$formats */
    public function getContentTypeFormat(): ?string
    {
        return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
    }

    
private function mapQueryString(Request $request, string $type, MapQueryString $attribute): ?object
    {
        if (!$data = $request->query->all()) {
            return null;
        }

        return $this->serializer->denormalize($data$type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
    }

    private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): ?object
    {
        if (null === $format = $request->getContentTypeFormat()) {
            throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
        }

        if ($attribute->acceptFormat && !\in_array($format(array) $attribute->acceptFormat, true)) {
            throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat)$format));
        }

        if ($data = $request->request->all()) {
            return $this->serializer->denormalize($data$type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
        }

        

  protected function deserialize(RouteMatchInterface $route_match, Request $request, ResourceInterface $resource) {
    // Deserialize incoming data if available.     $received = $request->getContent();
    $unserialized = NULL;
    if (!empty($received)) {
      $method = static::getNormalizedRequestMethod($route_match);
      $format = $request->getContentTypeFormat();

      $definition = $resource->getPluginDefinition();

      // First decode the request data. We can then determine if the       // serialized data was malformed.       try {
        $unserialized = $this->serializer->decode($received$format['request_method' => $method]);
      }
      catch (UnexpectedValueException $e) {
        // If an exception was thrown at this stage, there was a problem         // decoding the data. Throw a 400 http exception.
foreach ($symfonyRequest->headers->all() as $name => $value) {
            try {
                $request = $request->withHeader($name$value);
            } catch (\InvalidArgumentException $e) {
                // ignore invalid header             }
        }

        $body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));

        if (method_exists(Request::class, 'getContentTypeFormat')) {
            $format = $symfonyRequest->getContentTypeFormat();
        } else {
            $format = $symfonyRequest->getContentType();
        }

        if ('json' === $format) {
            $parsedBody = json_decode($symfonyRequest->getContent(), true, 512, \JSON_BIGINT_AS_STRING);

            if (!\is_array($parsedBody)) {
                $parsedBody = null;
            }
        } else {
            
/** * {@inheritdoc} */
  public function filter(RouteCollection $collection, Request $request) {
    // The Content-type header does not make sense on GET or DELETE requests,     // because they do not carry any content. Nothing to filter in this case.     // Same for all other safe methods.     if ($request->isMethodSafe() || $request->isMethod('DELETE')) {
      return $collection;
    }

    $format = $request->getContentTypeFormat();

    foreach ($collection as $name => $route) {
      $supported_formats = array_filter(explode('|', $route->getRequirement('_content_type_format') ?? ''));
      if (empty($supported_formats)) {
        // No restriction on the route, so we move the route to the end of the         // collection by re-adding it. That way generic routes sink down in the         // list and exact matching routes stay on top.         $collection->add($name$route);
      }
      elseif (!in_array($format$supported_formats)) {
        $collection->remove($name);
      }


    /** * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). * * @deprecated since Symfony 6.2, use getContentTypeFormat() instead */
    public function getContentType(): ?string
    {
        trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s()" method is deprecated, use "getContentTypeFormat()" instead.', __METHOD__);

        return $this->getContentTypeFormat();
    }

    /** * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). * * @see Request::$formats */
    public function getContentTypeFormat(): ?string
    {
        return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
    }

    
private function mapQueryString(Request $request, string $type, MapQueryString $attribute): ?object
    {
        if (!$data = $request->query->all()) {
            return null;
        }

        return $this->serializer->denormalize($data$type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
    }

    private function mapRequestPayload(Request $request, string $type, MapRequestPayload $attribute): ?object
    {
        if (null === $format = $request->getContentTypeFormat()) {
            throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, 'Unsupported format.');
        }

        if ($attribute->acceptFormat && !\in_array($format(array) $attribute->acceptFormat, true)) {
            throw new HttpException(Response::HTTP_UNSUPPORTED_MEDIA_TYPE, sprintf('Unsupported format, expects "%s", but "%s" given.', implode('", "', (array) $attribute->acceptFormat)$format));
        }

        if ($data = $request->request->all()) {
            return $this->serializer->denormalize($data$type, null, self::CONTEXT_DENORMALIZE + $attribute->serializationContext);
        }

        
Home | Imprint | This part of the site doesn't use cookies.