Skip to content

Commit 82913ba

Browse files
committed
Restructure DOMDocument
1 parent 45595d7 commit 82913ba

25 files changed

+142
-137
lines changed

phpstan-dev.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 9
2+
level: 6
33
paths:
44
- tests

src/AbstractElement.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DOMElement;
88
use RuntimeException;
99
use SimpleSAML\XML\Assert\Assert;
10+
use SimpleSAML\XML\DOM\DOMDocument;
1011
use SimpleSAML\XML\Exception\MissingAttributeException;
1112
use SimpleSAML\XML\Exception\SchemaViolationException;
1213
use SimpleSAML\XML\SerializableElementTrait;
@@ -40,7 +41,7 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
4041
$namespace = static::getNamespaceURI();
4142

4243
if ($parent === null) {
43-
$parent = DOMDocumentFactory::create();
44+
$parent = DOMDocument::create();
4445
$e = $parent->createElementNS($namespace, $qualifiedName);
4546
} else {
4647
$doc = $parent->ownerDocument;
@@ -57,10 +58,11 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
5758
/**
5859
* Get the value of an attribute from a given element.
5960
*
60-
* @param \DOMElement $xml The element where we should search for the attribute.
61-
* @param string $name The name of the attribute.
62-
* @param string $type The type of the attribute value.
63-
* @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
6466
*
6567
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
6668
*/
@@ -88,11 +90,13 @@ public static function getAttribute(
8890
/**
8991
* Get the value of an attribute from a given element.
9092
*
91-
* @param \DOMElement $xml The element where we should search for the attribute.
92-
* @param string $name The name of the attribute.
93-
* @param string $type The type of the attribute value.
94-
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
95-
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
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.
97+
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
98+
* The default to return in case the attribute does not exist and it is optional.
99+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null)
96100
*/
97101
public static function getOptionalAttribute(
98102
DOMElement $xml,
@@ -152,7 +156,7 @@ public static function getChildrenOfClass(DOMElement $parent): array
152156
&& $node->localName === static::getLocalName()
153157
) {
154158
// Normalize the DOMElement by importing it into a clean empty document
155-
$newDoc = DOMDocumentFactory::create();
159+
$newDoc = DOMDocument::create();
156160
/** @var \DOMElement $node */
157161
$node = $newDoc->appendChild($newDoc->importNode($node, true));
158162

src/Chunk.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use DOMElement;
88
use SimpleSAML\XML\Assert\Assert;
9-
use SimpleSAML\XML\DOMDocument;
9+
use SimpleSAML\XML\DOM\DOMDocument;
1010
use SimpleSAML\XML\Exception\MissingAttributeException;
1111
use SimpleSAML\XML\Exception\SchemaViolationException;
1212
use SimpleSAML\XML\SerializableElementTrait;
@@ -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,
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace SimpleSAML\XML;
5+
namespace SimpleSAML\XML\DOM;
66

7-
use DOMDocument;
7+
use DOMDocument as BaseDOMDocument;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\IOException;
1010
use SimpleSAML\XML\Exception\RuntimeException;
@@ -20,7 +20,7 @@
2020
/**
2121
* @package simplesamlphp/xml-common
2222
*/
23-
final class DOMDocumentFactory
23+
final class DOMDocument extends BaseDOMDocument
2424
{
2525
/**
2626
* @var non-negative-int
@@ -33,12 +33,12 @@ final class DOMDocumentFactory
3333
* @param string $xml
3434
* @param non-negative-int $options
3535
*
36-
* @return \DOMDocument
36+
* @return \SimpleSAML\XML\DOM\DOMDocument
3737
*/
3838
public static function fromString(
3939
string $xml,
4040
int $options = self::DEFAULT_OPTIONS,
41-
): DOMDocument {
41+
): static {
4242
libxml_set_external_entity_loader(null);
4343
Assert::notWhitespaceOnly($xml);
4444
Assert::notRegex(
@@ -86,13 +86,13 @@ public static function fromString(
8686
* @param string $file
8787
* @param non-negative-int $options
8888
*
89-
* @return \DOMDocument
89+
* @return \SimpleSAML\XML\DOM\DOMDocument
9090
*/
9191
public static function fromFile(
9292
string $file,
9393
?string $schemaFile = null,
9494
int $options = self::DEFAULT_OPTIONS,
95-
): DOMDocument {
95+
): static {
9696
error_clear_last();
9797
$xml = @file_get_contents($file);
9898
if ($xml === false) {
@@ -110,10 +110,10 @@ public static function fromFile(
110110
/**
111111
* @param string $version
112112
* @param string $encoding
113-
* @return \DOMDocument
113+
* @return static
114114
*/
115-
public static function create(string $version = '1.0', string $encoding = 'UTF-8'): DOMDocument
115+
public static function create(string $version = '1.0', string $encoding = 'UTF-8'): static
116116
{
117-
return new DOMDocument($version, $encoding);
117+
return new static($version, $encoding);
118118
}
119119
}

src/ElementInterface.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public function getQualifiedName(): string;
2525
/**
2626
* Get the value of an attribute from a given element.
2727
*
28-
* @param \DOMElement $xml The element where we should search for the attribute.
29-
* @param string $name The name of the attribute.
30-
* @param string $type The type of the attribute value
31-
* @return \SimpleSAML\XML\Type\ValueTypeInterface
28+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
29+
* @param \DOMElement $xml The element where we should search for the attribute.
30+
* @param string $name The name of the attribute.
31+
* @param class-string<T> $type The type of the attribute value.
32+
* @return T
3233
*
3334
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
3435
*/
@@ -38,12 +39,13 @@ public static function getAttribute(DOMElement $xml, string $name, string $type
3839
/**
3940
* Get the value of an attribute from a given element.
4041
*
41-
* @param \DOMElement $xml The element where we should search for the attribute.
42-
* @param string $name The name of the attribute.
43-
* @param string $type The type of the attribute value
42+
* @template T of \SimpleSAML\XML\Type\ValueTypeInterface
43+
* @param \DOMElement $xml The element where we should search for the attribute.
44+
* @param string $name The name of the attribute.
45+
* @param class-string<T> $type The type of the attribute value.
4446
* @param \SimpleSAML\XML\Type\ValueTypeInterface|null $default
4547
* The default to return in case the attribute does not exist and it is optional.
46-
* @return \SimpleSAML\XML\Type\ValueTypeInterface|null
48+
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? T : T|null)
4749
*/
4850
public static function getOptionalAttribute(DOMElement $xml, string $name, string $type = StringValue::class, ?ValueTypeInterface $default = null): ?ValueTypeInterface;
4951
}

src/SerializableElementTrait.php

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

77
use DOMElement;
8-
use SimpleSAML\XML\DOMDocumentFactory;
8+
use SimpleSAML\XML\DOM\DOMDocument;
99

1010
use function array_pop;
1111
use function get_object_vars;
@@ -38,7 +38,7 @@ public function __toString(): string
3838

3939
$xmlString = $xml->ownerDocument->saveXML();
4040

41-
$doc = DOMDocumentFactory::fromString($xmlString);
41+
$doc = DOMDocument::fromString($xmlString);
4242
$doc->formatOutput = $this->formatOutput;
4343

4444
return $doc->saveXML($doc->firstChild);
@@ -70,7 +70,7 @@ public function __serialize(): array
7070
public function __unserialize(array $serialized): void
7171
{
7272
$xml = static::fromXML(
73-
DOMDocumentFactory::fromString(array_pop($serialized))->documentElement,
73+
DOMDocument::fromString(array_pop($serialized))->documentElement,
7474
);
7575

7676
$vars = get_object_vars($xml);

src/Type/AbstractValueType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace SimpleSAML\XML\Type;
66

7+
use Stringable;
8+
79
/**
810
* Abstract class to be implemented by all types
911
*
1012
* @package simplesamlphp/xml-common
1113
*/
12-
abstract class AbstractValueType implements ValueTypeInterface
14+
abstract class AbstractValueType implements Stringable, ValueTypeInterface
1315
{
1416
/**
1517
* Set the value for this type.

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', strval($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
}

0 commit comments

Comments
 (0)