Passed
Push — master ( 69f23d...f7c7ec )
by Tim
01:57
created

RSAKeyValue::toXML()   A

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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
14
/**
15
 * Class representing a ds:RSAKeyValue element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class RSAKeyValue extends AbstractDsElement
20
{
21
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Modulus $modulus */
22
    protected Modulus $modulus;
23
24
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Exponent $exponent */
25
    protected Exponent $exponent;
26
27
28
    /**
29
     * Initialize an RSAKeyValue.
30
     *
31
     * @param \SimpleSAML\XMLSecurity\XML\ds\Modulus $modulus
32
     * @param \SimpleSAML\XMLSecurity\XML\ds\Exponent $exponent
33
     */
34
    final public function __construct(Modulus $modulus, Exponent $exponent)
35
    {
36
        $this->setModulus($modulus);
37
        $this->setExponent($exponent);
38
    }
39
40
41
    /**
42
     * Collect the value of the modulus-property
43
     *
44
     * @return \SimpleSAML\XMLSecurity\XML\ds\Modulus
45
     */
46
    public function getModulus(): Modulus
47
    {
48
        return $this->modulus;
49
    }
50
51
52
    /**
53
     * Set the value of the modulus-property
54
     *
55
     * @param \SimpleSAML\XMLSecurity\XML\ds\Modulus $modulus
56
     */
57
    private function setModulus(Modulus $modulus): void
58
    {
59
        $this->modulus = $modulus;
60
    }
61
62
63
    /**
64
     * Collect the value of the exponent-property
65
     *
66
     * @return \SimpleSAML\XMLSecurity\XML\ds\Exponent
67
     */
68
    public function getExponent(): Exponent
69
    {
70
        return $this->exponent;
71
    }
72
73
74
    /**
75
     * Set the value of the exponent-property
76
     *
77
     * @param \SimpleSAML\XMLSecurity\XML\ds\Exponent $exponent
78
     */
79
    private function setExponent(Exponent $exponent): void
80
    {
81
        $this->exponent = $exponent;
82
    }
83
84
85
    /**
86
     * Convert XML into a RSAKeyValue
87
     *
88
     * @param \DOMElement $xml The XML element we should load
89
     * @return static
90
     *
91
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
92
     *   If the qualified name of the supplied element is wrong
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        Assert::same($xml->localName, 'RSAKeyValue', InvalidDOMElementException::class);
97
        Assert::same($xml->namespaceURI, RSAKeyValue::NS, InvalidDOMElementException::class);
98
99
        $modulus = Modulus::getChildrenOfClass($xml);
100
        Assert::minCount(
101
            $modulus,
102
            1,
103
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
104
            MissingElementException::class
105
        );
106
        Assert::maxCount(
107
            $modulus,
108
            1,
109
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
110
            TooManyElementsException::class
111
        );
112
113
        $exponent = Exponent::getChildrenOfClass($xml);
114
        Assert::minCount(
115
            $exponent,
116
            1,
117
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
118
            MissingElementException::class
119
        );
120
        Assert::maxCount(
121
            $exponent,
122
            1,
123
            'An <ds:RSAKeyValue> must contain exactly one <ds:Modulus>',
124
            TooManyElementsException::class
125
        );
126
127
        return new static(array_pop($modulus), array_pop($exponent));
128
    }
129
130
131
    /**
132
     * Convert this RSAKeyValue element to XML.
133
     *
134
     * @param \DOMElement|null $parent The element we should append this RSAKeyValue element to.
135
     * @return \DOMElement
136
     */
137
    public function toXML(DOMElement $parent = null): DOMElement
138
    {
139
        $e = $this->instantiateParentElement($parent);
140
141
        $this->modulus->toXML($e);
142
        $this->exponent->toXML($e);
143
144
        return $e;
145
    }
146
}
147