Passed
Push — master ( c93035...6d9219 )
by Tim
12:59 queued 11:11
created

NotUnderstood::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP\XML\env_200305;
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
        /** @var string|null $localName */
70
        if ($localName === null) {
71
            $prefix = null;
72
            $localName = $qName;
73
        }
74
75
        Assert::nullOrValidNCName($prefix);
76
        Assert::validNCName($localName);
77
78
        return [$prefix, $localName];
79
    }
80
81
82
    /**
83
     * Convert XML into a NotUnderstood
84
     *
85
     * @param \DOMElement $xml The XML element we should load
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
89
     *   If the qualified name of the supplied element is wrong
90
     */
91
    public static function fromXML(DOMElement $xml): static
92
    {
93
        Assert::same($xml->localName, 'NotUnderstood', InvalidDOMElementException::class);
94
        Assert::same($xml->namespaceURI, NotUnderstood::NS, InvalidDOMElementException::class);
95
96
        $qname = self::getAttribute($xml, 'qname');
97
98
        list($prefix, $localName) = self::parseQName($qname);
99
        /** @psalm-suppress PossiblyNullArgument */
100
        $namespace = $xml->lookupNamespaceUri($prefix);
101
102
        return new static($qname, $namespace);
103
    }
104
105
106
107
    /**
108
     * Convert this element to XML.
109
     *
110
     * @param \DOMElement|null $parent The element we should append this element to.
111
     * @return \DOMElement
112
     */
113
    public function toXML(DOMElement $parent = null): DOMElement
114
    {
115
        $e = $this->instantiateParentElement($parent);
116
117
        list($prefix, $localName) = self::parseQName($this->getQName());
118
        $namespaceUri = $this->getContentNamespaceUri();
119
        if ($namespaceUri !== null && $prefix !== null) {
120
            /** @phpstan-ignore-next-line */
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