Passed
Push — master ( a7f2e0...7bcc2b )
by Tim
02:25
created

KeyValue::getRSAKeyValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\SchemaViolationException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\SchemaValidatableElementInterface;
15
use SimpleSAML\XML\SchemaValidatableElementTrait;
16
use SimpleSAML\XML\SerializableElementInterface;
17
use SimpleSAML\XML\XsNamespace as NS;
18
use SimpleSAML\XMLSecurity\Constants as C;
19
use SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue;
20
21
use function array_merge;
22
use function array_pop;
23
24
/**
25
 * Class representing a ds:KeyValue element.
26
 *
27
 * @package simplesamlphp/xml-security
28
 */
29
final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
30
{
31
    // We use our own getter instead of the trait's one, so we prevent their use by marking them private
32
    use ExtendableElementTrait {
33
        getElements as private;
34
        setElements as private;
35
    }
36
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\KeyValue: $message, $line
Loading history...
37
38
39
    /** The namespace-attribute for the xs:any element */
40
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
41
42
43
    /**
44
     * Initialize an KeyValue.
45
     *
46
     * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
47
     */
48
    final public function __construct(
49
        protected RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface $keyValue,
50
    ) {
51
        if (
52
            !($keyValue instanceof RSAKeyValue
53
            || $keyValue instanceof DSAKeyValue
54
            || $keyValue instanceof ECKeyValue)
55
        ) {
56
            Assert::true(
57
                (($keyValue instanceof Chunk) ? $keyValue->getNamespaceURI() : $keyValue::getNameSpaceURI())
58
                !== C::NS_XDSIG,
59
                'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue '
60
                . 'or an element in namespace ##other',
61
                SchemaViolationException::class,
62
            );
63
        }
64
    }
65
66
67
    /**
68
     * Collect the value of the RSAKeyValue-property
69
     *
70
     * @return (\SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue| at position 3 could not be parsed: the token is null at position 3.
Loading history...
71
     *         \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
72
     *         \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
73
     *         \SimpleSAML\XML\SerializableElementInterface)
74
     */
75
    public function getKeyValue(): RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface
76
    {
77
        return $this->keyValue;
78
    }
79
80
81
    /**
82
     * Convert XML into a KeyValue
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, 'KeyValue', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class);
94
95
        $keyValue = array_merge(
96
            RSAKeyValue::getChildrenOfClass($xml),
97
            DSAKeyValue::getChildrenOfClass($xml),
98
            self::getChildElementsFromXML($xml),
99
        );
100
101
        Assert::count(
102
            $keyValue,
103
            1,
104
            'A <ds:KeyValue> must contain exactly one child element',
105
            TooManyElementsException::class,
106
        );
107
108
        return new static(array_pop($keyValue));
109
    }
110
111
112
    /**
113
     * Convert this KeyValue element to XML.
114
     *
115
     * @param \DOMElement|null $parent The element we should append this KeyValue element to.
116
     * @return \DOMElement
117
     */
118
    public function toXML(?DOMElement $parent = null): DOMElement
119
    {
120
        $e = $this->instantiateParentElement($parent);
121
122
        $this->getKeyValue()->toXML($e);
123
124
        return $e;
125
    }
126
}
127