public static function encode($data, $prettyPrint = false, $xmlVersion = '1.0', $encoding = 'utf-8')
    {
        $domDocument = new DOMDocument($xmlVersion, $encoding);
        $domDocument = self::loopEncode($data, $domDocument);
        
        if ($prettyPrint) {
            $domDocument->preserveWhiteSpace = false;
            $domDocument->formatOutput = true;
        }

        $resultString = $domDocument->saveXML();

        if (!$prettyPrint) {
            $resultStringLines = explode(PHP_EOL, $resultString);
            $documentLine = array_shift($resultStringLines);

            $resultString = $documentLine . PHP_EOL . implode('', $resultStringLines);
        } else {
            $resultString = trim($resultString, PHP_EOL);
        }

        return $resultString;
    }