public function setEncoding($direction, $encoding)
    {
        if (!isset($this->encoding[$direction])) {
            throw new Exception("Encoding direction invalid");
        }

        if (!$encoding) {
            throw new Exception("Encoding value invalid");
        } elseif (preg_match('/utf.?8/ui', $encoding)) {
            $encoding = 'UTF-8';
        } else {
            $encoding = mb_strtoupper($encoding);
        }

        $this->encoding[$direction] = $encoding;

        return $this;
    }