NotUnderstood::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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