1010use SimpleSAML \XML \Exception \MissingAttributeException ;
1111use SimpleSAML \XML \Exception \SchemaViolationException ;
1212use SimpleSAML \XML \SerializableElementTrait ;
13+ use SimpleSAML \XML \Type \{StringValue , ValueTypeInterface };
1314
1415use function array_slice ;
1516use function defined ;
1617use function explode ;
17- use function func_num_args ;
18- use function in_array ;
19- use function intval ;
2018use function join ;
2119use function strval ;
2220
@@ -61,22 +59,29 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
6159 *
6260 * @param \DOMElement $xml The element where we should search for the attribute.
6361 * @param string $name The name of the attribute.
64- * @return string
62+ * @param string $type The type of the attribute value.
63+ * @return \SimpleSAML\XML\Type\ValueTypeInterface
6564 *
6665 * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
6766 */
68- public static function getAttribute (DOMElement $ xml , string $ name ): string
69- {
67+ public static function getAttribute (
68+ DOMElement $ xml ,
69+ string $ name ,
70+ string $ type = StringValue::class,
71+ ): ValueTypeInterface {
72+ Assert::isAOf ($ type , ValueTypeInterface::class);
73+
7074 $ prefix = static ::getNamespacePrefix ();
7175 $ localName = static ::getLocalName ();
7276 $ qName = $ prefix ? ($ prefix . ': ' . $ localName ) : $ localName ;
7377 Assert::true (
74- $ xml ->hasAttribute ($ name ) && func_num_args () === 2 ,
78+ $ xml ->hasAttribute ($ name ),
7579 sprintf ('Missing \'%s \' attribute on %s. ' , $ name , $ qName ),
7680 MissingAttributeException::class,
7781 );
7882
79- return $ xml ->getAttribute ($ name );
83+ $ value = $ xml ->getAttribute ($ name );
84+ return $ type ::fromString ($ value );
8085 }
8186
8287
@@ -85,105 +90,21 @@ public static function getAttribute(DOMElement $xml, string $name): string
8590 *
8691 * @param \DOMElement $xml The element where we should search for the attribute.
8792 * @param string $name The name of the attribute.
93+ * @param string $type The type of the attribute value.
8894 * @param string|null $default The default to return in case the attribute does not exist and it is optional.
89- * @return ($default is string ? string : string|null)
90- */
91- public static function getOptionalAttribute (DOMElement $ xml , string $ name , ?string $ default = null ): ?string
92- {
93- if (!$ xml ->hasAttribute ($ name )) {
94- return $ default ;
95- }
96-
97- return self ::getAttribute ($ xml , $ name );
98- }
99-
100-
101- /**
102- * @param \DOMElement $xml The element where we should search for the attribute.
103- * @param string $name The name of the attribute.
104- * @return bool
105- *
106- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
107- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
108- */
109- public static function getBooleanAttribute (DOMElement $ xml , string $ name ): bool
110- {
111- $ value = self ::getAttribute ($ xml , $ name );
112-
113- $ prefix = static ::getNamespacePrefix ();
114- $ localName = static ::getLocalName ();
115- $ qName = $ prefix ? ($ prefix . ': ' . $ localName ) : $ localName ;
116- Assert::oneOf (
117- $ value ,
118- ['0 ' , '1 ' , 'false ' , 'true ' ],
119- sprintf ('The \'%s \' attribute of %s must be a boolean. ' , $ name , $ qName ),
120- );
121-
122- return in_array ($ value , ['1 ' , 'true ' ], true );
123- }
124-
125-
126- /**
127- * @param \DOMElement $xml The element where we should search for the attribute.
128- * @param string $name The name of the attribute.
129- * @param bool|null $default The default to return in case the attribute does not exist and it is optional.
130- * @return ($default is bool ? bool : bool|null)
131- *
132- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
95+ * @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
13396 */
134- public static function getOptionalBooleanAttribute (DOMElement $ xml , string $ name , ?bool $ default = null ): ?bool
135- {
136- if (!$ xml ->hasAttribute ($ name )) {
137- return $ default ;
138- }
139-
140- return self ::getBooleanAttribute ($ xml , $ name );
141- }
142-
143-
144- /**
145- * Get the integer value of an attribute from a given element.
146- *
147- * @param \DOMElement $xml The element where we should search for the attribute.
148- * @param string $name The name of the attribute.
149- * @return int
150- *
151- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
152- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
153- */
154- public static function getIntegerAttribute (DOMElement $ xml , string $ name ): int
155- {
156- $ value = self ::getAttribute ($ xml , $ name );
157-
158- $ prefix = static ::getNamespacePrefix ();
159- $ localName = static ::getLocalName ();
160- $ qName = $ prefix ? ($ prefix . ': ' . $ localName ) : $ localName ;
161- Assert::numeric (
162- $ value ,
163- sprintf ('The \'%s \' attribute of %s must be numerical. ' , $ name , $ qName ),
164- );
165-
166- return intval ($ value );
167- }
168-
169-
170- /**
171- * Get the integer value of an attribute from a given element.
172- *
173- * @param \DOMElement $xml The element where we should search for the attribute.
174- * @param string $name The name of the attribute.
175- * @param int|null $default The default to return in case the attribute does not exist and it is optional.
176- * @return ($default is int ? int : int|null)
177- *
178- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
179- */
180- public static function getOptionalIntegerAttribute (DOMElement $ xml , string $ name , ?int $ default = null ): ?int
181- {
97+ public static function getOptionalAttribute (
98+ DOMElement $ xml ,
99+ string $ name ,
100+ string $ type = StringValue::class,
101+ ?ValueTypeInterface $ default = null ,
102+ ): ?ValueTypeInterface {
182103 if (!$ xml ->hasAttribute ($ name )) {
183104 return $ default ;
184105 }
185106
186- return self ::getIntegerAttribute ($ xml , $ name );
107+ return self ::getAttribute ($ xml , $ name, $ type );
187108 }
188109
189110
0 commit comments