Passed
Pull Request — master (#8)
by Tim
03:28 queued 01:13
created

X509IssuerSerial::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\Utils as XMLUtils;
14
15
/**
16
 * Class representing a ds:X509IssuerSerial element.
17
 *
18
 * @package simplesaml/xml-security
19
 */
20
final class X509IssuerSerial extends AbstractDsElement
21
{
22
    /**
23
     * The Issuer's name.
24
     *
25
     * @var \SimpleSAML\XMLSecurity\XML\ds\X509IssuerName
26
     */
27
    protected X509IssuerName $X509IssuerName;
28
29
    /**
30
     * The serial number.
31
     *
32
     * @var \SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber
33
     */
34
    protected X509SerialNumber $X509SerialNumber;
35
36
37
    /**
38
     * Initialize a X509SubjectName element.
39
     *
40
     * @param \SimpleSAML\XMLSecurity\XML\ds\X509IssuerName $name
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber $serial
42
     */
43
    public function __construct(X509IssuerName $name, X509SerialNumber $serial)
44
    {
45
        $this->setIssuerName($name);
46
        $this->setSerialNumber($serial);
47
    }
48
49
50
    /**
51
     * Collect the value of the X509IssuerName-property
52
     *
53
     * @return \SimpleSAML\XMLSecurity\XML\ds\X509IssuerName
54
     */
55
    public function getIssuerName(): X509IssuerName
56
    {
57
        return $this->X509IssuerName;
58
    }
59
60
61
    /**
62
     * Set the value of the X509IssuerName-property
63
     *
64
     * @param \SimpleSAML\XMLSecurity\XML\ds\X509IssuerName $name
65
     */
66
    private function setIssuerName(X509IssuerName $name): void
67
    {
68
        $this->X509IssuerName = $name;
69
    }
70
71
72
    /**
73
     * Collect the value of the X509SerialNumber-property
74
     *
75
     * @return \SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber
76
     */
77
    public function getSerialNumber(): X509SerialNumber
78
    {
79
        return $this->X509SerialNumber;
80
    }
81
82
83
    /**
84
     * Set the value of the X509SerialNumber-property
85
     *
86
     * @param \SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber $serial
87
     */
88
    private function setSerialNumber(X509SerialNumber $serial): void
89
    {
90
        $this->X509SerialNumber = $serial;
91
    }
92
93
94
    /**
95
     * Convert XML into a X509IssuerSerial
96
     *
97
     * @param \DOMElement $xml The XML element we should load
98
     * @return self
99
     *
100
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
101
     *   If the qualified name of the supplied element is wrong
102
     */
103
    public static function fromXML(DOMElement $xml): object
104
    {
105
        Assert::same($xml->localName, 'X509IssuerSerial', InvalidDOMElementException::class);
106
        Assert::same($xml->namespaceURI, X509IssuerSerial::NS, InvalidDOMElementException::class);
107
108
        $issuer = X509IssuerName::getChildrenOfClass($xml);
109
        $serial = X509SerialNumber::getChildrenOfClass($xml);
110
111
        Assert::minCount($issuer, 1, MissingElementException::class);
112
        Assert::maxCount($issuer, 1, TooManyElementsException::class);
113
114
        Assert::minCount($serial, 1, MissingElementException::class);
115
        Assert::maxCount($serial, 1, TooManyElementsException::class);
116
117
        return new self(
118
            array_pop($issuer),
119
            array_pop($serial)
120
        );
121
    }
122
123
124
    /**
125
     * Convert this X509IssuerSerial element to XML.
126
     *
127
     * @param \DOMElement|null $parent The element we should append this X509IssuerSerial element to.
128
     * @return \DOMElement
129
     */
130
    public function toXML(DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
134
        $this->X509IssuerName->toXML($e);
135
        $this->X509SerialNumber->toXML($e);
136
137
        return $e;
138
    }
139
}
140