<commit_before><?php

namespace Perfumer\Framework\Router\Http;

use Perfumer\Framework\Bundle\Resolver\HttpResolver;
use Perfumer\Framework\Router\RouterInterface;
use Perfumer\Helper\Arr;
use Perfumer\Framework\Proxy\Exception\ProxyException;
use Perfumer\Framework\Proxy\Response;
use Symfony\Component\HttpFoundation\Response as ExternalResponse;

class DefaultRouter implements RouterInterface
{
    /**
     * @var HttpResolver
     */
    protected $bundle_resolver;

    /**
     * @var ExternalResponse
     */
    protected $response;

    protected $input;

    protected $options = [];

    protected $http_prefixes = [];
    protected $http_id;
    protected $http_id_array;
    protected $http_fields = [];
    protected $http_query = [];
    protected $http_args = [];

    public function __construct(HttpResolver $bundle_resolver, $options = [])
    {
        $this->bundle_resolver = $bundle_resolver;

        $default_options = [
            'auto_null' => true,
            'auto_trim' => true,
            'data_type' => 'query_string',
            'default_url' => 'home',
            'prefixes' => [],
            'prefix_options' => [],
            'allowed_actions' => ['get', 'post', 'head', 'options'],
            'not_found_attributes' => ['framework/http', 'exception/template', 'controllerNotFound']
        ];

        $this->options = array_merge($default_options, $options);
    }

    /**
     * @return array
     */
    public function getAllowedActions()
    {
        return $this->options['allowed_actions'];
    }

    /**
     * @return array
     */
    public function getNotFoundAttributes()
    {
        return $this->options['not_found_attributes'];
    }

    /**
     * @return bool
     */
    public function isHttp()
    {
        return true;
    }

