Passed
Pull Request — master (#38)
by Tim
01:51
created

KeyValue::toXML()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\SchemaViolationException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
use SimpleSAML\XML\ExtendableElementTrait;
15
use SimpleSAML\XML\ElementInterface;
16
use SimpleSAML\XMLSecurity\Constants as C;
17
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
18
19
/**
20
 * Class representing a ds:KeyValue element.
21
 *
22
 * @package simplesamlphp/xml-security
23
 */
24
final class KeyValue extends AbstractDsElement
25
{
26
    use ExtendableElementTrait;
27
28
29
    /** The namespace-attribute for the xs:any element */
30
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
31
32
    /** @var \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue */
33
    protected ?RSAKeyValue $RSAKeyValue;
34
35
    // DSA is not supported
36
    //protected ?DSAKeyValue $DSAKeyValue;
37
38
39
    /**
40
     * Initialize an KeyValue.
41
     *
42
     * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue
43
     * @param \SimpleSAML\XML\ElementInterface|null $element
44
     */
45
    final public function __construct(?RSAKeyValue $RSAKeyValue, ?ElementInterface $element = null)
46
    {
47
        Assert::false(
48
            is_null($RSAKeyValue) && is_null($element),
49
            'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other',
50
            SchemaViolationException::class,
51
        );
52
53
        $this->setRSAKeyValue($RSAKeyValue);
54
55
        if ($element !== null) {
56
            $this->setElements([$element]);
57
        }
58
    }
59
60
61
    /**
62
     * Collect the value of the RSAKeyValue-property
63
     *
64
     * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null
65
     */
66
    public function getRSAKeyValue(): ?RSAKeyValue
67
    {
68
        return $this->RSAKeyValue;
69
    }
70
71
72
    /**
73
     * Set the value of the RSAKeyValue-property
74
     *
75
     * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue $RSAKeyValue
76
     */
77
    private function setRSAKeyValue(?RSAKeyValue $RSAKeyValue): void
78
    {
79
        $this->RSAKeyValue = $RSAKeyValue;
80
    }
81
82
83
    /**
84
     * Convert XML into a KeyValue
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   If the qualified name of the supplied element is wrong
91
     */
92
    public static function fromXML(DOMElement $xml): static
93
    {
94
        Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class);
95
        Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class);
96
97
        $RSAKeyValue = RSAKeyValue::getChildrenOfClass($xml);
98
        Assert::maxCount(
99
            $RSAKeyValue,
100
            1,
101
            'A <ds:KeyValue> can contain exactly one <ds:RSAKeyValue>',
102
            TooManyElementsException::class
103
        );
104
105
        $elements = [];
106
        foreach ($xml->childNodes as $element) {
107
            if (!($element instanceof DOMElement) || $element->namespaceURI === KeyValue::NS) {
108
                continue;
109
            }
110
111
            $elements[] = new Chunk($element);
112
        }
113
        Assert::maxCount(
114
            $elements,
115
            1,
116
            'A <ds:KeyValue> can contain exactly one element in namespace ##other',
117
            TooManyElementsException::class
118
        );
119
120
        return new static(array_pop($RSAKeyValue), array_pop($elements));
121
    }
122
123
124
    /**
125
     * Convert this KeyValue element to XML.
126
     *
127
     * @param \DOMElement|null $parent The element we should append this KeyValue element to.
128
     * @return \DOMElement
129
     */
130
    public function toXML(DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
134
        if ($this->RSAKeyValue !== null) {
135
            $this->RSAKeyValue->toXML($e);
136
        }
137
138
        foreach ($this->elements as $element) {
139
            $e->appendChild($e->ownerDocument->importNode($element->toXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($element->toXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
        }
141
142
        return $e;
143
    }
144
}
145