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

Reference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 14
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
11
/**
12
 * Class representing a ds:Reference element.
13
 *
14
 * @package simplesamlphp/xml-security
15
 */
16
final class Reference extends AbstractDsElement
17
{
18
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Transforms|null */
19
    protected Transforms $transforms;
20
21
    /** @var \SimpleSAML\XMLSecurity\XML\ds\DigestMethod */
22
    protected DigestMethod $digestMethod;
23
24
    /** @var \SimpleSAML\XMLSecurity\XML\ds\DigestValue */
25
    protected DigestValue $digestValue;
26
27
    /** @var string|null $Id */
28
    protected ?string $Id;
29
30
    /** @var string|null $type */
31
    protected ?string $Type;
32
33
    /** @var string|null $URI */
34
    protected ?string $URI;
35
36
37
    /**
38
     * Initialize a ds:Reference
39
     *
40
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue
42
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
43
     * @param string|null $Id
44
     * @param string|null $Type
45
     * @param string|null $URI
46
     */
47
    public function __construct(
48
        DigestMethod $digestMethod,
49
        DigestValue $digestValue,
50
        ?Transforms $transforms = null,
51
        ?string $Id = null,
52
        ?string $Type = null,
53
        ?string $URI = null
54
    ) {
55
        $this->setTransforms($transforms);
56
        $this->setDigestMethod($digestMethod);
57
        $this->setDigestValue($digestValue);
58
        $this->setId($Id);
59
        $this->setType($Type);
60
        $this->setURI($URI);
61
    }
62
63
64
    /**
65
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
66
     */
67
    public function getTransforms(): ?Transforms
68
    {
69
        return $this->transforms;
70
    }
71
72
73
    /**
74
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
75
     */
76
    protected function setTransforms(?Transforms $transforms): void
77
    {
78
        $this->transforms = $transforms;
79
    }
80
81
82
    /**
83
     * @return \SimpleSAML\XMLSecurity\XML\ds\DigestMethod
84
     */
85
    public function getDigestMethod(): DigestMethod
86
    {
87
        return $this->digestMethod;
88
    }
89
90
91
    /**
92
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
93
     */
94
    private function setDigestMethod(DigestMethod $digestMethod): void
95
    {
96
        $this->digestMethod = $digestMethod;
97
    }
98
99
100
    /**
101
     * @return \SimpleSAML\XMLSecurity\XML\ds\DigestValue
102
     */
103
    public function getDigestValue(): DigestValue
104
    {
105
        return $this->digestValue;
106
    }
107
108
109
    /**
110
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue
111
     */
112
    private function setDigestValue(DigestValue $digestValue): void
113
    {
114
        $this->digestValue = $digestValue;
115
    }
116
117
118
    /**
119
     * @return string|null
120
     */
121
    public function getId(): ?string
122
    {
123
        return $this->Id;
124
    }
125
126
127
    /**
128
     * @param string|null $Id
129
     */
130
    private function setId(?string $Id): void
131
    {
132
        $this->Id = $Id;
133
    }
134
135
136
    /**
137
     * @return string|null
138
     */
139
    public function getType(): ?string
140
    {
141
        return $this->Type;
142
    }
143
144
145
    /**
146
     * @param string|null $Type
147
     */
148
    private function setType(?string $Type): void
149
    {
150
        $this->Type = $Type;
151
    }
152
153
154
    /**
155
     * @return string|null
156
     */
157
    public function getURI(): ?string
158
    {
159
        return $this->URI;
160
    }
161
162
163
    /**
164
     * @param string|null $URI
165
     */
166
    private function setURI(?string $URI): void
167
    {
168
        $this->URI = $URI;
169
    }
170
171
172
    /**
173
     * Convert XML into a Reference element
174
     *
175
     * @param \DOMElement $xml The XML element we should load
176
     * @return self
177
     *
178
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
179
     *   If the qualified name of the supplied element is wrong
180
     */
181
    public static function fromXML(DOMElement $xml): object
182
    {
183
        Assert::same($xml->localName, 'Reference', InvalidDOMElementException::class);
184
        Assert::same($xml->namespaceURI, Reference::NS, InvalidDOMElementException::class);
185
186
        $Id = self::getAttribute($xml, 'Id', null);
187
        $Type = self::getAttribute($xml, 'Type', null);
188
        $URI = self::getAttribute($xml, 'URI', null);
189
190
        $transforms = Transforms::getChildrenOfClass($xml);
191
        Assert::maxCount($transforms, 1, 'A <ds:Reference> may contain just one <ds:Transforms>.');
192
193
        $digestMethod = DigestMethod::getChildrenOfClass($xml);
194
        Assert::count($digestMethod, 1, 'A <ds:Reference> must contain a <ds:DigestMethod>.');
195
196
        $digestValue = DigestValue::getChildrenOfClass($xml);
197
        Assert::count($digestValue, 1, 'A <ds:Reference> must contain a <ds:DigestValue>.');
198
199
        return new self(
200
            array_pop($digestMethod),
201
            array_pop($digestValue),
202
            empty($transforms) ? null : array_pop($transforms),
203
            $Id,
204
            $Type,
205
            $URI
206
        );
207
    }
208
209
210
    /**
211
     * Convert this Reference element to XML.
212
     *
213
     * @param \DOMElement|null $parent The element we should append this Reference element to.
214
     * @return \DOMElement
215
     */
216
    public function toXML(DOMElement $parent = null): DOMElement
217
    {
218
        $e = $this->instantiateParentElement($parent);
219
        if ($this->Id !== null) {
220
            $e->setAttribute('Id', $this->Id);
221
        }
222
        if ($this->Type !== null) {
223
            $e->setAttribute('Type', $this->Type);
224
        }
225
        if ($this->URI !== null) {
226
            $e->setAttribute('URI', $this->URI);
227
        }
228
229
        if ($this->transforms !== null) {
230
            $this->transforms->toXML($e);
231
        }
232
233
        $this->digestMethod->toXML($e);
234
        $this->digestValue->toXML($e);
235
236
        return $e;
237
    }
238
}
239