Skip to content

Commit a085de9

Browse files
committed
Restructure getAttribute/getOptionalAttribute
1 parent c2b9b8e commit a085de9

File tree

9 files changed

+159
-303
lines changed

9 files changed

+159
-303
lines changed

src/AbstractElement.php

Lines changed: 22 additions & 101 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

@@ -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

src/Chunk.php

Lines changed: 28 additions & 95 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\DOMDocument;
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,54 @@ public function getQualifiedName(): string
158157

159158

160159
/**
160+
* Get the value of an attribute from a given element.
161+
*
161162
* @param \DOMElement $xml The element where we should search for the attribute.
162163
* @param string $name The name of the attribute.
163-
* @return string
164+
* @param string $type The type of the attribute value.
165+
* @return \SimpleSAML\XML\Type\ValueTypeInterface
164166
*
165167
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
166168
*/
167-
public static function getAttribute(DOMElement $xml, string $name): string
168-
{
169+
public static function getAttribute(
170+
DOMElement $xml,
171+
string $name,
172+
string $type = StringValue::class,
173+
): ValueTypeInterface {
174+
Assert::isAOf($type, ValueTypeInterface::class);
175+
169176
Assert::true(
170177
$xml->hasAttribute($name),
171178
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
172179
MissingAttributeException::class,
173180
);
174181

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);
182+
$value = $xml->getAttribute($name);
183+
return $type::fromString($value);
255184
}
256185

257186

258187
/**
259-
* Get the integer value of an attribute from a given element.
188+
* Get the value of an attribute from a given element.
260189
*
261190
* @param \DOMElement $xml The element where we should search for the attribute.
262191
* @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
192+
* @param string $type The type of the attribute value.
193+
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
194+
* The default to return in case the attribute does not exist and it is optional.
195+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
267196
*/
268-
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
269-
{
197+
public static function getOptionalAttribute(
198+
DOMElement $xml,
199+
string $name,
200+
string $type = StringValue::class,
201+
?ValueTypeInterface $default = null,
202+
): ?ValueTypeInterface {
270203
if (!$xml->hasAttribute($name)) {
271204
return $default;
272205
}
273206

274-
return self::getIntegerAttribute($xml, $name);
207+
return self::getAttribute($xml, $name, $type);
275208
}
276209

277210

@@ -307,7 +240,7 @@ public static function fromXML(DOMElement $xml): static
307240
public function toXML(?DOMElement $parent = null): DOMElement
308241
{
309242
if ($parent === null) {
310-
$doc = DOMDocumentFactory::create();
243+
$doc = DOMDocument::create();
311244
} else {
312245
$doc = $parent->ownerDocument;
313246
Assert::notNull($doc);

0 commit comments

Comments
 (0)