Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

Reference::setType()   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 1
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
13
use function array_pop;
14
15
/**
16
 * Class representing a ds:Reference element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class Reference extends AbstractDsElement
21
{
22
    /**
23
     * Initialize a ds:Reference
24
     *
25
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
26
     * @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
28
     * @param string|null $Id
29
     * @param string|null $Type
30
     * @param string|null $URI
31
     */
32
    public function __construct(
33
        protected DigestMethod $digestMethod,
34
        protected DigestValue $digestValue,
35
        protected ?Transforms $transforms = null,
36
        protected ?string $Id = null,
37
        protected ?string $Type = null,
38
        protected ?string $URI = null,
39
    ) {
40
        Assert::nullOrValidNCName($Id);
41
        Assert::nullOrValidURI($Type);
42
        Assert::nullOrValidURI($URI);
43
    }
44
45
46
    /**
47
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
48
     */
49
    public function getTransforms(): ?Transforms
50
    {
51
        return $this->transforms;
52
    }
53
54
55
    /**
56
     * @return \SimpleSAML\XMLSecurity\XML\ds\DigestMethod
57
     */
58
    public function getDigestMethod(): DigestMethod
59
    {
60
        return $this->digestMethod;
61
    }
62
63
64
    /**
65
     * @return \SimpleSAML\XMLSecurity\XML\ds\DigestValue
66
     */
67
    public function getDigestValue(): DigestValue
68
    {
69
        return $this->digestValue;
70
    }
71
72
73
    /**
74
     * @return string|null
75
     */
76
    public function getId(): ?string
77
    {
78
        return $this->Id;
79
    }
80
81
82
    /**
83
     * @return string|null
84
     */
85
    public function getType(): ?string
86
    {
87
        return $this->Type;
88
    }
89
90
91
    /**
92
     * @return string|null
93
     */
94
    public function getURI(): ?string
95
    {
96
        return $this->URI;
97
    }
98
99
100
    /**
101
     * Determine whether this is an xpointer reference.
102
     *
103
     * @return bool
104
     */
105
    public function isXPointer(): bool
106
    {
107
        return !empty($this->URI) && str_starts_with($this->URI, '#xpointer');
108
    }
109
110
111
    /**
112
     * Convert XML into a Reference element
113
     *
114
     * @param \DOMElement $xml The XML element we should load
115
     * @return static
116
     *
117
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
118
     *   If the qualified name of the supplied element is wrong
119
     */
120
    public static function fromXML(DOMElement $xml): static
121
    {
122
        Assert::same($xml->localName, 'Reference', InvalidDOMElementException::class);
123
        Assert::same($xml->namespaceURI, Reference::NS, InvalidDOMElementException::class);
124
125
        $Id = self::getAttribute($xml, 'Id', null);
126
        $Type = self::getAttribute($xml, 'Type', null);
127
        $URI = self::getAttribute($xml, 'URI', null);
128
129
        $transforms = Transforms::getChildrenOfClass($xml);
130
        Assert::maxCount(
131
            $transforms,
132
            1,
133
            'A <ds:Reference> may contain just one <ds:Transforms>.',
134
            TooManyElementsException::class,
135
        );
136
137
        $digestMethod = DigestMethod::getChildrenOfClass($xml);
138
        Assert::count(
139
            $digestMethod,
140
            1,
141
            'A <ds:Reference> must contain a <ds:DigestMethod>.',
142
            MissingElementException::class,
143
        );
144
145
        $digestValue = DigestValue::getChildrenOfClass($xml);
146
        Assert::count(
147
            $digestValue,
148
            1,
149
            'A <ds:Reference> must contain a <ds:DigestValue>.',
150
            MissingElementException::class,
151
        );
152
153
        return new static(
154
            array_pop($digestMethod),
155
            array_pop($digestValue),
156
            empty($transforms) ? null : array_pop($transforms),
157
            $Id,
158
            $Type,
159
            $URI,
160
        );
161
    }
162
163
164
    /**
165
     * Convert this Reference element to XML.
166
     *
167
     * @param \DOMElement|null $parent The element we should append this Reference element to.
168
     * @return \DOMElement
169
     */
170
    public function toXML(DOMElement $parent = null): DOMElement
171
    {
172
        $e = $this->instantiateParentElement($parent);
173
        if ($this->getId() !== null) {
174
            $e->setAttribute('Id', $this->getId());
175
        }
176
        if ($this->getType() !== null) {
177
            $e->setAttribute('Type', $this->getType());
178
        }
179
        if ($this->getURI() !== null) {
180
            $e->setAttribute('URI', $this->getURI());
181
        }
182
183
        $this->getTransforms()?->toXML($e);
184
        $this->getDigestMethod()->toXML($e);
185
        $this->getDigestValue()->toXML($e);
186
187
        return $e;
188
    }
189
}
190