1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 
<?php
    namespace Inteve\FeedGenerator\Utils;
    use Inteve\FeedGenerator\AssertException;
    use Inteve\FeedGenerator\IOutput;
    use Inteve\FeedGenerator\StaticClassException;
    class Helpers
    {
        public function __construct()
        {
            throw new StaticClassException('This is static class.');
        }
        
        public static function assert($value, $msg = NULL)
        {
            if ($value !== TRUE) {
                throw new AssertException($msg !== NULL ? $msg : "Assertion failed.");
            }
        }
        
        public static function escapeXml($s)
        {
            return htmlspecialchars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
        }
        
        public static function escapeUrl($s)
        {
            return rawurlencode($s);
        }
        
        public static function writeXml(IOutput $output, array $xml)
        {
            foreach ($xml as $tagName => $content) {
                if ($content === NULL || (is_array($content) && array_key_exists('content', $content) && $content['content'] === NULL)) {
                    continue;
                }
                $tag = $tagName;
                if (is_array($content) && isset($content['tag'])) {
                    $tag = $content['tag'];
                }
                
                $output->output('<' . $tag);
                if (is_array($content) && isset($content['attrs'])) {
                    foreach ($content['attrs'] as $attrName => $attrValue) {
                        if ($attrValue === NULL) {
                            continue;
                        }
                        $output->output(' ' . $attrName . '="' . self::escapeXml($attrValue) . '"');
                    }
                }
                $isEmpty = is_array($content) && !isset($content['content']);
                if ($isEmpty) {
                    $output->output(' /');
                }
                $output->output('>');
                if (!$isEmpty) {
                    
                    if (is_array($content) && isset($content['content'])) {
                        if (is_array($content['content'])) {
                            $output->output("\n");
                            self::writeXml($output, $content['content']);
                        } else {
                            $output->output(self::escapeXml($content['content']));
                        }
                    } else {
                        $output->output(self::escapeXml($content));
                    }
                    
                    $output->output('</' . $tag . ">");
                }
                $output->output("\n");
            }
        }
    }