Reference::getTransforms()   A
last analyzed

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