Passed
Push — master ( 9afd28...a5eb5d )
by Tim
01:39
created

NotUnderstood::setContentNamespaceUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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
    /**
22
     * Initialize a env:NotUnderstood
23
     *
24
     * @param string $qname
25
     * @param string|null $namespaceUri
26
     */
27
    public function __construct(
28
        protected string $qname,
29
        protected ?string $namespaceUri = null
30
    ) {
31
        Assert::validQName($qname);
32
        Assert::nullOrValidURI($namespaceUri, SchemaViolationException::class);
33
    }
34
35
36
    /**
37
     * @return string
38
     */
39
    public function getQName(): string
40
    {
41
        return $this->qname;
42
    }
43
44
45
    /**
46
     * Get the namespace URI.
47
     *
48
     * @return string|null
49
     */
50
    public function getContentNamespaceUri(): ?string
51
    {
52
        return $this->namespaceUri;
53
    }
54
55
56
57
    /**
58
     * Splits a QName into an array holding the prefix (or null if no prefix is available) and the localName
59
     *
60
     * @param string $qName  The qualified name
61
     * @psalm-return array{null|string, string}
62
     * @return array
63
     */
64
    private static function parseQName(string $qName): array
65
    {
66
        Assert::validQName($qName);
67
68
        @list($prefix, $localName) = preg_split('/:/', $qName, 2);
69
        if ($localName === null) {
70
            $prefix = null;
71
            $localName = $qName;
72
        }
73
74
        Assert::nullOrValidNCName($prefix);
75
        Assert::validNCName($localName);
76
77
        return [$prefix, $localName];
78
    }
79
80
81
    /**
82
     * Convert XML into a NotUnderstood
83
     *
84
     * @param \DOMElement $xml The XML element we should load
85
     * @return static
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, 'NotUnderstood', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, NotUnderstood::NS, InvalidDOMElementException::class);
94
95
        $qname = self::getAttribute($xml, 'qname');
96
97
        list($prefix, $localName) = self::parseQName($qname);
98
        /** @psalm-suppress PossiblyNullArgument */
99
        $namespace = $xml->lookupNamespaceUri($prefix);
100
101
        return new static($qname, $namespace);
102
    }
103
104
105
106
    /**
107
     * Convert this element to XML.
108
     *
109
     * @param \DOMElement|null $parent The element we should append this element to.
110
     * @return \DOMElement
111
     */
112
    public function toXML(DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
116
        list($prefix, $localName) = self::parseQName($this->getQName());
117
        $namespaceUri = $this->getContentNamespaceUri();
118
        /** @psalm-suppress RedundantConditionGivenDocblockType */
119
        if ($namespaceUri !== null && $prefix !== null) {
120
            /** @psalm-suppress TypeDoesNotContainNull */
121
            if ($e->lookupNamespaceUri($prefix) === null && $e->lookupPrefix($namespaceUri) === null) {
122
                // The namespace is not yet available in the document - insert it
123
                $e->setAttribute('xmlns:' . $prefix, $namespaceUri);
124
            }
125
        }
126
127
        $e->setAttribute('qname', ($prefix === null) ? $localName : ($prefix . ':' . $localName));
128
129
        return $e;
130
    }
131
}
132