Encodes the content based on http accept values, more on
this on the getHttpAccept function.
Will simply implode the return value if array and not xml or
json is prefered return type.

@param mixed $ret
@param string $type
@return string
170651
public static function autoEncode( $ret, $type = null )
    {
        if ( $type === null )
            $type = self::getHttpAccept( );

        if ( $type === 'xml' )
            return self::xmlEncode( $ret );
        else if ( $type === 'json' )
            return json_encode( $ret );
        else
            return self::textEncode( $ret );
    }
245663
public function getInlineParts($type = 'text')
    {
        $inline_parts = [];
        $mime_types = [
            'text'         => 'text/plain',
            'html'         => 'text/html',
        ];

        if (!in_array($type, array_keys($mime_types))) {
            throw new Exception('Invalid type specified for getInlineParts(). "type" can either be text or html.');
        }

        foreach ($this->parts as $partId => $part) {
            if ($this->getPart('content-type', $part) == $mime_types[$type]
                && $this->getPart('content-disposition', $part) != 'attachment'
                && !$this->partIdIsChildOfAnAttachment($partId)
            ) {
                $headers = $this->getPart('headers', $part);
                $encodingType = array_key_exists('content-transfer-encoding', $headers) ?
                    $headers['content-transfer-encoding'] : '';
                $undecoded_body = $this->decodeContentTransfer($this->getPartBody($part), $encodingType);
                $inline_parts[] = $this->charset->decodeCharset($undecoded_body, $this->getPartCharset($part));
            }
        }

        return $inline_parts;
    }
