diff --git a/src/AbstractElement.php b/src/AbstractElement.php
index 73a17d36..8e37ad92 100644
--- a/src/AbstractElement.php
+++ b/src/AbstractElement.php
@@ -10,6 +10,7 @@
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SerializableElementTrait;
+use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
use function array_slice;
use function defined;
@@ -61,22 +62,29 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
- * @return string
+ * @param string $type The type of the attribute value.
+ * @return \SimpleSAML\XML\Type\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
- public static function getAttribute(DOMElement $xml, string $name): string
- {
+ public static function getAttribute(
+ DOMElement $xml,
+ string $name,
+ string $type = StringValue::class,
+ ): ValueTypeInterface {
+ Assert::isAOf($type, ValueTypeInterface::class);
+
$prefix = static::getNamespacePrefix();
$localName = static::getLocalName();
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
Assert::true(
- $xml->hasAttribute($name) && func_num_args() === 2,
+ $xml->hasAttribute($name),
sprintf('Missing \'%s\' attribute on %s.', $name, $qName),
MissingAttributeException::class,
);
- return $xml->getAttribute($name);
+ $value = $xml->getAttribute($name);
+ return $type::fromString($value);
}
@@ -85,105 +93,21 @@ public static function getAttribute(DOMElement $xml, string $name): string
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
+ * @param string $type The type of the attribute value.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is string ? string : string|null)
- */
- public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
- {
- if (!$xml->hasAttribute($name)) {
- return $default;
- }
-
- return self::getAttribute($xml, $name);
- }
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return bool
- *
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
- */
- public static function getBooleanAttribute(DOMElement $xml, string $name): bool
- {
- $value = self::getAttribute($xml, $name);
-
- $prefix = static::getNamespacePrefix();
- $localName = static::getLocalName();
- $qName = $prefix ? ($prefix . ':' . $localName) : $localName;
- Assert::oneOf(
- $value,
- ['0', '1', 'false', 'true'],
- sprintf('The \'%s\' attribute of %s must be a boolean.', $name, $qName),
- );
-
- return in_array($value, ['1', 'true'], true);
- }
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param bool|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is bool ? bool : bool|null)
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
+ * @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
*/
- public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool
- {
- if (!$xml->hasAttribute($name)) {
- return $default;
- }
-
- return self::getBooleanAttribute($xml, $name);
- }
-
-
- /**
- * Get the integer value of an attribute from a given element.
- *
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return int
- *
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
- */
- public static function getIntegerAttribute(DOMElement $xml, string $name): int
- {
- $value = self::getAttribute($xml, $name);
-
- $prefix = static::getNamespacePrefix();
- $localName = static::getLocalName();
- $qName = $prefix ? ($prefix . ':' . $localName) : $localName;
- Assert::numeric(
- $value,
- sprintf('The \'%s\' attribute of %s must be numerical.', $name, $qName),
- );
-
- return intval($value);
- }
-
-
- /**
- * Get the integer value of an attribute from a given element.
- *
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param int|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is int ? int : int|null)
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
- */
- public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
- {
+ public static function getOptionalAttribute(
+ DOMElement $xml,
+ string $name,
+ string $type = StringValue::class,
+ ?ValueTypeInterface $default = null,
+ ): ?ValueTypeInterface {
if (!$xml->hasAttribute($name)) {
return $default;
}
- return self::getIntegerAttribute($xml, $name);
+ return self::getAttribute($xml, $name, $type);
}
diff --git a/src/Chunk.php b/src/Chunk.php
index 8af6eb75..5e88c94a 100644
--- a/src/Chunk.php
+++ b/src/Chunk.php
@@ -9,6 +9,7 @@
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SerializableElementTrait;
+use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
use function in_array;
use function intval;
@@ -158,120 +159,53 @@ public function getQualifiedName(): string
/**
+ * Get the value of an attribute from a given element.
+ *
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
- * @return string
+ * @param string $type The type of the attribute value.
+ * @return \SimpleSAML\XML\Type\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
- public static function getAttribute(DOMElement $xml, string $name): string
- {
+ public static function getAttribute(
+ DOMElement $xml,
+ string $name,
+ string $type = StringValue::class,
+ ): ValueTypeInterface {
+ Assert::isAOf($type, ValueTypeInterface::class);
+
Assert::true(
$xml->hasAttribute($name),
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
MissingAttributeException::class,
);
- return $xml->getAttribute($name);
+ $value = $xml->getAttribute($name);
+ return $type::fromString($value);
}
/**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param string|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is string ? string : null)
- */
- public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
- {
- if (!$xml->hasAttribute($name)) {
- return $default;
- }
-
- return $xml->getAttribute($name);
- }
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return bool
+ * Get the value of an attribute from a given element.
*
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
- */
- public static function getBooleanAttribute(DOMElement $xml, string $name): bool
- {
- $value = self::getAttribute($xml, $name);
-
- Assert::oneOf(
- $value,
- ['0', '1', 'false', 'true'],
- 'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be boolean.',
- );
-
- return in_array($value, ['1', 'true'], true);
- }
-
-
- /**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
- * @param bool|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is bool ? bool : null)
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
- */
- public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool
- {
- if (!$xml->hasAttribute($name)) {
- return $default;
- }
-
- return self::getBooleanAttribute($xml, $name);
- }
-
-
- /**
- * Get the integer value of an attribute from a given element.
- *
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return int
- *
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
- */
- public static function getIntegerAttribute(DOMElement $xml, string $name): int
- {
- $value = self::getAttribute($xml, $name);
-
- Assert::numeric(
- $value,
- 'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be numerical.',
- );
-
- return intval($value);
- }
-
-
- /**
- * Get the integer value of an attribute from a given element.
- *
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param int|null $default The default to return in case the attribute does not exist and it is optional.
- * @return ($default is int ? int : null)
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
+ * @param string $type The type of the attribute value.
+ * @param string|null $default The default to return in case the attribute does not exist and it is optional.
+ * @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
*/
- public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
- {
+ public static function getOptionalAttribute(
+ DOMElement $xml,
+ string $name,
+ string $type = StringValue::class,
+ ?ValueTypeInterface $default = null,
+ ): ?ValueTypeInterface {
if (!$xml->hasAttribute($name)) {
return $default;
}
- return self::getIntegerAttribute($xml, $name);
+ return self::getAttribute($xml, $name, $type);
}
diff --git a/src/ElementInterface.php b/src/ElementInterface.php
index 1478835d..565b86df 100644
--- a/src/ElementInterface.php
+++ b/src/ElementInterface.php
@@ -5,6 +5,7 @@
namespace SimpleSAML\XML;
use DOMElement;
+use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
/**
* interface class to be implemented by all the classes that represent an XML element
@@ -26,11 +27,12 @@ public function getQualifiedName(): string;
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
- * @return string
+ * @param string $type The type of the attribute value
+ * @return \SimpleSAML\XML\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
- public static function getAttribute(DOMElement $xml, string $name): string;
+ public static function getAttribute(DOMElement $xml, string $name, string $type = StringValue::class): ValueTypeInterface;
/**
@@ -38,52 +40,9 @@ public static function getAttribute(DOMElement $xml, string $name): string;
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
+ * @param string $type The type of the attribute value
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
- * @return string|null
- */
- public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string;
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return bool
- *
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
- */
- public static function getBooleanAttribute(DOMElement $xml, string $name): bool;
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param bool|null $default The default to return in case the attribute does not exist and it is optional.
- * @return bool|null
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
- */
- public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool;
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @return int
- *
- * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
- */
- public static function getIntegerAttribute(DOMElement $xml, string $name): int;
-
-
- /**
- * @param \DOMElement $xml The element where we should search for the attribute.
- * @param string $name The name of the attribute.
- * @param int|null $default The default to return in case the attribute does not exist and it is optional.
- * @return int|null
- *
- * @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
+ * @return \SimpleSAML\XML\Type\ValueInterfaceType|null
*/
- public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int;
+ public static function getOptionalAttribute(DOMElement $xml, string $name, string $type = StringValue::class, ?ValueTypeInterface $default = null): ?ValueTypeInterface;
}
diff --git a/tests/Utils/Element.php b/tests/Utils/Element.php
index a0de6ddf..b1b3a10e 100644
--- a/tests/Utils/Element.php
+++ b/tests/Utils/Element.php
@@ -8,6 +8,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
+use SimpleSAML\XML\Type\{BooleanValue, IntegerValue, StringValue};
use function strval;
@@ -26,14 +27,16 @@ final class Element extends AbstractElement
/**
- * @param int|null $integer
- * @param bool|null $boolean
- * @param string|null $text
+ * @param \SimpleSAML\XML\Type\IntegerValue|null $integer
+ * @param \SimpleSAML\XML\Type\BooleanValue|null $boolean
+ * @param \SimpleSAML\XML\Type\StringValue|null $text
+ * @param \SimpleSAML\XML\Type\StringValue|null $otherText
*/
public function __construct(
- protected ?int $integer = null,
- protected ?bool $boolean = null,
- protected ?string $text = null,
+ protected ?IntegerValue $integer = null,
+ protected ?BooleanValue $boolean = null,
+ protected ?StringValue $text = null,
+ protected ?StringValue $otherText = null,
) {
}
@@ -41,9 +44,9 @@ public function __construct(
/**
* Collect the value of the integer-property
*
- * @return int|null
+ * @return \SimpleSAML\XML\Type\IntegerValue|null
*/
- public function getInteger(): ?int
+ public function getInteger(): ?IntegerValue
{
return $this->integer;
}
@@ -52,9 +55,9 @@ public function getInteger(): ?int
/**
* Collect the value of the boolean-property
*
- * @return bool|null
+ * @return \SimpleSAML\XML\Type\BooleanValue|null
*/
- public function getBoolean(): ?bool
+ public function getBoolean(): ?BooleanValue
{
return $this->boolean;
}
@@ -63,14 +66,25 @@ public function getBoolean(): ?bool
/**
* Collect the value of the text-property
*
- * @return string|null
+ * @return \SimpleSAML\XML\Type\StringValue|null
*/
- public function getString(): ?string
+ public function getString(): ?StringValue
{
return $this->text;
}
+ /**
+ * Collect the value of the text2-property
+ *
+ * @return \SimpleSAML\XML\Type\StringValue|null
+ */
+ public function getOtherString(): ?StringValue
+ {
+ return $this->otherText;
+ }
+
+
/**
* Create a class from XML
*
@@ -82,11 +96,12 @@ public static function fromXML(DOMElement $xml): static
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
- $integer = self::getIntegerAttribute($xml, 'integer');
- $boolean = self::getBooleanAttribute($xml, 'boolean');
- $text = self::getAttribute($xml, 'text');
+ $integer = self::getAttribute($xml, 'integer', IntegerValue::class);
+ $boolean = self::getAttribute($xml, 'boolean', BooleanValue::class);
+ $text = self::getAttribute($xml, 'text', StringValue::class);
+ $otherText = self::getAttribute($xml, 'otherText');
- return new static($integer, $boolean, $text);
+ return new static($integer, $boolean, $text, $otherText);
}
@@ -100,16 +115,20 @@ public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
- if ($this->integer !== null) {
- $e->setAttribute('integer', strval($this->integer));
+ if ($this->getInteger() !== null) {
+ $e->setAttribute('integer', strval($this->getInteger()));
+ }
+
+ if ($this->getBoolean() !== null) {
+ $e->setAttribute('boolean', strval($this->getBoolean()));
}
- if ($this->boolean !== null) {
- $e->setAttribute('boolean', $this->boolean ? 'true' : 'false');
+ if ($this->getString() !== null) {
+ $e->setAttribute('text', strval($this->getString()));
}
- if ($this->text !== null) {
- $e->setAttribute('text', $this->text);
+ if ($this->getOtherString() !== null) {
+ $e->setAttribute('otherText', strval($this->getOtherString()));
}
return $e;
diff --git a/tests/XML/AbstractElementTest.php b/tests/XML/AbstractElementTest.php
index 1f870998..3d5c5436 100644
--- a/tests/XML/AbstractElementTest.php
+++ b/tests/XML/AbstractElementTest.php
@@ -11,6 +11,7 @@
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
+use SimpleSAML\XML\Type\{IntegerValue, BooleanValue, StringValue};
use function dirname;
use function strval;
@@ -43,7 +44,12 @@ public static function setUpBeforeClass(): void
*/
public function testMarshalling(): void
{
- $element = new Element(2, false, 'text');
+ $element = new Element(
+ IntegerValue::fromString('2'),
+ BooleanValue::fromString('false'),
+ StringValue::fromString('text'),
+ StringValue::fromString('otherText'),
+ );
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
@@ -60,9 +66,10 @@ public function testUnmarshalling(): void
$elt = self::$xmlRepresentation->documentElement;
$element = Element::fromXML($elt);
- $this->assertEquals(2, $element->getInteger());
- $this->assertEquals(false, $element->getBoolean());
- $this->assertEquals('text', $element->getString());
+ $this->assertEquals('2', strval($element->getInteger()));
+ $this->assertEquals('false', strval($element->getBoolean()));
+ $this->assertEquals('text', strval($element->getString()));
+ $this->assertEquals('otherText', strval($element->getOtherString()));
}
@@ -74,30 +81,33 @@ public function testGetAttribute(): void
$xml = self::$xmlRepresentation->documentElement;
// Get mandatory attributes
- $this->assertEquals('text', Element::getAttribute($xml, 'text'));
- $this->assertFalse(Element::getBooleanAttribute($xml, 'boolean'));
- $this->assertEquals(2, Element::getIntegerAttribute($xml, 'integer'));
+ $this->assertEquals('text', strval(Element::getAttribute($xml, 'text', StringValue::class)));
+ $this->assertEquals('otherText', strval(Element::getAttribute($xml, 'otherText', StringValue::class)));
+ $this->assertEquals('false', strval(Element::getAttribute($xml, 'boolean', BooleanValue::class)));
+ $this->assertEquals('2', strval(Element::getAttribute($xml, 'integer', IntegerValue::class)));
// Get optional attributes
- $this->assertEquals('text', Element::getOptionalAttribute($xml, 'text'));
- $this->assertFalse(Element::getOptionalBooleanAttribute($xml, 'boolean'));
- $this->assertEquals(2, Element::getOptionalIntegerAttribute($xml, 'integer'));
+ $this->assertEquals('text', strval(Element::getOptionalAttribute($xml, 'text', StringValue::Class)));
+ $this->assertEquals('otherText', strval(Element::getOptionalAttribute($xml, 'otherText', StringValue::class)));
+ $this->assertEquals('false', strval(Element::getOptionalAttribute($xml, 'boolean', BooleanValue::class)));
+ $this->assertEquals('2', strval(Element::getOptionalAttribute($xml, 'integer', IntegerValue::class)));
// Get optional non-existing attributes
$this->assertNull(Element::getOptionalAttribute($xml, 'non-existing'));
- $this->assertNull(Element::getOptionalBooleanAttribute($xml, 'non-existing'));
- $this->assertNull(Element::getOptionalIntegerAttribute($xml, 'non-existing'));
+ $this->assertNull(Element::getOptionalAttribute($xml, 'non-existing', StringValue::class));
+ $this->assertNull(Element::getOptionalAttribute($xml, 'non-existing', IntegerValue::class));
// Get optional non-existing attributes with default
- $this->assertEquals('other text', Element::getOptionalAttribute($xml, 'non-existing', 'other text'));
- $this->assertTrue(Element::getOptionalBooleanAttribute($xml, 'non-existing', true));
- $this->assertEquals(3, Element::getOptionalIntegerAttribute($xml, 'non-existing', 3));
+ $this->assertNull(Element::getOptionalAttribute($xml, 'non-existing', StringValue::class, null));
+ $this->assertEquals('other text', strval(Element::getOptionalAttribute($xml, 'non-existing', StringValue::class, StringValue::fromString('other text'))));
+ $this->assertEquals('true', strval(Element::getOptionalAttribute($xml, 'non-existing', BooleanValue::class, BooleanValue::fromString('true'))));
+ $this->assertEquals('3', strval(Element::getOptionalAttribute($xml, 'non-existing', IntegerValue::class, IntegerValue::fromString('3'))));
// Get mandatory non-existing attributes
$this->expectException(MissingAttributeException::class);
Element::getAttribute($xml, 'non-existing');
- Element::getBooleanAttribute($xml, 'non-existing');
- Element::getIntegerAttribute($xml, 'non-existing');
+ Element::getAttribute($xml, 'non-existing', BooleanValue::class);
+ Element::getAttribute($xml, 'non-existing', IntegerValue::class);
}
diff --git a/tests/XML/ChunkTest.php b/tests/XML/ChunkTest.php
index 5893b0cb..a88b1cb8 100644
--- a/tests/XML/ChunkTest.php
+++ b/tests/XML/ChunkTest.php
@@ -10,6 +10,7 @@
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
+use SimpleSAML\XML\Type\{BooleanValue, IntegerValue, StringValue};
use function dirname;
use function strval;
@@ -68,29 +69,31 @@ public function testUnmarshalling(): void
$this->assertFalse($chunk->isEmptyElement());
// Get mandatory attributes
- $this->assertEquals(2, $chunk::getIntegerAttribute($xml, 'integer'));
- $this->assertEquals(false, $chunk::getBooleanAttribute($xml, 'boolean'));
- $this->assertEquals('text', $chunk::getAttribute($xml, 'text'));
+ $this->assertEquals('2', strval($chunk::getAttribute($xml, 'integer', IntegerValue::class)));
+ $this->assertEquals('false', strval($chunk::getAttribute($xml, 'boolean', BooleanValue::class)));
+ $this->assertEquals('text', strval($chunk::getAttribute($xml, 'text', StringValue::class)));
+ $this->assertEquals('otherText', strval($chunk::getAttribute($xml, 'otherText')));
// Get optional attributes
- $this->assertEquals('text', $chunk::getOptionalAttribute($xml, 'text'));
- $this->assertFalse($chunk::getOptionalBooleanAttribute($xml, 'boolean'));
- $this->assertEquals(2, $chunk::getOptionalIntegerAttribute($xml, 'integer'));
+ $this->assertEquals('text', strval($chunk::getOptionalAttribute($xml, 'text')));
+ $this->assertEquals('otherText', strval($chunk::getOptionalAttribute($xml, 'otherText', StringValue::class)));
+ $this->assertEquals('false', strval($chunk::getOptionalAttribute($xml, 'boolean', BooleanValue::class)));
+ $this->assertEquals('2', strval($chunk::getOptionalAttribute($xml, 'integer', IntegerValue::class)));
// Get optional non-existing attributes
$this->assertNull($chunk::getOptionalAttribute($xml, 'non-existing'));
- $this->assertNull($chunk::getOptionalBooleanAttribute($xml, 'non-existing'));
- $this->assertNull($chunk::getOptionalIntegerAttribute($xml, 'non-existing'));
+ $this->assertNull($chunk::getOptionalAttribute($xml, 'non-existing', BooleanValue::class));
+ $this->assertNull($chunk::getOptionalAttribute($xml, 'non-existing', IntegerValue::class));
// Get optional non-existing attributes with default
- $this->assertEquals('other text', $chunk::getOptionalAttribute($xml, 'non-existing', 'other text'));
- $this->assertTrue($chunk::getOptionalBooleanAttribute($xml, 'non-existing', true));
- $this->assertEquals(3, $chunk::getOptionalIntegerAttribute($xml, 'non-existing', 3));
+ $this->assertEquals('other text', $chunk::getOptionalAttribute($xml, 'non-existing', StringValue::class, StringValue::fromString('other text')));
+ $this->assertEquals('true', $chunk::getOptionalAttribute($xml, 'non-existing', BooleanValue::class, BooleanValue::fromString('true')));
+ $this->assertEquals('3', $chunk::getOptionalAttribute($xml, 'non-existing', IntegerValue::class, IntegerValue::fromString('3')));
// Get mandatory non-existing attributes
$this->expectException(MissingAttributeException::class);
$chunk::getAttribute($xml, 'non-existing');
- $chunk::getBooleanAttribute($xml, 'non-existing');
- $chunk::getIntegerAttribute($xml, 'non-existing');
+ $chunk::getAttribute($xml, 'non-existing', BooleanValue::class);
+ $chunk::getAttribute($xml, 'non-existing', IntegerValue::class);
}
}
diff --git a/tests/resources/xml/ssp_Element.xml b/tests/resources/xml/ssp_Element.xml
index f32f693a..a7f206a6 100644
--- a/tests/resources/xml/ssp_Element.xml
+++ b/tests/resources/xml/ssp_Element.xml
@@ -1 +1 @@
-
+