Passed
Push — master ( b4dd13...046ad9 )
by Tim
01:52
created

Text::fromXML()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 6
nop 1
dl 0
loc 22
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\SOAP\XML\env;
4
5
use DOMAttr;
6
use DOMElement;
7
use DOMNameSpaceNode;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\StringElementTrait;
11
12
/**
13
 * Class representing a env:Text element.
14
 *
15
 * @package simplesaml/xml-soap
16
 */
17
final class Text extends AbstractSoapElement
18
{
19
    use StringElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SOAP\XML\env\Text: $localName, $namespaceURI
Loading history...
20
21
    /** @var \DOMAttr|null */
22
    protected $node = null;
23
24
25
    /**
26
     * Initialize a env:Text
27
     *
28
     * @param string $content
29
     * @param DOMAttr|null $node
30
     */
31
    public function __construct(string $content, ?DOMAttr $node = null)
32
    {
33
        $this->setContent($content);
34
        $this->setNode($node);
35
    }
36
37
38
    /**
39
     * @return \DOMAttr|null
40
     */
41
    public function getNode(): ?DOMAttr
42
    {
43
        return $this->node;
44
    }
45
46
47
    /**
48
     * @param \DOMAttr|null $node
49
     */
50
    private function setNode(?DOMAttr $node): void
51
    {
52
        $this->node = $node;
53
    }
54
55
56
    /**
57
     * Validate the content of the element.
58
     *
59
     * @param string $content  The value to go in the XML textContent
60
     * @throws \SimpleSAML\Assert\AssertionFailedException on failure
61
     * @return void
62
     */
63
    protected function validateContent(string $content): void
64
    {
65
        Assert::notWhitespaceOnly($content);
66
    }
67
68
69
    /**
70
     * Convert this element to XML.
71
     *
72
     * @param \DOMElement|null $parent The element we should append this element to.
73
     * @return \DOMElement
74
     */
75
    public function toXML(DOMElement $parent = null): DOMElement
76
    {
77
        $e = $this->instantiateParentElement($parent);
78
        $e->textContent = $this->getContent();
79
80
        if ($this->node !== null) {
81
            if (!($e->hasAttribute($this->node->localName))) {
82
                $e->setAttributeNode($this->getNode());
83
            }
84
        }
85
86
        return $e;
87
    }
88
89
    /**
90
     * Convert XML into a Text
91
     *
92
     * @param \DOMElement $xml The XML element we should load
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
96
     *   If the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        Assert::same($xml->localName, 'Text', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, Text::NS, InvalidDOMElementException::class);
102
103
        @list($prefix, $localName) = preg_split('/:/', $xml->textContent, 2);
104
        if ($localName === null) {
105
            // We don't have a prefixed value here
106
            $prefix = null;
107
        }
108
109
        $node = null;
110
        if ($prefix !== null) {
111
            $node = $xml->ownerDocument->documentElement->getAttributeNode('xmlns:' . $prefix);
112
            if ($node !== false) {
113
                $node = new DOMAttr('xmlns:' . $prefix, $node->namespaceURI);
114
            } else {
115
                $node = null;
116
            }
117
        }
118
119
        return new static($xml->textContent, $node);
120
    }
121
}
122