<commit_before><?php
/*
 * Inphinit
 *
 * Copyright (c) 2018 <NAME> (<EMAIL>)
 *
 * Released under the MIT license
 */

namespace Inphinit\Experimental\Http;

use Inphinit\Http\Request;
use Inphinit\Experimental\Exception;

class Negotiation
{
    private $headers;

    /** Sort values in the header low to high by q-factors */
    const LOW = 1;

    /** Sort values in the header high to low by q-factors */
    const HIGH = 2;

    /** Get all values from a accept header (without q-factor) */
    const ALL = 3;

    /**
     * Create a Negotiation instance
     *
     * @param array $headers This parameter is optional, you can set with
     *                       headers returned by curl or other way
     * @return void
     */
    public function __construct(array $headers = null)
    {
        $this->headers = array_change_key_case($headers ? $headers : Request::header(), CASE_LOWER);

        self::filter($this->headers);
    }

    /**
     * Get all languages by `Accept-Language` header sorted by q-factor (defined by `$level`)
     *
     * @param int $level Sorts languages using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function acceptLanguage($level = self::HIGH)
    {
        return $this->header('accept-language', $level);
    }

    /**
     * Get all languages by `Accept-Charset` header and sort by q-factor (defined by `$level`)
     *
     * @param int $level Sorts charsets using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array
     */
    public function acceptCharset($level = self::HIGH)
    {
        return $this->header('accept-charset', $level);
    }

    /**
     * Get all languages by `Accept-Encoding` header and sort by q-factor (defined by `$level`)
     *
     * @param string $level Sorts encodings using `LOW` or `HIGH` constants,
     *                      or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function acceptEncoding($level = self::HIGH)
    {
        return $this->header('accept-encoding', $level);
    }

    /**
     * Get all document types by `Accept` header and sorted by q-factor (defined by `$level`)
     *
     * @param int $level Sorts types using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function accept($level = self::HIGH)
    {
        return $this->header('accept', $level);
    }

    /**
     * Get the first language with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getLanguage($alternative = false)
    {
        $headers = $this->languages();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first charset with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getCharset($alternative = false)
    {
        $headers = $this->charsets();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first encoding with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getEncoding($alternative = false)
    {
        $headers = $this->encodings();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first language with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getAccept($alternative = false)
    {
        $headers = $this->accept();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Parse any header like `TE` header or headers with `Accepet-` prefix
     *
     * @param string $header
     * @param int    $level
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function header($header, $level = self::HIGH)
    {
        $header = strtolower($header);

        if (empty($this->headers[$header])) {
            return false;
        }

        return self::qFactor($this->headers[$header], $level);
    }

    /**
     * Parse and sort a custom value with q-factor
     *
     * @param string $value
     * @param int    $level
     * @throws \Inphinit\Experimental\Exception<commit_msg>Fix Negotiation class<commit_after><?php
/*
 * Inphinit
 *
 * Copyright (c) 2018 <NAME> (<EMAIL>)
 *
 * Released under the MIT license
 */

namespace Inphinit\Experimental\Http;

use Inphinit\Http\Request;
use Inphinit\Experimental\Exception;

class Negotiation
{
    private $headers;

    /** Sort values in the header low to high by q-factors */
    const LOW = 1;

    /** Sort values in the header high to low by q-factors */
    const HIGH = 2;

    /** Get all values from a accept header (without q-factor) */
    const ALL = 3;

    /**
     * Create a Negotiation instance
     *
     * @param array $headers This parameter is optional, you can set with
     *                       headers returned by curl or other way
     * @return void
     */
    public function __construct(array $headers = null)
    {
        $this->headers = array_change_key_case($headers ? $headers : Request::header(), CASE_LOWER);

        self::filter($this->headers);
    }

    /**
     * Get all languages by `Accept-Language` header sorted by q-factor (defined by `$level`)
     *
     * @param int $level Sorts languages using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function acceptLanguage($level = self::HIGH)
    {
        return $this->header('accept-language', $level);
    }

    /**
     * Get all languages by `Accept-Charset` header and sort by q-factor (defined by `$level`)
     *
     * @param int $level Sorts charsets using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array
     */
    public function acceptCharset($level = self::HIGH)
    {
        return $this->header('accept-charset', $level);
    }

    /**
     * Get all languages by `Accept-Encoding` header and sort by q-factor (defined by `$level`)
     *
     * @param string $level Sorts encodings using `LOW` or `HIGH` constants,
     *                      or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function acceptEncoding($level = self::HIGH)
    {
        return $this->header('accept-encoding', $level);
    }

    /**
     * Get all document types by `Accept` header and sorted by q-factor (defined by `$level`)
     *
     * @param int $level Sorts types using `LOW` or `HIGH` constants,
     *                   or return all in an simple array use `ALL` constant
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function accept($level = self::HIGH)
    {
        return $this->header('accept', $level);
    }

    /**
     * Get the first language with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getLanguage($alternative = false)
    {
        $headers = $this->acceptLanguage();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first charset with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getCharset($alternative = false)
    {
        $headers = $this->acceptCharset();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first encoding with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getEncoding($alternative = false)
    {
        $headers = $this->acceptEncoding();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Get the first language with with the greatest q-factor,
     * if it does not exist then return the value of `$alternative`
     *
     * @param mixed $alternative Define alternative value, this value will be
     *                           used does not have the "header"
     * @throws \Inphinit\Experimental\Exception
     * @return mixed
     */
    public function getAccept($alternative = false)
    {
        $headers = $this->acceptEncoding();
        return $headers ? key($headers) : $alternative;
    }

    /**
     * Parse any header like `TE` header or headers with `Accepet-` prefix
     *
     * @param string $header
     * @param int    $level
     * @throws \Inphinit\Experimental\Exception
     * @return array|bool
     */
    public function header($header, $level = self::HIGH)
    {
        $header = strtolower($header);

        if (empty($this->headers[$header])) {
            return false;
        }

        return self::qFactor($this->headers[$header], $level);
    }

    /**
     * Parse and sort a custom value with q-factor
     *
     * @param string $value
     * @param int    $level
     * @throws \Inphinit\Experimental\Exception
{'fraction_non_alphanumeric': 0.10708210224729162, 'fraction_numerical': 0.0012534694242994002, 'mean_word_length': 2.2033266418124464, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1351222', 'n_tokens_mistral': 3112, 'n_tokens_neox': 3037, 'n_words': 1045}