Passed
Push — master ( c726f3...c686fd )
by Tim
10:07
created

NotUnderstood::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP12\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 array{null|string, string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{null|string, string} at position 2 could not be parsed: Expected ':' at position 2, but found 'null'.
Loading history...
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
        /** @psalm-var string $qname */
122
        $qname = self::getAttribute($xml, 'qname');
123
124
        list($prefix, $localName) = self::parseQName($qname);
125
        /** @psalm-suppress PossiblyNullArgument */
126
        $namespace = $xml->lookupNamespaceUri($prefix);
127
128
        return new static($qname, $namespace);
129
    }
130
131
132
133
    /**
134
     * Convert this element to XML.
135
     *
136
     * @param \DOMElement|null $parent The element we should append this element to.
137
     * @return \DOMElement
138
     */
139
    public function toXML(DOMElement $parent = null): DOMElement
140
    {
141
        $e = $this->instantiateParentElement($parent);
142
143
        list($prefix, $localName) = self::parseQName($this->getQName());
144
        $namespaceUri = $this->getContentNamespaceUri();
145
        /** @psalm-suppress RedundantConditionGivenDocblockType */
146
        if ($namespaceUri !== null && $prefix !== null) {
147
            /** @psalm-suppress TypeDoesNotContainNull */
148
            if ($e->lookupNamespaceUri($prefix) === null && $e->lookupPrefix($namespaceUri) === null) {
149
                // The namespace is not yet available in the document - insert it
150
                $e->setAttribute('xmlns:' . $prefix, $namespaceUri);
151
            }
152
        }
153
154
        $e->setAttribute('qname', ($prefix === null) ? $localName : ($prefix . ':' . $localName));
155
156
        return $e;
157
    }
158
}
159