Passed
Push — master ( 633469...6fda02 )
by Tim
02:18
created

AbstractDSAKeyValueType::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 25
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 35
rs 9.52
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\SchemaViolationException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
14
use function array_pop;
15
16
/**
17
 * A class implementing the ds:AbstractDSAKeyValueType element.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractDSAKeyValueType extends AbstractDsElement
22
{
23
    /**
24
     * DSAKeyValueType constructor.
25
     *
26
     * @param \SimpleSAML\XMLSecurity\XML\ds\Y $y
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\G|null $g
28
     * @param \SimpleSAML\XMLSecurity\XML\ds\J|null $j
29
     * @param \SimpleSAML\XMLSecurity\XML\ds\P|null $p
30
     * @param \SimpleSAML\XMLSecurity\XML\ds\Q|null $q
31
     * @param \SimpleSAML\XMLSecurity\XML\ds\Generator|null $generator
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\Generator was not found. Did you mean Generator? If so, make sure to prefix the type with \.
Loading history...
32
     * @param \SimpleSAML\XMLSecurity\XML\ds\Seed|null $seed
33
     * @param \SimpleSAML\XMLSecurity\XML\ds\PgenCounter|null $pgenCounter
34
     */
35
    final public function __construct(
36
        protected Y $y,
37
        protected ?G $g = null,
38
        protected ?J $j = null,
39
        protected ?P $p = null,
40
        protected ?Q $q = null,
41
        protected ?Seed $seed = null,
42
        protected ?PgenCounter $pgenCounter = null,
43
    ) {
44
        if ($p !== null || $q !== null) {
45
            Assert::allNotNull([$p, $q], SchemaViolationException::class);
46
        } else {
47
            Assert::allNull([$p, $q], SchemaViolationException::class);
48
        }
49
50
        if ($seed !== null || $pgenCounter !== null) {
51
            Assert::allNotNull([$seed, $pgenCounter], SchemaViolationException::class);
52
        } else {
53
            Assert::allNull([$seed, $pgenCounter], SchemaViolationException::class);
54
        }
55
    }
56
57
58
    /**
59
     * Get the Y.
60
     *
61
     * @return \SimpleSAML\XMLSecurity\XML\ds\Y
62
     */
63
    public function getY(): Y
64
    {
65
        return $this->y;
66
    }
67
68
69
    /**
70
     * Get the G.
71
     *
72
     * @return \SimpleSAML\XMLSecurity\XML\ds\G
73
     */
74
    public function getG(): G
75
    {
76
        return $this->g;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->g could return the type null which is incompatible with the type-hinted return SimpleSAML\XMLSecurity\XML\ds\G. Consider adding an additional type-check to rule them out.
Loading history...
77
    }
78
79
80
    /**
81
     * Get the J.
82
     *
83
     * @return \SimpleSAML\XMLSecurity\XML\ds\J
84
     */
85
    public function getJ(): J
86
    {
87
        return $this->j;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->j could return the type null which is incompatible with the type-hinted return SimpleSAML\XMLSecurity\XML\ds\J. Consider adding an additional type-check to rule them out.
Loading history...
88
    }
89
90
91
    /**
92
     * Get the P.
93
     *
94
     * @return \SimpleSAML\XMLSecurity\XML\ds\P|null
95
     */
96
    public function getP(): ?P
97
    {
98
        return $this->p;
99
    }
100
101
102
    /**
103
     * Get the Q.
104
     *
105
     * @return \SimpleSAML\XMLSecurity\XML\ds\Q|null
106
     */
107
    public function getQ(): ?Q
108
    {
109
        return $this->q;
110
    }
111
112
113
    /**
114
     * Get the Seed.
115
     *
116
     * @return \SimpleSAML\XMLSecurity\XML\ds\Seed|null
117
     */
118
    public function getSeed(): ?Seed
119
    {
120
        return $this->seed;
121
    }
122
123
124
    /**
125
     * Get the PgenCounter.
126
     *
127
     * @return \SimpleSAML\XMLSecurity\XML\ds\PgenCounter|null
128
     */
129
    public function getPgenCounter(): ?PgenCounter
130
    {
131
        return $this->pgenCounter;
132
    }
133
134
135
    /**
136
     * Initialize an DSAKeyValue object from an existing XML.
137
     *
138
     * @param \DOMElement $xml
139
     * @return static
140
     *
141
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
142
     *   if the qualified name of the supplied element is wrong
143
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
144
     *   if the supplied element is missing one of the mandatory attributes
145
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
146
     *   if too many child-elements of a type are specified
147
     */
148
    public static function fromXML(DOMElement $xml): static
149
    {
150
        Assert::same($xml->localName, 'DSAKeyValue', InvalidDOMElementException::class);
151
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
152
153
        $y = Y::getChildrenOfClass($xml);
154
        Assert::minCount($y, 1, TooManyElementsException::class);
155
        Assert::maxCount($y, 1, TooManyElementsException::class);
156
157
        $g = G::getChildrenOfClass($xml);
158
        Assert::maxCount($g, 1, TooManyElementsException::class);
159
160
        $j = J::getChildrenOfClass($xml);
161
        Assert::maxCount($j, 1, TooManyElementsException::class);
162
163
        $p = P::getChildrenOfClass($xml);
164
        Assert::maxCount($p, 1, TooManyElementsException::class);
165
166
        $q = Q::getChildrenOfClass($xml);
167
        Assert::maxCount($q, 1, TooManyElementsException::class);
168
169
        $seed = Seed::getChildrenOfClass($xml);
170
        Assert::maxCount($seed, 1, TooManyElementsException::class);
171
172
        $pgenCounter = PgenCounter::getChildrenOfClass($xml);
173
        Assert::maxCount($pgenCounter, 1, TooManyElementsException::class);
174
175
        return new static(
176
            array_pop($y),
177
            array_pop($g),
178
            array_pop($j),
179
            array_pop($p),
180
            array_pop($q),
181
            array_pop($seed),
182
            array_pop($pgenCounter),
183
        );
184
    }
185
186
187
    /**
188
     * Convert this DSAKeyValue object to XML.
189
     *
190
     * @param \DOMElement|null $parent The element we should append this DSAKeyValue to.
191
     * @return \DOMElement
192
     */
193
    public function toXML(?DOMElement $parent = null): DOMElement
194
    {
195
        $e = $this->instantiateParentElement($parent);
196
197
        $this->getP()?->toXML($e);
198
        $this->getQ()?->toXML($e);
199
        $this->getG()?->toXML($e);
200
        $this->getY()->toXML($e);
201
        $this->getJ()?->toXML($e);
202
        $this->getSeed()?->toXML($e);
203
        $this->getPgenCounter()?->toXML($e);
204
205
        return $e;
206
    }
207
}
208