Passed
Push — master ( f7c7ec...af75f1 )
by Tim
01:59
created

RetrievalMethod::getURI()   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\XML\Exception\TooManyElementsException;
12
13
/**
14
 * Class representing a ds:RetrievalMethod element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class RetrievalMethod extends AbstractDsElement
19
{
20
    protected ?Transforms $transforms;
21
22
    /** @var string $URI */
23
    protected string $URI;
24
25
    /** @var string|null $Type */
26
    protected ?string $Type;
27
28
29
    /**
30
     * Initialize a ds:RetrievalMethod
31
     *
32
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
33
     * @param string $URI
34
     * @param string|null $Type
35
     */
36
    final public function __construct(
37
        ?Transforms $transforms,
38
        string $URI,
39
        ?string $Type = null
40
    ) {
41
        $this->setTransforms($transforms);
42
        $this->setURI($URI);
43
        $this->setType($Type);
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
49
     */
50
    public function getTransforms(): ?Transforms
51
    {
52
        return $this->transforms;
53
    }
54
55
56
    /**
57
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
58
     */
59
    protected function setTransforms(?Transforms $transforms): void
60
    {
61
        $this->transforms = $transforms;
62
    }
63
64
65
    /**
66
     * @return string
67
     */
68
    public function getURI(): string
69
    {
70
        return $this->URI;
71
    }
72
73
74
    /**
75
     * @param string $URI
76
     */
77
    private function setURI(string $URI): void
78
    {
79
        Assert::validURI($URI, SchemaViolationException::class); // Covers the empty string
80
        $this->URI = $URI;
81
    }
82
83
84
    /**
85
     * @return string|null
86
     */
87
    public function getType(): ?string
88
    {
89
        return $this->Type;
90
    }
91
92
93
    /**
94
     * @param string|null $Type
95
     */
96
    private function setType(?string $Type): void
97
    {
98
        if ($Type !== null) {
99
            Assert::validURI($Type, SchemaViolationException::class); // Covers the empty string
100
        }
101
        $this->Type = $Type;
102
    }
103
104
105
    /**
106
     * Convert XML into a RetrievalMethod element
107
     *
108
     * @param \DOMElement $xml The XML element we should load
109
     * @return static
110
     *
111
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
112
     *   If the qualified name of the supplied element is wrong
113
     */
114
    public static function fromXML(DOMElement $xml): static
115
    {
116
        Assert::same($xml->localName, 'RetrievalMethod', InvalidDOMElementException::class);
117
        Assert::same($xml->namespaceURI, RetrievalMethod::NS, InvalidDOMElementException::class);
118
119
        /** @psalm-var string $URI */
120
        $URI = self::getAttribute($xml, 'URI');
121
        $Type = self::getAttribute($xml, 'Type', null);
122
123
        /** @psalm-var \SimpleSAML\XMLSecurity\XML\ds\Transforms[] $transforms */
124
        $transforms = Transforms::getChildrenOfClass($xml);
125
        Assert::maxCount(
126
            $transforms,
127
            1,
128
            'A <ds:RetrievalMethod> may contain a maximum of one <ds:Transforms>.',
129
            TooManyElementsException::class,
130
        );
131
132
        return new static(
133
            array_pop($transforms),
134
            $URI,
135
            $Type
136
        );
137
    }
138
139
140
    /**
141
     * Convert this RetrievalMethod element to XML.
142
     *
143
     * @param \DOMElement|null $parent The element we should append this RetrievalMethod element to.
144
     * @return \DOMElement
145
     */
146
    public function toXML(DOMElement $parent = null): DOMElement
147
    {
148
        $e = $this->instantiateParentElement($parent);
149
        $e->setAttribute('URI', $this->URI);
150
151
        if ($this->Type !== null) {
152
            $e->setAttribute('Type', $this->Type);
153
        }
154
155
        if ($this->transforms !== null) {
156
            $this->transforms->toXML($e);
157
        }
158
159
        return $e;
160
    }
161
}
162