RSAKeyValue::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
15
/**
16
 * Class representing a ds:RSAKeyValue element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class RSAKeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
21
{
22
    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\RSAKeyValue: $message, $line
Loading history...
23
24
25
    /**
26
     * Initialize an RSAKeyValue.
27
     *
28
     * @param \SimpleSAML\XMLSecurity\XML\ds\Modulus $modulus
29
     * @param \SimpleSAML\XMLSecurity\XML\ds\Exponent $exponent
30
     */
31
    final public function __construct(
32
        protected Modulus $modulus,
33
        protected Exponent $exponent,
34
    ) {
35
    }
36
37
38
    /**
39
     * Collect the value of the modulus-property
40
     *
41
     * @return \SimpleSAML\XMLSecurity\XML\ds\Modulus
42
     */
43
    public function getModulus(): Modulus
44
    {
45
        return $this->modulus;
46
    }
47
48
49
    /**
50
     * Collect the value of the exponent-property
51
     *
52
     * @return \SimpleSAML\XMLSecurity\XML\ds\Exponent
53
     */
54
    public function getExponent(): Exponent
55
    {
56
        return $this->exponent;
57
    }
58
59
60
    /**
61
     * Convert XML into a RSAKeyValue
62
     *
63
     * @param \DOMElement $xml The XML element we should load
64
     * @return static
65
     *
66
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
67
     *   If the qualified name of the supplied element is wrong
68
     */
69
    public static function fromXML(DOMElement $xml): static
70
    {
71
        Assert::same($xml->localName, 'RSAKeyValue', InvalidDOMElementException::class);
72
        Assert::same($xml->namespaceURI, RSAKeyValue::NS, InvalidDOMElementException::class);
73
74
        $modulus = Modulus::getChildrenOfClass($xml);
75
        Assert::minCount(
76
            $modulus,
77
            1,
78
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
79
            MissingElementException::class,
80
        );
81
        Assert::maxCount(
82
            $modulus,
83
            1,
84
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
85
            TooManyElementsException::class,
86
        );
87
88
        $exponent = Exponent::getChildrenOfClass($xml);
89
        Assert::minCount(
90
            $exponent,
91
            1,
92
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
93
            MissingElementException::class,
94
        );
95
        Assert::maxCount(
96
            $exponent,
97
            1,
98
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
99
            TooManyElementsException::class,
100
        );
101
102
        return new static(array_pop($modulus), array_pop($exponent));
103
    }
104
105
106
    /**
107
     * Convert this RSAKeyValue element to XML.
108
     *
109
     * @param \DOMElement|null $parent The element we should append this RSAKeyValue element to.
110
     * @return \DOMElement
111
     */
112
    public function toXML(?DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
116
        $this->getModulus()->toXML($e);
117
        $this->getExponent()->toXML($e);
118
119
        return $e;
120
    }
121
}
122