AbstractDHKeyValueType::getQ()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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