SimpleXMLElement::addCDataChild()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Ratepay\Business\Api;
9
10
use DOMDocument;
11
use SimpleXMLElement as SimpleXMLElementSimpleXMLElement;
12
13
class SimpleXMLElement extends SimpleXMLElementSimpleXMLElement
14
{
15
    /**
16
     * @param string $sName
17
     * @param string $sValue
18
     *
19
     * @return \SimpleXMLElement|bool
20
     */
21
    public function addCDataChild($sName, $sValue)
22
    {
23
        $dom = new DOMDocument();
24
        $cDataNode = $dom->appendChild($dom->createElement($sName));
25
        $cDataNode->appendChild($dom->createCDATASection($this->removeSpecialChars($sValue)));
26
        $oNodeOld = dom_import_simplexml($this);
27
        $oNodeTarget = $oNodeOld->ownerDocument->importNode($cDataNode, true);
28
        $oNodeOld->appendChild($oNodeTarget);
29
30
        return simplexml_import_dom($oNodeTarget, 'SimpleXMLElement');
31
    }
32
33
    /**
34
     * This method replaced all zoot signs
35
     *
36
     * @param string $str
37
     *
38
     * @return string
39
     */
40
    protected function removeSpecialChars($str)
41
    {
42
        $search = [
43
            "–" => "-",
44
            "´" => "'",
45
            "‹" => "<",
46
            "›" => ">",
47
            "‘" => "'",
48
            "’" => "'",
49
            "‚" => ",",
50
            "“" => '"',
51
            "”" => '"',
52
            "„" => '"',
53
            "‟" => '"',
54
            "•" => "-",
55
            "‒" => "-",
56
            "―" => "-",
57
            "—" => "-",
58
            "™" => "TM",
59
            "¼" => "1/4",
60
            "½" => "1/2",
61
            "¾" => "3/4",
62
        ];
63
64
        return str_replace(array_keys($search), array_values($search), $str);
65
    }
66
}
67