* @copyright 2014 Fabian Grutschus. All rights reserved. * @license BSD * @link http://github.com/fabiang/xmpp */ namespace Fabiang\Xmpp\Util; /** * XML utility methods. * * @package Xmpp\Util */ class XML { /** * Quote XML string. * * @param string $string String to be quoted * @param string $encoding Encoding used for quotation * @return string */ public static function quote($string, $encoding = 'UTF-8') { $flags = ENT_QUOTES; if (defined('ENT_XML1')) { $flags |= ENT_XML1; } return htmlspecialchars($string, $flags, $encoding); } /** * Replace variables in a string and quote them before. * * Hint: this function works like sprintf * * @param string $message * @param mixed $args * @param mixed $... * @return string */ public static function quoteMessage($message) { $variables = func_get_args(); // shift message variable array_shift($variables); // workaround for `static` call in a closure $class = __CLASS__; return vsprintf( $message, array_map( function ($var) use ($class) { return $class::quote($var); }, $variables ) ); } /** * Generate a unique id. * * @return string */ public static function generateId() { return static::quote('fabiang_xmpp_' . uniqid()); } /** * Encode a string with Base64 and quote it. * * @param string $data * @param string $encoding * @return string */ public static function base64Encode($data, $encoding = 'UTF-8') { return static::quote(base64_encode($data), $encoding); } /** * Decode a Base64 encoded string. * * @param string $data * @return string */ public static function base64Decode($data) { return base64_decode($data); } }