Issues (81)

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