<commit_before><?php

namespace FOS\RestBundle\View;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class RequestListener
{
    protected $detectFormat;
    protected $decodeBody;
    protected $defaultFormat;

    public function __construct($detectFormat, $decodeBody, $defaultFormat)
    {
        $this->detectFormat = $detectFormat;
        $this->decodeBody = $decodeBody;
        $this->defaultFormat = $defaultFormat;
    }

    public function onCoreRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if ($this->detectFormat) {
            $this->detectFormat($request);
        }

        if ($this->decodeBody) {
            $this->detectFormat($request);
        }
    }

    protected function detectFormat($request)
    {
        // TODO enable once https://github.com/symfony/symfony/pull/565 is merged
//        $format = $request->getRequestFormat(null);
        $format = $request->get('_format');
        if (null === $format) {
            $formats = $this->splitHttpAcceptHeader($request->headers->get('Accept'));
            if (!empty($formats)) {
                $format = $request->getFormat(key($formats));
            }

            if (null === $format) {
                $format = $this->defaultFormat;
            }
        }

        $request->setRequestFormat($format);
    }

    protected function decodeBody($request)
    {
        if (in_array($request->getMethod(), array('POST', 'PUT'))) {
            switch ($request->headers->get('Content-Type')) {
                case 'json':
                    $post = json_decode($request->getContent());
                    break;
                case 'xml':
                    $post = (array)simplexml_load_string($request->getContent());
                    break;
                default:
                    return;
            }

            $request->request = new ParameterBag($post);
        }
    }

    /**
     * Splits an Accept-* HTTP header.
     * TODO remove once https://github.com/symfony/symfony/pull/565 is merged
     *
     * @param string $header  Header to split
     */
    private function splitHttpAcceptHeader($header)
    {
        if (!$header) {
            return array();
        }

        $values = array();<commit_msg>convert content type to a normalized format<commit_after><?php

namespace FOS\RestBundle\View;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class RequestListener
{
    protected $detectFormat;
    protected $decodeBody;
    protected $defaultFormat;

    public function __construct($detectFormat, $decodeBody, $defaultFormat)
    {
        $this->detectFormat = $detectFormat;
        $this->decodeBody = $decodeBody;
        $this->defaultFormat = $defaultFormat;
    }

    public function onCoreRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if ($this->detectFormat) {
            $this->detectFormat($request);
        }

        if ($this->decodeBody) {
            $this->detectFormat($request);
        }
    }

    protected function detectFormat($request)
    {
        // TODO enable once https://github.com/symfony/symfony/pull/565 is merged
//        $format = $request->getRequestFormat(null);
        $format = $request->get('_format');
        if (null === $format) {
            $formats = $this->splitHttpAcceptHeader($request->headers->get('Accept'));
            if (!empty($formats)) {
                $format = $request->getFormat(key($formats));
            }

            if (null === $format) {
                $format = $this->defaultFormat;
            }
        }

        $request->setRequestFormat($format);
    }

    protected function decodeBody($request)
    {
        if (in_array($request->getMethod(), array('POST', 'PUT'))) {
            switch ($request->getFormat($request->headers->get('Content-Type'))) {
                case 'json':
                    $post = json_decode($request->getContent());
                    break;
                case 'xml':
                    $post = (array)simplexml_load_string($request->getContent());
                    break;
                default:
                    return;
            }

            $request->request = new ParameterBag($post);
        }
    }

    /**
     * Splits an Accept-* HTTP header.
     * TODO remove once https://github.com/symfony/symfony/pull/565 is merged
     *
     * @param string $header  Header to split
     */
    private function splitHttpAcceptHeader($header)
    {
        if (!$header) {
            return array();
        }

        $values = array();
{'fraction_non_alphanumeric': 0.13352088292577363, 'fraction_numerical': 0.002596840510711967, 'mean_word_length': 1.917929292929293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1745442', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1198, 'n_words': 233}