Skip to content

Commit cb29a42

Browse files
committed
Restructure DOMDocument
1 parent bdc53d8 commit cb29a42

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

src/AbstractElement.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
5858
/**
5959
* Get the value of an attribute from a given element.
6060
*
61-
* @param \DOMElement $xml The element where we should search for the attribute.
62-
* @param string $name The name of the attribute.
63-
* @param string $type The type of the attribute value.
64-
* @return \SimpleSAML\XML\Type\ValueTypeInterface
61+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
62+
* @param \DOMElement $xml The element where we should search for the attribute.
63+
* @param string $name The name of the attribute.
64+
* @param class-string<T> $type The type of the attribute value.
65+
* @return T
6566
*
6667
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
6768
*/
@@ -89,12 +90,13 @@ public static function getAttribute(
8990
/**
9091
* Get the value of an attribute from a given element.
9192
*
92-
* @param \DOMElement $xml The element where we should search for the attribute.
93-
* @param string $name The name of the attribute.
94-
* @param string $type The type of the attribute value.
93+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
94+
* @param \DOMElement $xml The element where we should search for the attribute.
95+
* @param string $name The name of the attribute.
96+
* @param class-string<T> $type The type of the attribute value.
9597
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
9698
* The default to return in case the attribute does not exist and it is optional.
97-
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
99+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null)
98100
*/
99101
public static function getOptionalAttribute(
100102
DOMElement $xml,

src/Chunk.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ public function getQualifiedName(): string
159159
/**
160160
* Get the value of an attribute from a given element.
161161
*
162-
* @param \DOMElement $xml The element where we should search for the attribute.
163-
* @param string $name The name of the attribute.
164-
* @param string $type The type of the attribute value.
165-
* @return \SimpleSAML\XML\Type\ValueTypeInterface
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
166167
*
167168
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
168169
*/
@@ -187,12 +188,13 @@ public static function getAttribute(
187188
/**
188189
* Get the value of an attribute from a given element.
189190
*
190-
* @param \DOMElement $xml The element where we should search for the attribute.
191-
* @param string $name The name of the attribute.
192-
* @param string $type The type of the attribute value.
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.
193195
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
194196
* 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)
197+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null)
196198
*/
197199
public static function getOptionalAttribute(
198200
DOMElement $xml,

src/Type/ValueTypeInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @package simplesamlphp/xml-common
1111
*/
12-
interface ValueTypeInterface
12+
interface ValueTypeInterface extends \Stringable
1313
{
1414
/**
1515
* @return string
@@ -28,4 +28,12 @@ public function getRawValue(): string;
2828
* @return \SimpleSAML\XML\Type\ValueTypeInterface
2929
*/
3030
public static function fromString(string $value): ValueTypeInterface;
31+
32+
33+
/**
34+
* Output the value as a string
35+
*
36+
* @return string
37+
*/
38+
public function __toString(): string;
3139
}

tests/Utils/Element.php

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\AbstractElement;
1010
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11-
use SimpleSAML\XML\Type\{BooleanValue, IntegerValue, StringValue};
11+
use SimpleSAML\XML\Type\{BooleanValue, IntegerValue, StringValue, ValueTypeInterface};
1212

1313
use function strval;
1414

@@ -27,26 +27,26 @@ final class Element extends AbstractElement
2727

2828

2929
/**
30-
* @param \SimpleSAML\XML\Type\IntegerValue|null $integer
31-
* @param \SimpleSAML\XML\Type\BooleanValue|null $boolean
32-
* @param \SimpleSAML\XML\Type\StringValue|null $text
33-
* @param \SimpleSAML\XML\Type\StringValue|null $otherText
30+
* @param \SimpleSAML\XML\Type\IntegerValue $integer
31+
* @param \SimpleSAML\XML\Type\BooleanValue $boolean
32+
* @param \SimpleSAML\XML\Type\StringValue $text
33+
* @param \SimpleSAML\XML\Type\StringValue $otherText
3434
*/
3535
public function __construct(
36-
protected ?IntegerValue $integer = null,
37-
protected ?BooleanValue $boolean = null,
38-
protected ?StringValue $text = null,
39-
protected ?StringValue $otherText = null,
36+
protected IntegerValue $integer,
37+
protected BooleanValue $boolean,
38+
protected StringValue $text,
39+
protected StringValue $otherText,
4040
) {
4141
}
4242

4343

4444
/**
4545
* Collect the value of the integer-property
4646
*
47-
* @return \SimpleSAML\XML\Type\IntegerValue|null
47+
* @return \SimpleSAML\XML\Type\IntegerValue
4848
*/
49-
public function getInteger(): ?IntegerValue
49+
public function getInteger(): IntegerValue
5050
{
5151
return $this->integer;
5252
}
@@ -55,9 +55,9 @@ public function getInteger(): ?IntegerValue
5555
/**
5656
* Collect the value of the boolean-property
5757
*
58-
* @return \SimpleSAML\XML\Type\BooleanValue|null
58+
* @return \SimpleSAML\XML\Type\BooleanValue
5959
*/
60-
public function getBoolean(): ?BooleanValue
60+
public function getBoolean(): BooleanValue
6161
{
6262
return $this->boolean;
6363
}
@@ -66,20 +66,20 @@ public function getBoolean(): ?BooleanValue
6666
/**
6767
* Collect the value of the text-property
6868
*
69-
* @return \SimpleSAML\XML\Type\StringValue|null
69+
* @return \SimpleSAML\XML\Type\StringValue
7070
*/
71-
public function getString(): ?StringValue
71+
public function getString(): StringValue
7272
{
7373
return $this->text;
7474
}
7575

7676

7777
/**
78-
* Collect the value of the text2-property
78+
* Collect the value of the otherText-property
7979
*
80-
* @return \SimpleSAML\XML\Type\StringValue|null
80+
* @return \SimpleSAML\XML\Type\StringValue
8181
*/
82-
public function getOtherString(): ?StringValue
82+
public function getOtherString(): StringValue
8383
{
8484
return $this->otherText;
8585
}
@@ -115,21 +115,10 @@ public function toXML(?DOMElement $parent = null): DOMElement
115115
{
116116
$e = $this->instantiateParentElement($parent);
117117

118-
if ($this->getInteger() !== null) {
119-
$e->setAttribute('integer', $this->getInteger());
120-
}
121-
122-
if ($this->getBoolean() !== null) {
123-
$e->setAttribute('boolean', strval($this->getBoolean()));
124-
}
125-
126-
if ($this->getString() !== null) {
127-
$e->setAttribute('text', strval($this->getString()));
128-
}
129-
130-
if ($this->getOtherString() !== null) {
131-
$e->setAttribute('otherText', strval($this->getOtherString()));
132-
}
118+
$e->setAttribute('integer', strval($this->getInteger()));
119+
$e->setAttribute('boolean', strval($this->getBoolean()));
120+
$e->setAttribute('text', strval($this->getString()));
121+
$e->setAttribute('otherText', strval($this->getOtherString()));
133122

134123
return $e;
135124
}

tests/XML/ChunkTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testUnmarshalling(): void
6969
$this->assertFalse($chunk->isEmptyElement());
7070

7171
// Get mandatory attributes
72-
$this->assertEquals('2', (string)$chunk::getAttribute($xml, 'integer', IntegerValue::class));
72+
$this->assertEquals('2', $chunk::getAttribute($xml, 'integer', IntegerValue::class));
7373
$this->assertEquals('false', strval($chunk::getAttribute($xml, 'boolean', BooleanValue::class)));
7474
$this->assertEquals('text', strval($chunk::getAttribute($xml, 'text', StringValue::class)));
7575
$this->assertEquals('otherText', strval($chunk::getAttribute($xml, 'otherText')));

0 commit comments

Comments
 (0)