<commit_before><?php
/**
 * This file is part of the Everon framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Everon;

class Response implements Interfaces\Response
{
    protected $data = null;
    protected $HeaderCollection = null;
    protected $content_type = 'text/html';
    protected $charset = 'utf-8';
    protected $status = 200;
    protected $result = false;

    
    public function __construct(Interfaces\Collection $Headers)
    {
        $this->HeaderCollection = $Headers;
    }
    
    public function setData($data)
    {
        $this->data = $data;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getContentType()
    {
        return $this->content_type;
    }

    public function setContentType($content_type)
    {
        $this->content_type = strtolower($content_type);
    }
    
    public function getCharset()
    {
        return $this->charset;
    }

    public function setCharset($charset)
    {
        $this->charset = $charset;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($status)
    {
        $this->status = (int) $status;
    }
    
    public function getResult()
    {
        return $this->result;
    }

    public function setResult($result)
    {
        $this->result = (bool) $result;
    }
    
    protected function sendHeaders()
    {
        if ($this->HeaderCollection->has('content-type') === false) {
            switch ($this->getContentType()) {
                case 'application/json':
                    $this->HeaderCollection->set('content-type', 'application/json');
                    break;

                case 'text/html':
                    $this->HeaderCollection->set('content-type', 'text/html; charset="'.$this->getCharset().'"');
                    break;
                
                case 'text/plain':
                    $this->HeaderCollection->set('content-type', 'text/plain; charset="'.$this->getCharset().'"');
                    break;
            }
        }

        foreach ($this->HeaderCollection as $name => $value) {
            header($name.': '.$value, false);
        }
    }
    
    /**
     * @return Http\HeaderCollection
     */
    public function getHeaderCollection()
    {
        return $this->HeaderCollection;
    }

    public function toHtml()
    {
        $this->setContentType('text/html');
        return is_array($this->data) ? implode('', $this->data) : $this->data;
    }

    public function toJson($root='data')
    {
        $this->setContentType('application/json');
        return json_encode([$root=>$this->data]);
    }
    
    public function toText()
    {
        $this->setContentType('text/plain');
        return (string) $this->data;
    }
    
    public function send()
    {
        $this->sendHeaders();
    }

}<commit_msg>added check for headers sent<commit_after><?php
/**
 * This file is part of the Everon framework.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Everon;

class Response implements Interfaces\Response
{
    protected $data = null;
    protected $HeaderCollection = null;
    protected $content_type = 'text/html';
    protected $charset = 'utf-8';
    protected $status = 200;
    protected $result = false;

    
    public function __construct(Interfaces\Collection $Headers)
    {
        $this->HeaderCollection = $Headers;
    }
    
    public function setData($data)
    {
        $this->data = $data;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getContentType()
    {
        return $this->content_type;
    }

    public function setContentType($content_type)
    {
        $this->content_type = strtolower($content_type);
    }
    
    public function getCharset()
    {
        return $this->charset;
    }

    public function setCharset($charset)
    {
        $this->charset = $charset;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($status)
    {
        $this->status = (int) $status;
    }
    
    public function getResult()
    {
        return $this->result;
    }

    public function setResult($result)
    {
        $this->result = (bool) $result;
    }
    
    protected function sendHeaders()
    {
        if ($this->HeaderCollection->has('content-type') === false) {
            switch ($this->getContentType()) {
                case 'application/json':
                    $this->HeaderCollection->set('content-type', 'application/json');
                    break;

                case 'text/html':
                    $this->HeaderCollection->set('content-type', 'text/html; charset="'.$this->getCharset().'"');
                    break;
                
                case 'text/plain':
                    $this->HeaderCollection->set('content-type', 'text/plain; charset="'.$this->getCharset().'"');
                    break;
            }
        }

        foreach ($this->HeaderCollection as $name => $value) {
            header($name.': '.$value, false);
        }
    }
    
    /**
     * @return Http\HeaderCollection
     */
    public function getHeaderCollection()
    {
        return $this->HeaderCollection;
    }

    public function toHtml()
    {
        $this->setContentType('text/html');
        return is_array($this->data) ? implode('', $this->data) : $this->data;
    }

    public function toJson($root='data')
    {
        $this->setContentType('application/json');
        return json_encode([$root=>$this->data]);
    }
    
    public function toText()
    {
        $this->setContentType('text/plain');
        return (string) $this->data;
    }
    
    public function send()
    {
        if (headers_sent() === false) {
            $this->sendHeaders();
        }
    }

}
{'fraction_non_alphanumeric': 0.13593980042532308, 'fraction_numerical': 0.0013086864060199575, 'mean_word_length': 1.957910014513788, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 79, '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': '3271053', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1684, 'n_words': 365}