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

X509IssuerSerial::getIssuerName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 string
26
     */
27
    protected string $X509IssuerName;
28
29
    /**
30
     * The serial number.
31
     *
32
     * @var string
33
     */
34
    protected string $X509SerialNumber;
35
36
37
    /**
38
     * Initialize a X509SubjectName element.
39
     *
40
     * @param string $name
41
     * @param string $serial
42
     */
43
    public function __construct(string $name, string $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 string
54
     */
55
    public function getIssuerName(): string
56
    {
57
        return $this->X509IssuerName;
58
    }
59
60
61
    /**
62
     * Set the value of the X509IssuerName-property
63
     *
64
     * @param string $name
65
     */
66
    private function setIssuerName(string $name): void
67
    {
68
        Assert::notEmpty($name, 'X509IssuerName cannot be empty');
69
        $this->X509IssuerName = $name;
70
    }
71
72
73
    /**
74
     * Collect the value of the X509SerialNumber-property
75
     *
76
     * @return string
77
     */
78
    public function getSerialNumber(): string
79
    {
80
        return $this->X509SerialNumber;
81
    }
82
83
84
    /**
85
     * Set the value of the X509SerialNumber-property
86
     *
87
     * @param string $serial
88
     */
89
    private function setSerialNumber(string $serial): void
90
    {
91
        Assert::notEmpty($serial, 'X509SerialNumber cannot be empty');
92
        $this->X509SerialNumber = $serial;
93
    }
94
95
96
    /**
97
     * Convert XML into a X509IssuerSerial
98
     *
99
     * @param \DOMElement $xml The XML element we should load
100
     * @return self
101
     *
102
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
103
     *   If the qualified name of the supplied element is wrong
104
     */
105
    public static function fromXML(DOMElement $xml): object
106
    {
107
        Assert::same($xml->localName, 'X509IssuerSerial', InvalidDOMElementException::class);
108
        Assert::same($xml->namespaceURI, X509IssuerSerial::NS, InvalidDOMElementException::class);
109
110
        $issuer = XMLUtils::extractStrings($xml, AbstractDsElement::NS, 'X509IssuerName');
111
        $serial = XMLUtils::extractStrings($xml, AbstractDsElement::NS, 'X509SerialNumber');
112
113
        Assert::minCount($issuer, 1, MissingElementException::class);
114
        Assert::minCount($issuer, 1, TooManyElementsException::class);
115
116
        Assert::minCount($serial, 1, MissingElementException::class);
117
        Assert::minCount($serial, 1, TooManyElementsException::class);
118
119
        return new self(
120
            array_pop($issuer),
121
            array_pop($serial)
122
        );
123
    }
124
125
126
    /**
127
     * Convert this X509IssuerSerial element to XML.
128
     *
129
     * @param \DOMElement|null $parent The element we should append this X509IssuerSerial element to.
130
     * @return \DOMElement
131
     */
132
    public function toXML(DOMElement $parent = null): DOMElement
133
    {
134
        $e = $this->instantiateParentElement($parent);
135
136
        XMLUtils::addString($e, AbstractDsElement::NS, 'X509IssuerName', $this->X509IssuerName);
137
        XMLUtils::addString($e, AbstractDsElement::NS, 'X509SerialNumber', $this->X509SerialNumber);
138
139
        // Fix the xs:type on the SerialNumber
140
        $e->childNodes[1]->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:integer');
141
142
        return $e;
143
    }
144
}
145