Skip to content

Commit 94db95a

Browse files
committed
Restructure getAttribute/getOptionalAttribute
1 parent c2b9b8e commit 94db95a

File tree

9 files changed

+172
-313
lines changed

9 files changed

+172
-313
lines changed

src/AbstractElement.php

Lines changed: 30 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
use SimpleSAML\XML\Exception\MissingAttributeException;
1111
use SimpleSAML\XML\Exception\SchemaViolationException;
1212
use SimpleSAML\XML\SerializableElementTrait;
13+
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
1314

1415
use function array_slice;
1516
use function defined;
1617
use function explode;
17-
use function func_num_args;
18-
use function in_array;
19-
use function intval;
2018
use function join;
2119
use function strval;
2220

@@ -57,133 +55,57 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
5755

5856

5957
/**
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
6563
*
6664
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
6765
*/
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+
7073
$prefix = static::getNamespacePrefix();
7174
$localName = static::getLocalName();
7275
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
7376
Assert::true(
74-
$xml->hasAttribute($name) && func_num_args() === 2,
77+
$xml->hasAttribute($name),
7578
sprintf('Missing \'%s\' attribute on %s.', $name, $qName),
7679
MissingAttributeException::class,
7780
);
7881

79-
return $xml->getAttribute($name);
82+
$value = $xml->getAttribute($name);
83+
return $type::fromString($value);
8084
}
8185

8286

8387
/**
8488
* Get the value of an attribute from a given element.
8589
*
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)
10897
*/
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 {
182104
if (!$xml->hasAttribute($name)) {
183105
return $default;
184106
}
185107

186-
return self::getIntegerAttribute($xml, $name);
108+
return self::getAttribute($xml, $name, $type);
187109
}
188110

189111

src/Chunk.php

Lines changed: 33 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
use DOMElement;
88
use SimpleSAML\XML\Assert\Assert;
9+
use SimpleSAML\XML\DOMDocumentFactory;
910
use SimpleSAML\XML\Exception\MissingAttributeException;
1011
use SimpleSAML\XML\Exception\SchemaViolationException;
1112
use SimpleSAML\XML\SerializableElementTrait;
12-
13-
use function in_array;
14-
use function intval;
13+
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
1514

1615
/**
1716
* Serializable class used to hold an XML element.
@@ -158,120 +157,56 @@ public function getQualifiedName(): string
158157

159158

160159
/**
161-
* @param \DOMElement $xml The element where we should search for the attribute.
162-
* @param string $name The name of the attribute.
163-
* @return string
160+
* Get the value of an attribute from a given element.
161+
*
162+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
163+
* @param \DOMElement $xml The element where we should search for the attribute.
164+
* @param string $name The name of the attribute.
165+
* @param class-string<T> $type The type of the attribute value.
166+
* @return T
164167
*
165168
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
166169
*/
167-
public static function getAttribute(DOMElement $xml, string $name): string
168-
{
170+
public static function getAttribute(
171+
DOMElement $xml,
172+
string $name,
173+
string $type = StringValue::class,
174+
): ValueTypeInterface {
175+
Assert::isAOf($type, ValueTypeInterface::class);
176+
169177
Assert::true(
170178
$xml->hasAttribute($name),
171179
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
172180
MissingAttributeException::class,
173181
);
174182

175-
return $xml->getAttribute($name);
176-
}
177-
178-
179-
/**
180-
* @param \DOMElement $xml The element where we should search for the attribute.
181-
* @param string $name The name of the attribute.
182-
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
183-
* @return ($default is string ? string : null)
184-
*/
185-
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
186-
{
187-
if (!$xml->hasAttribute($name)) {
188-
return $default;
189-
}
190-
191-
return $xml->getAttribute($name);
192-
}
193-
194-
195-
/**
196-
* @param \DOMElement $xml The element where we should search for the attribute.
197-
* @param string $name The name of the attribute.
198-
* @return bool
199-
*
200-
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
201-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
202-
*/
203-
public static function getBooleanAttribute(DOMElement $xml, string $name): bool
204-
{
205-
$value = self::getAttribute($xml, $name);
206-
207-
Assert::oneOf(
208-
$value,
209-
['0', '1', 'false', 'true'],
210-
'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be boolean.',
211-
);
212-
213-
return in_array($value, ['1', 'true'], true);
214-
}
215-
216-
217-
/**
218-
* @param \DOMElement $xml The element where we should search for the attribute.
219-
* @param string $name The name of the attribute.
220-
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
221-
* @return ($default is bool ? bool : null)
222-
*
223-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
224-
*/
225-
public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool
226-
{
227-
if (!$xml->hasAttribute($name)) {
228-
return $default;
229-
}
230-
231-
return self::getBooleanAttribute($xml, $name);
232-
}
233-
234-
235-
/**
236-
* Get the integer value of an attribute from a given element.
237-
*
238-
* @param \DOMElement $xml The element where we should search for the attribute.
239-
* @param string $name The name of the attribute.
240-
* @return int
241-
*
242-
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
243-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
244-
*/
245-
public static function getIntegerAttribute(DOMElement $xml, string $name): int
246-
{
247-
$value = self::getAttribute($xml, $name);
248-
249-
Assert::numeric(
250-
$value,
251-
'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be numerical.',
252-
);
253-
254-
return intval($value);
183+
$value = $xml->getAttribute($name);
184+
return $type::fromString($value);
255185
}
256186

257187

258188
/**
259-
* Get the integer value of an attribute from a given element.
260-
*
261-
* @param \DOMElement $xml The element where we should search for the attribute.
262-
* @param string $name The name of the attribute.
263-
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
264-
* @return ($default is int ? int : null)
189+
* Get the value of an attribute from a given element.
265190
*
266-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
191+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
192+
* @param \DOMElement $xml The element where we should search for the attribute.
193+
* @param string $name The name of the attribute.
194+
* @param class-string<T> $type The type of the attribute value.
195+
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
196+
* The default to return in case the attribute does not exist and it is optional.
197+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null)
267198
*/
268-
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
269-
{
199+
public static function getOptionalAttribute(
200+
DOMElement $xml,
201+
string $name,
202+
string $type = StringValue::class,
203+
?ValueTypeInterface $default = null,
204+
): ?ValueTypeInterface {
270205
if (!$xml->hasAttribute($name)) {
271206
return $default;
272207
}
273208

274-
return self::getIntegerAttribute($xml, $name);
209+
return self::getAttribute($xml, $name, $type);
275210
}
276211

277212

0 commit comments

Comments
 (0)