    /**
     * @return array
     */
    public function dispatch()
    {
        $action = strtolower($_SERVER['REQUEST_METHOD']);

        if ($prefixes = $this->options['prefixes']) {
            $this->http_prefixes = array_fill_keys($prefixes, null);

            $prefix_options = $this->options['prefix_options'];

            if (is_array($prefix_options)) {
                foreach ($prefixes as $prefix) {
                    if (isset($prefix_options[$prefix]['default_value'])) {
                        $this->http_prefixes[$prefix] = $prefix_options[$prefix]['default_value'];
                    }
                }
            }
        }

        if ($_SERVER['PATH_INFO'] == '/') {
            $url = $this->options['default_url'];
        } else {
            $url = trim($_SERVER['PATH_INFO'], '/');

            // Try to define prefixes
            if ($prefixes = $this->options['prefixes']) {
                $url = explode('/', $url);

                $prefix_options = $this->options['prefix_options'];

                while ($prefixes && $url) {
                    $one_prefix = array_shift($prefixes);
                    $one_url = $url[0];

                    if ($this->validateUrlPartForPrefix($one_prefix, $one_url, $prefix_options)) {
                        $this->http_prefixes[$one_prefix] = $one_url;

                        array_shift($url);
                    }
                }

                $url = count($url) > 0 ? implode('/', $url) : $this->options['default_url'];
            }

            // Try to define id
            if ($url) {
                if ((int) $url > 0) {
                    $this->http_id = $url;
                } else {
                    preg_match('/\/[0-9]+/', $url, $matches, PREG_OFFSET_CAPTURE);

                    if (isset($matches[0])) {
                        $slash_position = $matches[0][1];

                        $this->http_id = substr($url, $slash_position + 1);

                        if ($slash_position > 0) {
                            $url = substr($url, 0, $slash_position);
                        }
                    }
                }
            }
        }

        $this->input = file_get_contents("php://input");

        $data_type = $this->options['data_type'];

        // Get query parameters and args depending from type of data in the http request body
        if ($data_type == 'query_string') {
            switch ($action) {
                case 'get':
                    $this->http_query = $_GET;
                    break;
                case 'post':
                    $this->http_query = $_GET;
                    $this->http_args = $_POST;
                    break;
                default:
                    $this->http_query = $_GET;
                    parse_str($this->getInput(), $this->http_args);
                    break;
            }
        } else if ($data_type == 'json') {
            $this->http_query = $_GET;
            $this->http_args = $this->getInput() ? json_decode($this->getInput(), true) : [];

            if (!is_array($this->http_args)) {
                $this->http_args = [];
            }
        }

        // Trim all args if auto_trim setting enabled
        if ($this->options['auto_trim']) {
            $this->http_args = Arr::trim($this->http_args);
        }

        // Convert empty strings to null values if auto_null setting enabled
        if ($this->options['auto_null']) {
            $this->http_args = Arr::convertValues($this->http_args, '', null);
        }
<commit_msg>Auto trim and auto null on query parameters<commit_after><?php

namespace Perfumer\Framework\Router\Http;

use Perfumer\Framework\Bundle\Resolver\HttpResolver;
use Perfumer\Framework\Router\RouterInterface;
use Perfumer\Helper\Arr;
use Perfumer\Framework\Proxy\Exception\ProxyException;
use Perfumer\Framework\Proxy\Response;
use Symfony\Component\HttpFoundation\Response as ExternalResponse;

class DefaultRouter implements RouterInterface
{
    /**
     * @var HttpResolver
     */
    protected $bundle_resolver;

    /**
     * @var ExternalResponse
     */
    protected $response;

    protected $input;

    protected $options = [];

    protected $http_prefixes = [];
    protected $http_id;
    protected $http_id_array;
    protected $http_fields = [];
    protected $http_query = [];
    protected $http_args = [];

    public function __construct(HttpResolver $bundle_resolver, $options = [])
    {
        $this->bundle_resolver = $bundle_resolver;

        $default_options = [
            'auto_null' => true,
            'auto_trim' => true,
            'data_type' => 'query_string',
            'default_url' => 'home',
            'prefixes' => [],
            'prefix_options' => [],
            'allowed_actions' => ['get', 'post', 'head', 'options'],
            'not_found_attributes' => ['framework/http', 'exception/template', 'controllerNotFound']
        ];

        $this->options = array_merge($default_options, $options);
    }

    /**
     * @return array
     */
    public function getAllowedActions()
    {
        return $this->options['allowed_actions'];
    }

    /**
     * @return array
     */
    public function getNotFoundAttributes()
    {
        return $this->options['not_found_attributes'];
    }

    /**
     * @return bool
     */
    public function isHttp()
    {
        return true;
    }

    /**
     * @return array
     */
    public function dispatch()
    {
        $action = strtolower($_SERVER['REQUEST_METHOD']);

        if ($prefixes = $this->options['prefixes']) {
            $this->http_prefixes = array_fill_keys($prefixes, null);

            $prefix_options = $this->options['prefix_options'];

            if (is_array($prefix_options)) {
                foreach ($prefixes as $prefix) {
                    if (isset($prefix_options[$prefix]['default_value'])) {
                        $this->http_prefixes[$prefix] = $prefix_options[$prefix]['default_value'];
                    }
                }
            }
        }

        if ($_SERVER['PATH_INFO'] == '/') {
            $url = $this->options['default_url'];
        } else {
            $url = trim($_SERVER['PATH_INFO'], '/');

            // Try to define prefixes
            if ($prefixes = $this->options['prefixes']) {
                $url = explode('/', $url);

                $prefix_options = $this->options['prefix_options'];

                while ($prefixes && $url) {
                    $one_prefix = array_shift($prefixes);
                    $one_url = $url[0];

                    if ($this->validateUrlPartForPrefix($one_prefix, $one_url, $prefix_options)) {
                        $this->http_prefixes[$one_prefix] = $one_url;

                        array_shift($url);
                    }
                }

                $url = count($url) > 0 ? implode('/', $url) : $this->options['default_url'];
            }

            // Try to define id
            if ($url) {
                if ((int) $url > 0) {
                    $this->http_id = $url;
                } else {
                    preg_match('/\/[0-9]+/', $url, $matches, PREG_OFFSET_CAPTURE);

                    if (isset($matches[0])) {
                        $slash_position = $matches[0][1];

                        $this->http_id = substr($url, $slash_position + 1);

                        if ($slash_position > 0) {
                            $url = substr($url, 0, $slash_position);
                        }
                    }
                }
            }
        }

        $this->input = file_get_contents("php://input");

        $data_type = $this->options['data_type'];

        // Get query parameters and args depending from type of data in the http request body
        if ($data_type == 'query_string') {
            switch ($action) {
                case 'get':
                    $this->http_query = $_GET;
                    break;
                case 'post':
                    $this->http_query = $_GET;
                    $this->http_args = $_POST;
                    break;
                default:
                    $this->http_query = $_GET;
                    parse_str($this->getInput(), $this->http_args);
                    break;
            }
        } else if ($data_type == 'json') {
            $this->http_query = $_GET;
            $this->http_args = $this->getInput() ? json_decode($this->getInput(), true) : [];

            if (!is_array($this->http_args)) {
                $this->http_args = [];
            }
        }

        // Trim all args if auto_trim setting enabled
        if ($this->options['auto_trim']) {
            $this->http_args = Arr::trim($this->http_args);
            $this->http_query = Arr::trim($this->http_query);
        }

        // Convert empty strings to null values if auto_null setting enabled
        if ($this->options['auto_null']) {
            $this->http_args = Arr::convertValues($this->http_args, '', null);
            $this->http_query = Arr::convertValues($this->http_query, '', null);
        }

{'fraction_non_alphanumeric': 0.14064362336114422, 'fraction_numerical': 0.0020170532685431374, 'mean_word_length': 1.6424418604651163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3733483', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2905, 'n_words': 558}