|
10 | 10 | use SimpleSAML\XML\Exception\MissingAttributeException; |
11 | 11 | use SimpleSAML\XML\Exception\SchemaViolationException; |
12 | 12 | use SimpleSAML\XML\SerializableElementTrait; |
| 13 | +use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface}; |
13 | 14 |
|
14 | 15 | use function array_slice; |
15 | 16 | use function defined; |
16 | 17 | use function explode; |
17 | | -use function func_num_args; |
18 | | -use function in_array; |
19 | | -use function intval; |
20 | 18 | use function join; |
21 | 19 | use function strval; |
22 | 20 |
|
@@ -57,133 +55,57 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement |
57 | 55 |
|
58 | 56 |
|
59 | 57 | /** |
60 | | - * Get the value of an attribute from a given element. |
61 | | - * |
62 | | - * @param \DOMElement $xml The element where we should search for the attribute. |
63 | | - * @param string $name The name of the attribute. |
64 | | - * @return string |
| 58 | + * @template T of \SimpleSAML\XML\Type\ValueTypeInterface |
| 59 | + * @param \DOMElement $xml The element where we should search for the attribute. |
| 60 | + * @param string $name The name of the attribute. |
| 61 | + * @param class-string<T> $type The type of the attribute value. |
| 62 | + * @return T |
65 | 63 | * |
66 | 64 | * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element |
67 | 65 | */ |
68 | | - public static function getAttribute(DOMElement $xml, string $name): string |
69 | | - { |
| 66 | + public static function getAttribute( |
| 67 | + DOMElement $xml, |
| 68 | + string $name, |
| 69 | + string $type = StringValue::class, |
| 70 | + ): ValueTypeInterface { |
| 71 | + Assert::isAOf($type, ValueTypeInterface::class); |
| 72 | + |
70 | 73 | $prefix = static::getNamespacePrefix(); |
71 | 74 | $localName = static::getLocalName(); |
72 | 75 | $qName = $prefix ? ($prefix . ':' . $localName) : $localName; |
73 | 76 | Assert::true( |
74 | | - $xml->hasAttribute($name) && func_num_args() === 2, |
| 77 | + $xml->hasAttribute($name), |
75 | 78 | sprintf('Missing \'%s\' attribute on %s.', $name, $qName), |
76 | 79 | MissingAttributeException::class, |
77 | 80 | ); |
78 | 81 |
|
79 | | - return $xml->getAttribute($name); |
| 82 | + $value = $xml->getAttribute($name); |
| 83 | + return $type::fromString($value); |
80 | 84 | } |
81 | 85 |
|
82 | 86 |
|
83 | 87 | /** |
84 | 88 | * Get the value of an attribute from a given element. |
85 | 89 | * |
86 | | - * @param \DOMElement $xml The element where we should search for the attribute. |
87 | | - * @param string $name The name of the attribute. |
88 | | - * @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 |
| 90 | + * @template T of \SimpleSAML\XML\Type\ValueTypeInterface |
| 91 | + * @param \DOMElement $xml The element where we should search for the attribute. |
| 92 | + * @param string $name The name of the attribute. |
| 93 | + * @param class-string<T> $type The type of the attribute value. |
| 94 | + * @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default |
| 95 | + * The default to return in case the attribute does not exist and it is optional. |
| 96 | + * @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null) |
108 | 97 | */ |
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 |
133 | | - */ |
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 | | - { |
| 98 | + public static function getOptionalAttribute( |
| 99 | + DOMElement $xml, |
| 100 | + string $name, |
| 101 | + string $type = StringValue::class, |
| 102 | + ?ValueTypeInterface $default = null, |
| 103 | + ): ?ValueTypeInterface { |
182 | 104 | if (!$xml->hasAttribute($name)) { |
183 | 105 | return $default; |
184 | 106 | } |
185 | 107 |
|
186 | | - return self::getIntegerAttribute($xml, $name); |
| 108 | + return self::getAttribute($xml, $name, $type); |
187 | 109 | } |
188 | 110 |
|
189 | 111 |
|
|
0 commit comments