1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SimpleSAML\XMLSecurity\XML\ds; |
||
6 | |||
7 | use DOMElement; |
||
8 | use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; |
||
9 | use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException}; |
||
10 | use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue}; |
||
11 | use SimpleSAML\XMLSecurity\Assert\Assert; |
||
12 | |||
13 | use function array_pop; |
||
14 | use function strval; |
||
15 | |||
16 | /** |
||
17 | * Class representing a ds:Reference element. |
||
18 | * |
||
19 | * @package simplesamlphp/xml-security |
||
20 | */ |
||
21 | final class Reference extends AbstractDsElement implements SchemaValidatableElementInterface |
||
22 | { |
||
23 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
24 | |||
25 | /** |
||
26 | * Initialize a ds:Reference |
||
27 | * |
||
28 | * @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod |
||
29 | * @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue |
||
30 | * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms |
||
31 | * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id |
||
32 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Type |
||
33 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $URI |
||
34 | */ |
||
35 | public function __construct( |
||
36 | protected DigestMethod $digestMethod, |
||
37 | protected DigestValue $digestValue, |
||
38 | protected ?Transforms $transforms = null, |
||
39 | protected ?IDValue $Id = null, |
||
40 | protected ?AnyURIValue $Type = null, |
||
41 | protected ?AnyURIValue $URI = null, |
||
42 | ) { |
||
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 \SimpleSAML\XMLSchema\Type\IDValue|null |
||
75 | */ |
||
76 | public function getId(): ?IDValue |
||
77 | { |
||
78 | return $this->Id; |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null |
||
84 | */ |
||
85 | public function getType(): ?AnyURIValue |
||
86 | { |
||
87 | return $this->Type; |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null |
||
93 | */ |
||
94 | public function getURI(): ?AnyURIValue |
||
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 !is_null($this->getURI()) && str_starts_with(strval($this->getURI()), '#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\XMLSchema\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::getOptionalAttribute($xml, 'Id', IDValue::class, null); |
||
126 | $Type = self::getOptionalAttribute($xml, 'Type', AnyURIValue::class, null); |
||
127 | $URI = self::getOptionalAttribute($xml, 'URI', AnyURIValue::class, 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', strval($this->getId())); |
||
175 | } |
||
176 | if ($this->getType() !== null) { |
||
177 | $e->setAttribute('Type', strval($this->getType())); |
||
178 | } |
||
179 | if ($this->getURI() !== null) { |
||
180 | $e->setAttribute('URI', strval($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 |