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

KeyInfoReference::getId()   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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSecurity\Constants as C;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
14
/**
15
 * Class representing a ds:KeyInfoReference element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class KeyInfoReference extends AbstractDsElement
20
{
21
    /** @var string $URI */
22
    protected string $URI;
23
24
    /** @var string|null $Id */
25
    protected ?string $Id = null;
26
27
28
    /**
29
     * Initialize a KeyInfoReference element.
30
     *
31
     * @param string $URI
32
     * @param string|null $Id
33
     */
34
    public function __construct(string $URI, ?string $Id = null)
35
    {
36
        $this->setURI($URI);
37
        $this->setId($Id);
38
    }
39
40
41
    /**
42
     * Collect the value of the URI-property
43
     *
44
     * @return string
45
     */
46
    public function getURI(): string
47
    {
48
        return $this->URI;
49
    }
50
51
52
    /**
53
     * Set the value of the URI-property
54
     *
55
     * @param string $URI
56
     */
57
    private function setURI(string $URI): void
58
    {
59
        Assert::validURI($URI, SchemaViolationException::class);
60
        $this->URI = $URI;
61
    }
62
63
64
    /**
65
     * Collect the value of the Id-property
66
     *
67
     * @return string|null
68
     */
69
    public function getId(): ?string
70
    {
71
        return $this->Id;
72
    }
73
74
75
    /**
76
     * Set the value of the Id-property
77
     *
78
     * @param string $Id
79
     */
80
    private function setId(?string $Id): void
81
    {
82
        Assert::nullOrValidNCName($Id);
83
        $this->Id = $Id;
84
    }
85
86
87
    /**
88
     * Convert XML into a KeyInfoReference
89
     *
90
     * @param \DOMElement $xml The XML element we should load
91
     * @return static
92
     *
93
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
94
     *   If the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, 'KeyInfoReference', InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, KeyInfoReference::NS, InvalidDOMElementException::class);
100
101
        /** @psalm-var string $URI */
102
        $URI = KeyInfoReference::getAttribute($xml, 'URI');
103
        $Id = KeyInfoReference::getAttribute($xml, 'Id', null);
104
105
        return new static($URI, $Id);
106
    }
107
108
109
    /**
110
     * Convert this KeyInfoReference element to XML.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this KeyInfoReference element to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(DOMElement $parent = null): DOMElement
116
    {
117
        $e = $this->instantiateParentElement($parent);
118
        $e->setAttribute('URI', $this->URI);
119
120
        if ($this->Id !== null) {
121
            $e->setAttribute('Id', $this->Id);
122
        }
123
124
        return $e;
125
    }
126
}
127