452250
public static function doctype(string $type = 'html5')
    {
        $doctypes = ['xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
                  'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
                  'xhtml1-trans'  => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
                  'xhtml1-frame'  => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
                  'html5'	  => '<!DOCTYPE html>',
                  'html4-strict'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
                  'html4-trans'	  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
                  'html4-frame'	  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'];

        if (isset($doctypes[$type])) return $doctypes[$type]; else return false;
    }
880387
public static function input(string $type)
    {
        $self = self::$input[$type] ?? new self;

        self::$input[$type] = $self;

        switch (strtoupper($type)) {
            case 'GET':
            case 'POST':
                $self->params = filter_input_array(constant('INPUT_' . $type)) ?? [];
                break;
            case 'PUT':
            case 'DELETE':
                $self->params = self::getParsedInput();
                break;
        }

        return function ($key = null) use ($self) {
            $self->key = $key;

            return $self;
        };
    }
456644
public function results($type = 'array')
    {
        switch ($type) {
            case 'array':
                // return array data
                return $this->results;
                break;
            case 'xml':
                //send the xml header to the browser
                header('Content-Type:text/xml');
                // return xml content
                return $this->helper()->arrayToXml($this->results);
                break;
            case 'json':
                // set header as json
                header('Content-type: application/json; charset="utf-8"');
                // return json encoded data
                return json_encode($this->results);
                break;
        }
    }
1136291
public function json($data, $type = 'json') {
        switch (strtolower($type)) {
            case 'xml':
                return Factory::response()->xml($data);
            case 'jsonp':
                return Factory::response()->jsonp($data);
        }
        return Factory::response()->json($data);
    }
112491
public static function encode(String $string, String $type = 'quotes', String $encoding = 'utf-8') : String
    {
        return htmlspecialchars(trim($string), Helper::toConstant($type, 'ENT_'), $encoding);
    }
965450
protected function parseRequestContent($content, $type)
    {
        $content = json_decode($content, true);
        if (empty($content['data'])) {
            throw new Exception(
                'Payload either contains misformed JSON or missing "data" parameter.',
                static::ERROR_SCOPE | static::ERROR_INVALID_ATTRS,
                BaseResponse::HTTP_BAD_REQUEST
            );
        }

        $data = $content['data'];
        if (!isset($data['type'])) {
            throw new Exception(
                '"type" parameter not set in request.',
                static::ERROR_SCOPE | static::ERROR_INVALID_ATTRS,
                BaseResponse::HTTP_BAD_REQUEST
            );
        }
        if ($data['type'] !== $type) {
            throw new Exception(
                '"type" parameter is not valid. Expecting ' . $type,
                static::ERROR_SCOPE | static::ERROR_INVALID_ATTRS,
                BaseResponse::HTTP_CONFLICT
            );
        }
        unset($data['type']);

        return $data;
    }
1071557
public function contentType($type, $set = false) {
        $content_type = self::showContentType($type);

        if ($content_type && $set) {
            $this->_response->header('Content-Type', $content_type);
        }
        return $content_type;
    }
312468
public static function getTypeFromExtension(
        string $ext,
        string $default = "application/octet-stream"
    ): string
    {
        $ext = strtolower($ext);
        return self::$extensions[$ext] ?? $default;
    }
154988
protected function _format($type) {
        $response = array(
            'success' => $this->_success,
            'data' => $this->_data
        );

        if ($this->_code) {
            $response['code'] = $this->_code;
        }

        switch (strtolower($type)) {
            case 'json':
                $format = TypeConverter::toJson($response);
            break;
            case 'xml':
                $format = TypeConverter::toXml($response);
            break;
            case 'html';
            case 'text':
            default:
                $format = (string) $this->_data;
            break;
        }

        return $format;
    }
996442
public function setEncodingType($type)
    {
        if ($type === false || $type === self::RFC1738 || $type === self::RFC3986) {
            $this->encoding = $type;
        } else {
            throw new \InvalidArgumentException('Invalid URL encoding type');
        }

        return $this;
    }
239637
public function setContent($data, $type = DataFormats::PHP_ARRAY)
    {
        $this->content = $data;

        switch ($type) {
            case DataFormats::PHP_ARRAY:
                $this->contentAsArray = $data;
                $this->contentType = ''; // this could be null, but may cause issues with clients
                break;
            case DataFormats::JSON:
                $this->contentAsArray = json_decode($data, true);
                $this->contentType = DataFormats::toMimeType($type);
                break;
        }

        return $this;
    }
1107983
public function getHtml($type = self::NO_SINGLE_RENDER_TYPE) {
        $this->_singleRenderType = $type;
        return $this->_handleDirectCall('getHtml')->toHtml();
    }
329615
static function decodeDataTypes( $node, $type = "" )
    {
        $returnValue = false;

        $attributeValue = '';
        $attribute = $node->getAttributeNodeNS( eZSOAPEnvelope::SCHEMA_INSTANCE, 'type' );
        if ( !$attribute )
        {
            $attribute = $node->getAttributeNodeNS( 'http://www.w3.org/1999/XMLSchema-instance', 'type' );
        }
        $attributeValue = $attribute->value;

        $dataType = $type;
        $attrParts = explode( ":", $attributeValue );
        if ( $attrParts[1] )
        {
            $dataType = $attrParts[1];
        }

/*
        $typeNamespacePrefix = $this->DOMDocument->namespaceByAlias( $attrParts[0] );

        check that this is a namespace type definition
                if ( ( $typeNamespacePrefix == eZSOAPEnvelope::SCHEMA_DATA ) ||
                     ( $typeNamespacePrefix == eZSOAPEnvelope::ENC )
                     )
TODO: add encoding checks with schema validation.
*/

        switch ( $dataType )
        {
            case "string" :
            case "int" :
            case "float" :
            case 'double' :
            {
                $returnValue = $node->textContent;
            } break;

            case "boolean" :
            {
                if ( $node->textContent == "true" )
                    $returnValue = true;
                else
                    $returnValue = false;
            } break;

            case "base64" :
            {
                $returnValue = base64_decode( $node->textContent );
            } break;

            case "Array" :
            {
                // Get array type
                $arrayType = $node->getAttributeNodeNS( eZSOAPEnvelope::ENC, 'arrayType' )->value;
                $arrayTypeParts = explode( ":", $arrayType );

                preg_match( "#(.*)\[(.*)\]#",  $arrayTypeParts[1], $matches );

                $type = $matches[1];
                $count = $matches[2];

                $returnValue = array();
                foreach( $node->childNodes as $child )
                {
                    if ( $child instanceof DOMElement )
                    {
                        $returnValue[] = eZSOAPResponse::decodeDataTypes( $child, $type );
                    }
                }
            }break;

            case "SOAPStruct" :
            {
                $returnValue = array();

                foreach( $node->childNodes as $child )
                {
                    if ( $child instanceof DOMElement )
                    {
                        $returnValue[$child->tagName] = eZSOAPResponse::decodeDataTypes( $child );
                    }
                }
            }break;

            default:
            {
                foreach ( $node->childNodes as $childNode )
                {
                    if ( $childNode instanceof DOMElement )
                    {
                        // check data type for child
                        $attr = $childNode->getAttributeNodeNS( eZSOAPEnvelope::SCHEMA_INSTANCE, 'type' )->value;

                        $dataType = false;
                        $attrParts = explode( ":", $attr );
                        $dataType = $attrParts[1];

                        $returnValue[$childNode->tagName] = eZSOAPResponse::decodeDataTypes( $childNode );
                    }
                }

            } break;
        }

        return $returnValue;
    }
1322023
public static function encodeArray(array $array, string $type = null): array
    {
        foreach ($array as &$v) {
            $v = self::run(self::ENCODER, $v, $type);
        }

        return $array;
    }
666077
public static function validateRequest($type)
    {
        $hasValidParams = isset(
            $_POST['id'],
            $_POST['app'],
            $_POST['remote'],
            $_POST['request'],
            $_POST['complement'],
            $_POST['nonce'],
            $_POST['filter'],
            $_POST['sort'],
            $_SESSION['efc'],
            $_SERVER['HTTP_X_REQUESTED_WITH']
        );

        if (! $hasValidParams || ! self::sanitizeParams()) {
            return false;
        }

        if (self::$config['complement'] !== $type) {
            return false;
        }

        $isAjaxRequest = $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';

        $isValidNonce = self::$config['nonce'] === $_SESSION['efc'];

        if (! $isAjaxRequest || ! $isValidNonce) {
            return false;
        }

        return true;
    }