<commit_before><?php
/**
 * This file is part of the BEAR.Sunday package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Package\Provide\Router;

final class OverrideMethod
{
    /**
     * Request method
     *
     * @var string
     */
    private $method;

    /**
     * HTTP Parameters
     *
     * @var array
     */
    private $query;

    /**
     * Return http method and queries
     *
     * 'queries' will change by method.
     * get method return $_GET, post method return $_POST + $_GET
     * patch | put | delete  return parsed 'php://input' value
     *
     * @param array $server $_SERVER
     * @param array $get    $_GET
     * @param array $post   $_POST
     *
     * @return array
     */
    public function get(array $server, array $get, array $post) {

        // set the original value
        $this->method = strtolower($server['REQUEST_METHOD']);

        // early return on GET
        if ($this->method === 'get') {
            return [$this->method, $get];
        }
        // must be a POST to do an override
        if ($this->method === 'post' && $this->methodOverRide($server, $post)) {
            return [$this->method, $this->query];
        }

        // native method POST / PUT / PATCH / DELETE
        return [$this->method, $this->getParams($this->method, $get, $post)];
    }

    /**
     * HTTP Method override
     *
     * @param array $server
     * @param array $post
     *
     * @return bool is override ?
     */
    private function methodOverRide(array $server, array $post)
    {
        // look for override in post data
        if (isset($post['_method'])) {
            $this->method = strtolower($post['_method']);
            unset($post['_method']);
            $this->query = $post;

            return true;
        }

        // look for override in headers
        if (isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
            $this->method = strtolower($server['HTTP_X_HTTP_METHOD_OVERRIDE']);
            $this->query = $post;

            return true;
        }

        return false;
    }

    /**
     * Return request parameters
     *
     * @param string $method Request method
     * @param array  $get    $_GET
     * @param array  $post   $_POST
     *
     * @return array
     */
    private function getParams($method, array $get, array $post)
    {
        if ($method === 'put' || $method === 'patch'  || $method === 'delete') {

            return $this->phpInput();
        }

        // post
        return $post + $get;
    }

    /**
     * Return 'php://input' query
     *
     * @return array
     */
    private function phpInput()
    {
        $input = file_get_contents('php://input');
        $input ?  parse_str(file_get_contents('php://input'), $put) : $put = [];

        return $put;
    }
}<commit_msg>post does not merge get<commit_after><?php
/**
 * This file is part of the BEAR.Sunday package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Package\Provide\Router;

final class OverrideMethod
{
    /**
     * Request method
     *
     * @var string
     */
    private $method;

    /**
     * HTTP Parameters
     *
     * @var array
     */
    private $query;

    /**
     * Return http method and queries
     *
     * 'queries' will change by method.
     * get method return $_GET, post method return $_POST + $_GET
     * patch | put | delete  return parsed 'php://input' value
     *
     * @param array $server $_SERVER
     * @param array $get    $_GET
     * @param array $post   $_POST
     *
     * @return array
     */
    public function get(array $server, array $get, array $post) {

        // set the original value
        $this->method = strtolower($server['REQUEST_METHOD']);

        // early return on GET
        if ($this->method === 'get') {
            return [$this->method, $get];
        }
        // must be a POST to do an override
        if ($this->method === 'post' && $this->methodOverRide($server, $post)) {
            return [$this->method, $this->query];
        }

        // native method POST / PUT / PATCH / DELETE
        return [$this->method, $this->getParams($this->method, $get, $post)];
    }

    /**
     * HTTP Method override
     *
     * @param array $server
     * @param array $post
     *
     * @return bool is override ?
     */
    private function methodOverRide(array $server, array $post)
    {
        // look for override in post data
        if (isset($post['_method'])) {
            $this->method = strtolower($post['_method']);
            unset($post['_method']);
            $this->query = $post;

            return true;
        }

        // look for override in headers
        if (isset($server['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
            $this->method = strtolower($server['HTTP_X_HTTP_METHOD_OVERRIDE']);
            $this->query = $post;

            return true;
        }

        return false;
    }

    /**
     * Return request parameters
     *
     * @param string $method Request method
     * @param array  $get    $_GET
     * @param array  $post   $_POST
     *
     * @return array
     */
    private function getParams($method, array $get, array $post)
    {
        if ($method === 'put' || $method === 'patch'  || $method === 'delete') {

            return $this->phpInput();
        }

        return $post;
    }

    /**
     * Return 'php://input' query
     *
     * @return array
     */
    private function phpInput()
    {
        $input = file_get_contents('php://input');
        $input ?  parse_str(file_get_contents('php://input'), $put) : $put = [];

        return $put;
    }
}
{'fraction_non_alphanumeric': 0.1430831429073651, 'fraction_numerical': 0.0, 'mean_word_length': 1.8322548531607765, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '5884509', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1650, 'n_words': 448}