XmlRenderer   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 133
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A xml2xhtml() 0 11 2
A registerRule() 0 10 2
C render() 0 36 7
A firePreSaveEvent() 0 17 3
1
<?php
2
3
namespace Kaloa\Renderer;
4
5
use DOMDocument;
6
use Kaloa\Renderer\Xml\Rule\AbstractRule;
7
8
/**
9
 *
10
 */
11
final class XmlRenderer implements RendererInterface
12
{
13
    private $rules = array();
14
15
//    /**
16
//     * Converts all HTML entities outside of CDATA elements to their corresponding
17
//     * UTF-8 characters
18
//     *
19
//     * @param string $xmlString
20
//     * @return string
21
//     */
22
//    private function decodeEntitiesFromXml($xmlString)
23
//    {
24
//        $retVal = '';
25
//
26
//        $parts = preg_split(
27
//            '/(<!\[CDATA\[.*?\]\]>)/s',
28
//            $xmlString,
29
//            -1,
30
//            PREG_SPLIT_DELIM_CAPTURE
31
//        );
32
//
33
//        foreach ($parts as $part) {
34
//            if (strpos($part, '<![CDATA[') === 0) {
35
//                $retVal .= $part;
36
//            } else {
37
//                $retVal .= html_entity_decode($part, ENT_QUOTES, 'UTF-8');
38
//            }
39
//        }
40
//
41
//        return $retVal;
42
//    }
43
44
    /**
45
     *
46
     * http://www.php.net/manual/en/domdocument.savexml.php#95252
47
     *
48
     * @param string $xml
49
     * @return string
50
     */
51
    private function xml2xhtml($xml)
52
    {
53 1
        return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', function ($m) {
54
            $xhtml_tags = array(
55 1
                'br', 'hr', 'input', 'frame', 'img', 'area', 'link', 'col',
56 1
                'base', 'basefont', 'param'
57 1
            );
58
59 1
            return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]></$m[1]>";
60 1
        }, $xml);
61
    }
62
63
    /**
64
     *
65
     * @param AbstractRule $rule
66
     * @param int $weight
67
     */
68 1
    public function registerRule(AbstractRule $rule, $weight = 0)
69
    {
70 1
        if (!isset($this->rules[$weight])) {
71 1
            $this->rules[$weight] = array();
72 1
        }
73
74 1
        $this->rules[$weight][] = $rule;
75
76 1
        krsort($this->rules);
77 1
    }
78
79
    /**
80
     *
81
     * @param string $xmlCode
82
     * @return string
83
     */
84 1
    public function render($xmlCode)
85
    {
86 1
        $xmlCode = '<root xmlns:k="lalalala">' . $xmlCode . '</root>';
87
88 1
        $xmldoc = new DOMDocument('1.0', 'UTF-8');
89 1
        $xmldoc->resolveExternals = true;
90
91 1
        $xmldoc->loadXML($xmlCode);
92
93 1
        foreach ($this->rules as $rulesArray) {
94 1
            foreach ($rulesArray as $rule) {
95 1
                $rule->setDocument($xmldoc);
96 1
                $rule->init();
97 1
                $rule->preRender();
98 1
            }
99 1
        }
100
101 1
        foreach ($this->rules as $rulesArray) {
102 1
            foreach ($rulesArray as $rule) {
103 1
                $rule->render();
104 1
            }
105 1
        }
106
107 1
        foreach ($this->rules as $rulesArray) {
108 1
            foreach ($rulesArray as $rule) {
109 1
                $rule->postRender();
110 1
            }
111 1
        }
112
113 1
        $s = preg_replace('!<root[^>]*>(.*)</root>!s', '$1', $xmldoc->saveXML($xmldoc->documentElement));
114
115
        // Handle self-closing tags
116 1
        $s = $this->xml2xhtml($s);
117
118 1
        return $s;
119
    }
120
121
    /**
122
     *
123
     * @param string $xmlCode
124
     * @return string
125
     */
126 1
    public function firePreSaveEvent($xmlCode)
127
    {
128 1
        $xmlCode = '<root xmlns:k="lalalala">' . $xmlCode . '</root>';
129
130 1
        $xmldoc = new DOMDocument('1.0', 'UTF-8');
131 1
        $xmldoc->loadXML($xmlCode);
132
133 1
        foreach ($this->rules as $rulesArray) {
134 1
            foreach ($rulesArray as $rule) {
135 1
                $rule->setDocument($xmldoc);
136 1
                $rule->init();
137 1
                $rule->preSave();
138 1
            }
139 1
        }
140
141 1
        return preg_replace('!<root[^>]*>(.*)</root>!s', '$1', $xmldoc->saveXML($xmldoc->documentElement));
142
    }
143
}
144