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 ;
@@ -61,22 +62,29 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
6162 *
6263 * @param \DOMElement $xml The element where we should search for the attribute.
6364 * @param string $name The name of the attribute.
64- * @return string
65+ * @param string $type The type of the attribute value.
66+ * @return \SimpleSAML\XML\Type\ValueTypeInterface
6567 *
6668 * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
6769 */
68- public static function getAttribute (DOMElement $ xml , string $ name ): string
69- {
70+ public static function getAttribute (
71+ DOMElement $ xml ,
72+ string $ name ,
73+ string $ type = StringValue::class,
74+ ): ValueTypeInterface {
75+ Assert::isAOf ($ type , ValueTypeInterface::class);
76+
7077 $ prefix = static ::getNamespacePrefix ();
7178 $ localName = static ::getLocalName ();
7279 $ qName = $ prefix ? ($ prefix . ': ' . $ localName ) : $ localName ;
7380 Assert::true (
74- $ xml ->hasAttribute ($ name ) && func_num_args () === 2 ,
81+ $ xml ->hasAttribute ($ name ),
7582 sprintf ('Missing \'%s \' attribute on %s. ' , $ name , $ qName ),
7683 MissingAttributeException::class,
7784 );
7885
79- return $ xml ->getAttribute ($ name );
86+ $ value = $ xml ->getAttribute ($ name );
87+ return $ type ::fromString ($ value );
8088 }
8189
8290
@@ -85,105 +93,21 @@ public static function getAttribute(DOMElement $xml, string $name): string
8593 *
8694 * @param \DOMElement $xml The element where we should search for the attribute.
8795 * @param string $name The name of the attribute.
96+ * @param string $type The type of the attribute value.
8897 * @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
98+ * @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
13399 */
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- {
100+ public static function getOptionalAttribute (
101+ DOMElement $ xml ,
102+ string $ name ,
103+ string $ type = StringValue::class,
104+ ?ValueTypeInterface $ default = null ,
105+ ): ?ValueTypeInterface {
182106 if (!$ xml ->hasAttribute ($ name )) {
183107 return $ default ;
184108 }
185109
186- return self ::getIntegerAttribute ($ xml , $ name );
110+ return self ::getAttribute ($ xml , $ name, $ type );
187111 }
188112
189113
0 commit comments