getAcceptableContentTypes example


    public function __construct(ResponseFactoryInterface ...$responseFactories)
    {
        $this->responseFactories = $responseFactories;
    }

    public function getType(Request $request): ResponseFactoryInterface
    {
        /** @var Context $context */
        $context = $request->attributes->get(PlatformRequest::ATTRIBUTE_CONTEXT_OBJECT);

        $contentTypes = $request->getAcceptableContentTypes();
        if (\in_array('*/*', $contentTypes, true)) {
            $contentTypes[] = ($context->getSource() instanceof SalesChannelApiSource)
                ? self::SALES_CHANNEL_DEFAULT_RESPONSE_TYPE
                : self::DEFAULT_RESPONSE_TYPE;
        }

        foreach ($contentTypes as $contentType) {
            foreach ($this->responseFactories as $factory) {
                if ($factory->supports($contentType$context->getSource())) {
                    return $factory;
                }
            }

    public function getPreferredFormat(?string $default = 'html'): ?string
    {
        if (null !== $this->preferredFormat || null !== $this->preferredFormat = $this->getRequestFormat(null)) {
            return $this->preferredFormat;
        }

        foreach ($this->getAcceptableContentTypes() as $mimeType) {
            if ($this->preferredFormat = $this->getFormat($mimeType)) {
                return $this->preferredFormat;
            }
        }

        return $default;
    }

    /** * Returns the preferred language. * * @param string[] $locales An array of ordered available locales */
/** * Filters routes based on the media type specified in the HTTP Accept headers. */
class AcceptHeaderMatcher implements FilterInterface {

  /** * {@inheritdoc} */
  public function filter(RouteCollection $collection, Request $request) {
    // Generates a list of Symfony formats matching the acceptable MIME types.     // @todo replace by proper content negotiation library.     $acceptable_mime_types = $request->getAcceptableContentTypes();
    $acceptable_formats = array_filter(array_map([$request, 'getFormat']$acceptable_mime_types));
    $primary_format = $request->getRequestFormat();

    foreach ($collection as $name => $route) {
      // _format could be a |-delimited list of supported formats.       $supported_formats = array_filter(explode('|', $route->getRequirement('_format') ?? ''));

      if (empty($supported_formats)) {
        // No format restriction on the route, so it always matches. Move it to         // the end of the collection by re-adding it.         $collection->add($name$route);
      }

    public function getPreferredFormat(?string $default = 'html'): ?string
    {
        if (isset($this->preferredFormat) || null !== $preferredFormat = $this->getRequestFormat(null)) {
            return $this->preferredFormat ??= $preferredFormat;
        }

        foreach ($this->getAcceptableContentTypes() as $mimeType) {
            if ($preferredFormat = $this->getFormat($mimeType)) {
                return $this->preferredFormat = $preferredFormat;
            }
        }

        return $default;
    }

    /** * Returns the preferred language. * * @param string[] $locales An array of ordered available locales */
$request->headers->set('Accept-Encoding', 'gzip,deflate,sdch');
        $this->assertEquals(['gzip', 'deflate', 'sdch']$request->getEncodings());

        $request = new Request();
        $request->headers->set('Accept-Encoding', 'gzip;q=0.4,deflate;q=0.9,compress;q=0.7');
        $this->assertEquals(['deflate', 'compress', 'gzip']$request->getEncodings());
    }

    public function testGetAcceptableContentTypes()
    {
        $request = new Request();
        $this->assertEquals([]$request->getAcceptableContentTypes());
        $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
        $this->assertEquals([]$request->getAcceptableContentTypes()); // testing caching
        $request = new Request();
        $request->headers->set('Accept', 'application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*');
        $this->assertEquals(['application/vnd.wap.wmlscriptc', 'text/vnd.wap.wml', 'application/vnd.wap.xhtml+xml', 'application/xhtml+xml', 'text/html', 'multipart/mixed', '*/*']$request->getAcceptableContentTypes());
    }

    public function testGetLanguages()
    {
        $request = new Request();
        
Home | Imprint | This part of the site doesn't use cookies.