AbstractDHKeyValueType::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 6
dl 0
loc 18
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc;
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\SchemaViolationException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
16
use function array_pop;
17
18
/**
19
 * A class implementing the xenc:AbstractDHKeyValueType element.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
abstract class AbstractDHKeyValueType extends AbstractXencElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\X...\AbstractDHKeyValueType: $message, $line
Loading history...
26
27
    /**
28
     * DHKeyValueType constructor.
29
     *
30
     * @param \SimpleSAML\XMLSecurity\XML\xenc\XencPublic $xencPublic
31
     * @param \SimpleSAML\XMLSecurity\XML\xenc\P|null $p
32
     * @param \SimpleSAML\XMLSecurity\XML\xenc\Q|null $q
33
     * @param \SimpleSAML\XMLSecurity\XML\xenc\Generator|null $generator
34
     * @param \SimpleSAML\XMLSecurity\XML\xenc\Seed|null $seed
35
     * @param \SimpleSAML\XMLSecurity\XML\xenc\PgenCounter|null $pgenCounter
36
     */
37
    final public function __construct(
38
        protected XencPublic $xencPublic,
39
        protected ?P $p = null,
40
        protected ?Q $q = null,
41
        protected ?Generator $generator = null,
42
        protected ?Seed $seed = null,
43
        protected ?PgenCounter $pgenCounter = null,
44
    ) {
45
        if ($p !== null || $q !== null || $generator !== null) {
46
            Assert::allNotNull([$p, $q, $generator], SchemaViolationException::class);
47
        } else {
48
            Assert::allNull([$p, $q, $generator], SchemaViolationException::class);
49
        }
50
51
        if ($seed !== null || $pgenCounter !== null) {
52
            Assert::allNotNull([$seed, $pgenCounter], SchemaViolationException::class);
53
        } else {
54
            Assert::allNull([$seed, $pgenCounter], SchemaViolationException::class);
55
        }
56
    }
57
58
59
    /**
60
     * Get the Public.
61
     *
62
     * @return \SimpleSAML\XMLSecurity\XML\xenc\XencPublic
63
     */
64
    public function getPublic(): XencPublic
65
    {
66
        return $this->xencPublic;
67
    }
68
69
70
    /**
71
     * Get the P.
72
     *
73
     * @return \SimpleSAML\XMLSecurity\XML\xenc\P|null
74
     */
75
    public function getP(): ?P
76
    {
77
        return $this->p;
78
    }
79
80
81
    /**
82
     * Get the Q.
83
     *
84
     * @return \SimpleSAML\XMLSecurity\XML\xenc\Q|null
85
     */
86
    public function getQ(): ?Q
87
    {
88
        return $this->q;
89
    }
90
91
92
    /**
93
     * Get the Generator.
94
     *
95
     * @return \SimpleSAML\XMLSecurity\XML\xenc\Generator|null
96
     */
97
    public function getGenerator(): ?Generator
98
    {
99
        return $this->generator;
100
    }
101
102
103
    /**
104
     * Get the Seed.
105
     *
106
     * @return \SimpleSAML\XMLSecurity\XML\xenc\Seed|null
107
     */
108
    public function getSeed(): ?Seed
109
    {
110
        return $this->seed;
111
    }
112
113
114
    /**
115
     * Get the PgenCounter.
116
     *
117
     * @return \SimpleSAML\XMLSecurity\XML\xenc\PgenCounter|null
118
     */
119
    public function getPgenCounter(): ?PgenCounter
120
    {
121
        return $this->pgenCounter;
122
    }
123
124
125
    /**
126
     * Initialize an DHKeyValue object from an existing XML.
127
     *
128
     * @param \DOMElement $xml
129
     * @return static
130
     *
131
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
132
     *   if the qualified name of the supplied element is wrong
133
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
134
     *   if the supplied element is missing one of the mandatory attributes
135
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
136
     *   if too many child-elements of a type are specified
137
     */
138
    public static function fromXML(DOMElement $xml): static
139
    {
140
        Assert::same($xml->localName, 'DHKeyValue', InvalidDOMElementException::class);
141
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
142
143
        $xencPublic = XencPublic::getChildrenOfClass($xml);
144
        Assert::minCount($xencPublic, 1, MissingElementException::class);
145
        Assert::maxCount($xencPublic, 1, TooManyElementsException::class);
146
147
        $p = P::getChildrenOfClass($xml);
148
        Assert::maxCount($p, 1, TooManyElementsException::class);
149
150
        $q = Q::getChildrenOfClass($xml);
151
        Assert::maxCount($q, 1, TooManyElementsException::class);
152
153
        $generator = Generator::getChildrenOfClass($xml);
154
        Assert::maxCount($generator, 1, TooManyElementsException::class);
155
156
        $seed = Seed::getChildrenOfClass($xml);
157
        Assert::maxCount($seed, 1, TooManyElementsException::class);
158
159
        $pgenCounter = PgenCounter::getChildrenOfClass($xml);
160
        Assert::maxCount($pgenCounter, 1, TooManyElementsException::class);
161
162
        return new static(
163
            array_pop($xencPublic),
164
            array_pop($p),
165
            array_pop($q),
166
            array_pop($generator),
167
            array_pop($seed),
168
            array_pop($pgenCounter),
169
        );
170
    }
171
172
173
    /**
174
     * Convert this DHKeyValue object to XML.
175
     *
176
     * @param \DOMElement|null $parent The element we should append this DHKeyValue to.
177
     * @return \DOMElement
178
     */
179
    public function toXML(?DOMElement $parent = null): DOMElement
180
    {
181
        $e = $this->instantiateParentElement($parent);
182
183
        $this->getP()?->toXML($e);
184
        $this->getQ()?->toXML($e);
185
        $this->getGenerator()?->toXML($e);
186
        $this->getPublic()->toXML($e);
187
        $this->getSeed()?->toXML($e);
188
        $this->getPgenCounter()?->toXML($e);
189
190
        return $e;
191
    }
192
}
193