KeySize::getKeySize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\SchemaViolationException;
11
12
/**
13
 * Class representing a xenc:KeySize element.
14
 *
15
 * @package simplesaml/xml-security
16
 */
17
final class KeySize extends AbstractXencElement
18
{
19
    /**
20
     * @param int $keySize
21
     */
22
    public function __construct(
23
        protected int $keySize,
24
    ) {
25
        Assert::positiveInteger($keySize, SchemaViolationException::class);
26
    }
27
28
29
    /**
30
     * @return int
31
     */
32
    public function getKeySize(): int
33
    {
34
        return $this->keySize;
35
    }
36
37
38
    /**
39
     * Convert XML into a class instance
40
     *
41
     * @param \DOMElement $xml The XML element we should load
42
     * @return static
43
     *
44
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
45
     *   If the qualified name of the supplied element is wrong
46
     */
47
    public static function fromXML(DOMElement $xml): static
48
    {
49
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
50
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
51
        Assert::numeric($xml->textContent);
52
53
        return new static(intval($xml->textContent));
54
    }
55
56
57
    /**
58
     * Convert this element to XML.
59
     *
60
     * @param \DOMElement|null $parent The element we should append this element to.
61
     * @return \DOMElement
62
     */
63
    public function toXML(?DOMElement $parent = null): DOMElement
64
    {
65
        $e = $this->instantiateParentElement($parent);
66
        $e->textContent = strval($this->getKeySize());
67
68
        return $e;
69
    }
70
}
71