Passed
Pull Request — master (#8)
by Tim
02:09
created

X509SerialNumber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSerial() 0 3 1
A setSerial() 0 3 1
A toXML() 0 8 1
A fromXML() 0 7 1
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\Constants;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
12
/**
13
 * Class representing a ds:X509SerialNumber element.
14
 *
15
 * @package simplesaml/xml-security
16
 */
17
final class X509SerialNumber extends AbstractDsElement
18
{
19
    /**
20
     * The serial number.
21
     *
22
     * @var int
23
     */
24
    protected int $serial;
25
26
27
    /**
28
     * Initialize a X509SerialNumber element.
29
     *
30
     * @param int $serial
31
     */
32
    public function __construct(int $serial)
33
    {
34
        $this->setSerial($serial);
35
    }
36
37
38
    /**
39
     * Collect the value of the serial-property
40
     *
41
     * @return int
42
     */
43
    public function getSerial(): int
44
    {
45
        return $this->serial;
46
    }
47
48
49
    /**
50
     * Set the value of the serial-property
51
     *
52
     * @param int $serial
53
     */
54
    private function setSerial(int $serial): void
55
    {
56
        $this->serial = $serial;
57
    }
58
59
60
    /**
61
     * Convert XML into a X509SerialNumber
62
     *
63
     * @param \DOMElement $xml The XML element we should load
64
     * @return self
65
     *
66
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
67
     *   If the qualified name of the supplied element is wrong
68
     */
69
    public static function fromXML(DOMElement $xml): object
70
    {
71
        Assert::same($xml->localName, 'X509SerialNumber', InvalidDOMElementException::class);
72
        Assert::same($xml->namespaceURI, X509SerialNumber::NS, InvalidDOMElementException::class);
73
        Assert::same($xml->getAttributeNS(Constants::NS_XSI, "type"), 'xs:integer');
74
75
        return new self(intval($xml->textContent));
76
    }
77
78
79
    /**
80
     * Convert this X509SerialNumber element to XML.
81
     *
82
     * @param \DOMElement|null $parent The element we should append this X509SerialNumber element to.
83
     * @return \DOMElement
84
     */
85
    public function toXML(DOMElement $parent = null): DOMElement
86
    {
87
        $e = $this->instantiateParentElement($parent);
88
89
        $e->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:integer');
90
        $e->textContent = strval($this->serial);
91
92
        return $e;
93
    }
94
}
95