<commit_before><?php
/**
 * Response
 *
 * Handles the HTTP Response for the current execution.
 * 
 * @package core
 * @author <EMAIL>
 * @version 1.0
 * @copyright Caffeina srl - 2014 - http://caffeina.co
 */

class Response {
    use Module;

    const TYPE_JSON          = 'application/json';
    const TYPE_HTML          = 'text/html';
    const TYPE_TEXT          = 'text/plain';
    const TYPE_CSS           = 'text/css';
    const TYPE_XML           = 'text/xml';
    const TYPE_SVG           = 'image/svg+xml';
    const TYPE_JS            = 'application/javascript';
    const TYPE_BIN           = 'application/octet-stream';

    protected static $payload     = [];
    protected static $status      = 200;
    protected static $headers     = ['Content-Type' => ['text/html'] ];
    protected static $buffer      = null;
    protected static $link        = null;

    public static function type($mime){
        static::header('Content-Type',$mime);
    }

    /**
     * Start capturing output
     */
    public static function start(){
        static::$buffer = ob_start();
    }

    /**
     * Enable CORS HTTP headers.
     */
    public static function enableCORS(){
        
        // Allow from any origin
        if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) {
          static::header('Access-Control-Allow-Origin', $origin);
          static::header('Access-Control-Allow-Credentials', 'true');
          static::header('Access-Control-Max-Age', 86400);
        }

        // Access-Control headers are received during OPTIONS requests
        if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') {
            static::clean();

            if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) {
              static::header('Access-Control-Allow-Methods',
                'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE');
            }
            if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) {
              static::header('Access-Control-Allow-Headers',$req_h);
            }
            
            static::send();
            exit;
        }
    }

    /**
     * Finish the output buffer capturing.
     * @return string The captured buffer
     */
    public static function end(){
        if (static::$buffer){
            static::$payload[] = ob_get_contents();
            ob_end_clean();
            static::$buffer = null;
            return end(static::$payload);
        }
    }

    /**
     * Check if an response output buffering is active.
     * @return boolean
     */
    public static function isBuffering(){
        return static::$buffer;
    }

    /**
     * Clear the response body
     */
    public static function clean(){
        static::$payload = [];
    }

    /**
     * Append a JSON object to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function json($payload){
        static::type(static::TYPE_JSON);
        static::$payload[] = json_encode($payload,JSON_NUMERIC_CHECK);
    }

    /**
     * Append a text to the buffer.
     * @param  mixed $payload Text to append to the response buffer
     */
    public static function text(){
        static::type(static::TYPE_TEXT);
        static::$payload[] = implode('',func_get_args());
    }

    /**
     * Append an XML string to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function xml(){
        static::type(static::TYPE_XML);
        static::$payload[] = implode('',func_get_args());
    }

    /**
     * Append a SVG string to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */<commit_msg>Response new option for passing json_encode flags<commit_after><?php
/**
 * Response
 *
 * Handles the HTTP Response for the current execution.
 * 
 * @package core
 * @author <EMAIL>
 * @version 1.0
 * @copyright Caffeina srl - 2014 - http://caffeina.co
 */

class Response {
    use Module;

    const TYPE_JSON          = 'application/json';
    const TYPE_HTML          = 'text/html';
    const TYPE_TEXT          = 'text/plain';
    const TYPE_CSS           = 'text/css';
    const TYPE_XML           = 'text/xml';
    const TYPE_SVG           = 'image/svg+xml';
    const TYPE_JS            = 'application/javascript';
    const TYPE_BIN           = 'application/octet-stream';

    protected static $payload     = [];
    protected static $status      = 200;
    protected static $headers     = ['Content-Type' => ['text/html'] ];
    protected static $buffer      = null;
    protected static $link        = null;

    public static function type($mime){
        static::header('Content-Type',$mime);
    }

    /**
     * Start capturing output
     */
    public static function start(){
        static::$buffer = ob_start();
    }

    /**
     * Enable CORS HTTP headers.
     */
    public static function enableCORS(){
        
        // Allow from any origin
        if ($origin = filter_input(INPUT_SERVER,'HTTP_ORIGIN')) {
          static::header('Access-Control-Allow-Origin', $origin);
          static::header('Access-Control-Allow-Credentials', 'true');
          static::header('Access-Control-Max-Age', 86400);
        }

        // Access-Control headers are received during OPTIONS requests
        if (filter_input(INPUT_SERVER,'REQUEST_METHOD') == 'OPTIONS') {
            static::clean();

            if (filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_METHOD')) {
              static::header('Access-Control-Allow-Methods',
                'GET, POST, PUT, DELETE, OPTIONS, HEAD, CONNECT, PATCH, TRACE');
            }
            if ($req_h = filter_input(INPUT_SERVER,'HTTP_ACCESS_CONTROL_REQUEST_HEADERS')) {
              static::header('Access-Control-Allow-Headers',$req_h);
            }
            
            static::send();
            exit;
        }
    }

    /**
     * Finish the output buffer capturing.
     * @return string The captured buffer
     */
    public static function end(){
        if (static::$buffer){
            static::$payload[] = ob_get_contents();
            ob_end_clean();
            static::$buffer = null;
            return end(static::$payload);
        }
    }

    /**
     * Check if an response output buffering is active.
     * @return boolean
     */
    public static function isBuffering(){
        return static::$buffer;
    }

    /**
     * Clear the response body
     */
    public static function clean(){
        static::$payload = [];
    }

    /**
     * Append a JSON object to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function json($payload){
        static::type(static::TYPE_JSON);
        static::$payload[] = json_encode($payload, Options::get('core.response.json_flags',JSON_NUMERIC_CHECK));
    }

    /**
     * Append a text to the buffer.
     * @param  mixed $payload Text to append to the response buffer
     */
    public static function text(){
        static::type(static::TYPE_TEXT);
        static::$payload[] = implode('',func_get_args());
    }

    /**
     * Append an XML string to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
    public static function xml(){
        static::type(static::TYPE_XML);
        static::$payload[] = implode('',func_get_args());
    }

    /**
     * Append a SVG string to the buffer.
     * @param  mixed $payload Data to append to the response buffer
     */
{'fraction_non_alphanumeric': 0.12434071729957806, 'fraction_numerical': 0.003691983122362869, 'mean_word_length': 2.1086065573770494, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4512374', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2058, 'n_words': 545}