Skip to content

Commit 81153a2

Browse files
committed
Restructure getAttribute/getOptionalAttribute
1 parent c2b9b8e commit 81153a2

File tree

7 files changed

+139
-290
lines changed

7 files changed

+139
-290
lines changed

src/AbstractElement.php

Lines changed: 22 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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;
@@ -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

src/Chunk.php

Lines changed: 25 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SimpleSAML\XML\Exception\MissingAttributeException;
1010
use SimpleSAML\XML\Exception\SchemaViolationException;
1111
use SimpleSAML\XML\SerializableElementTrait;
12+
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
1213

1314
use function in_array;
1415
use function intval;
@@ -158,120 +159,53 @@ public function getQualifiedName(): string
158159

159160

160161
/**
162+
* Get the value of an attribute from a given element.
163+
*
161164
* @param \DOMElement $xml The element where we should search for the attribute.
162165
* @param string $name The name of the attribute.
163-
* @return string
166+
* @param string $type The type of the attribute value.
167+
* @return \SimpleSAML\XML\Type\ValueTypeInterface
164168
*
165169
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
166170
*/
167-
public static function getAttribute(DOMElement $xml, string $name): string
168-
{
171+
public static function getAttribute(
172+
DOMElement $xml,
173+
string $name,
174+
string $type = StringValue::class,
175+
): ValueTypeInterface {
176+
Assert::isAOf($type, ValueTypeInterface::class);
177+
169178
Assert::true(
170179
$xml->hasAttribute($name),
171180
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
172181
MissingAttributeException::class,
173182
);
174183

175-
return $xml->getAttribute($name);
184+
$value = $xml->getAttribute($name);
185+
return $type::fromString($value);
176186
}
177187

178188

179189
/**
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
190+
* Get the value of an attribute from a given element.
199191
*
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-
/**
218192
* @param \DOMElement $xml The element where we should search for the attribute.
219193
* @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);
255-
}
256-
257-
258-
/**
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)
265-
*
266-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
194+
* @param string $type The type of the attribute value.
195+
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
196+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
267197
*/
268-
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
269-
{
198+
public static function getOptionalAttribute(
199+
DOMElement $xml,
200+
string $name,
201+
string $type = StringValue::class,
202+
?ValueTypeInterface $default = null,
203+
): ?ValueTypeInterface {
270204
if (!$xml->hasAttribute($name)) {
271205
return $default;
272206
}
273207

274-
return self::getIntegerAttribute($xml, $name);
208+
return self::getAttribute($xml, $name, $type);
275209
}
276210

277211

src/ElementInterface.php

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\XML;
66

77
use DOMElement;
8+
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
89

910
/**
1011
* interface class to be implemented by all the classes that represent an XML element
@@ -26,64 +27,22 @@ public function getQualifiedName(): string;
2627
*
2728
* @param \DOMElement $xml The element where we should search for the attribute.
2829
* @param string $name The name of the attribute.
29-
* @return string
30+
* @param string $type The type of the attribute value
31+
* @return \SimpleSAML\XML\ValueTypeInterface
3032
*
3133
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
3234
*/
33-
public static function getAttribute(DOMElement $xml, string $name): string;
35+
public static function getAttribute(DOMElement $xml, string $name, string $type = StringValue::class): ValueTypeInterface;
3436

3537

3638
/**
3739
* Get the value of an attribute from a given element.
3840
*
3941
* @param \DOMElement $xml The element where we should search for the attribute.
4042
* @param string $name The name of the attribute.
43+
* @param string $type The type of the attribute value
4144
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
42-
* @return string|null
43-
*/
44-
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string;
45-
46-
47-
/**
48-
* @param \DOMElement $xml The element where we should search for the attribute.
49-
* @param string $name The name of the attribute.
50-
* @return bool
51-
*
52-
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
53-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
54-
*/
55-
public static function getBooleanAttribute(DOMElement $xml, string $name): bool;
56-
57-
58-
/**
59-
* @param \DOMElement $xml The element where we should search for the attribute.
60-
* @param string $name The name of the attribute.
61-
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
62-
* @return bool|null
63-
*
64-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
65-
*/
66-
public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool;
67-
68-
69-
/**
70-
* @param \DOMElement $xml The element where we should search for the attribute.
71-
* @param string $name The name of the attribute.
72-
* @return int
73-
*
74-
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
75-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
76-
*/
77-
public static function getIntegerAttribute(DOMElement $xml, string $name): int;
78-
79-
80-
/**
81-
* @param \DOMElement $xml The element where we should search for the attribute.
82-
* @param string $name The name of the attribute.
83-
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
84-
* @return int|null
85-
*
86-
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
45+
* @return \SimpleSAML\XML\Type\ValueInterfaceType|null
8746
*/
88-
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int;
47+
public static function getOptionalAttribute(DOMElement $xml, string $name, string $type = StringValue::class, ?ValueTypeInterface $default = null): ?ValueTypeInterface;
8948
}

0 commit comments

Comments
 (0)