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

        if ($this->headers->has('Content-Type')) {
            $contentType = $this->headers->get('Content-Type');
            if (stripos($contentType, 'text/') === 0) {
                $matches = [];
                if (preg_match('/(?P<contenttype>.*); ?charset[^;]+(?P<extra>;.*)?/iu', $contentType, $matches)) {
                    $contentType = $matches['contenttype'];
                }
                $contentType .= '; charset=' . $this->charset . (isset($matches['extra']) ? $matches['extra'] : '');
                $this->setHeader('Content-Type', $contentType, true);
            }
        }
        return $this;
    }