Passed
Push — master ( a7f2e0...7bcc2b )
by Tim
02:25
created

CoFactor::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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