Passed
Push — master ( bf4de8...284c89 )
by Tim
02:01
created

NotUnderstood::getContentNamespaceUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
12
use function preg_split;
13
14
/**
15
 * Class representing a env:NotUnderstood element.
16
 *
17
 * @package simplesaml/xml-soap
18
 */
19
final class NotUnderstood extends AbstractSoapElement
20
{
21
    /** @var string $qname */
22
    protected string $qname;
23
24
    /** @var string|null */
25
    protected ?string $namespaceUri;
26
27
28
    /**
29
     * Initialize a env:NotUnderstood
30
     *
31
     * @param string $qname
32
     * @param string|null $namespaceUri
33
     */
34
    public function __construct(string $qname, ?string $namespaceUri = null)
35
    {
36
        $this->setQName($qname);
37
        $this->setContentNamespaceUri($namespaceUri);
38
    }
39
40
41
    /**
42
     * @return string
43
     */
44
    public function getQName(): string
45
    {
46
        return $this->qname;
47
    }
48
49
50
    /**
51
     * @param string $qname
52
     */
53
    private function setQName(string $qname): void
54
    {
55
        Assert::validQName($qname);
56
        $this->qname = $qname;
57
    }
58
59
60
    /**
61
     * Set the namespaceUri.
62
     *
63
     * @param string|null $namespaceUri
64
     */
65
    protected function setContentNamespaceUri(?string $namespaceUri): void
66
    {
67
        Assert::nullOrValidURI($namespaceUri, SchemaViolationException::class);
68
        $this->namespaceUri = $namespaceUri;
69
    }
70
71
72
    /**
73
     * Get the namespace URI.
74
     *
75
     * @return string|null
76
     */
77
    public function getContentNamespaceUri(): ?string
78
    {
79
        return $this->namespaceUri;
80
    }
81
82
83
84
    /**
85
     * Splits a QName into an array holding the prefix (or null if no prefix is available) and the localName
86
     *
87
     * @param string $qName  The qualified name
88
     * @return string[]
89
     */
90
    private static function parseQName(string $qName): array
91
    {
92
        Assert::validQName($qName);
93
94
        @list($prefix, $localName) = preg_split('/:/', $qName, 2);
95
        if ($localName === null) {
96
            $prefix = null;
97
            $localName = $qName;
98
        }
99
100
        Assert::nullOrValidNCName($prefix);
101
        Assert::validNCName($localName);
102
103
        return [$prefix, $localName];
104
    }
105
106
107
    /**
108
     * Convert XML into a NotUnderstood
109
     *
110
     * @param \DOMElement $xml The XML element we should load
111
     * @return static
112
     *
113
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
114
     *   If the qualified name of the supplied element is wrong
115
     */
116
    public static function fromXML(DOMElement $xml): static
117
    {
118
        Assert::same($xml->localName, 'NotUnderstood', InvalidDOMElementException::class);
119
        Assert::same($xml->namespaceURI, NotUnderstood::NS, InvalidDOMElementException::class);
120
121
        $qname = self::getAttribute($xml, 'qname');
122
123
        list($prefix, $localName) = self::parseQName($qname);
124
        if ($prefix === null) {
125
            // We don't have a prefixed value here; use target namespace
126
            $namespace = $xml->lookupNamespaceUri(null);
127
        } else {
128
            $namespace = $xml->lookupNamespaceUri($prefix);
129
        }
130
131
        return new static($qname, $namespace);
132
    }
133
134
135
136
    /**
137
     * Convert this element to XML.
138
     *
139
     * @param \DOMElement|null $parent The element we should append this element to.
140
     * @return \DOMElement
141
     */
142
    public function toXML(DOMElement $parent = null): DOMElement
143
    {
144
        $e = $this->instantiateParentElement($parent);
145
146
        list($prefix, $localName) = self::parseQName($this->qname);
147
        if ($this->namespaceUri !== null && $prefix !== null) {
148
            if ($e->lookupNamespaceUri($prefix) === null && $e->lookupPrefix($this->namespaceUri) === null) {
149
                // The namespace is not yet available in the document - insert it
150
                $e->setAttribute('xmlns:' . $prefix, $this->namespaceUri);
151
            }
152
        }
153
154
        $e->setAttribute('qname', ($prefix === null) ? $localName : ($prefix . ':' . $localName));
155
156
        return $e;
157
    }
158
}
159