Issues (88)

src/XML/ds/RSAKeyValue.php (1 issue)

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