Skip to content

Commit

Permalink
Migrate Attribute to use value type
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 16, 2025
1 parent e3d8613 commit 9edf3c6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
45 changes: 28 additions & 17 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use DOMAttr;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};

use function array_keys;
use function strval;

/**
* Class to represent an arbitrary namespaced attribute.
Expand All @@ -23,20 +25,19 @@ final class Attribute implements ArrayizableElementInterface
* @param string|null $namespaceURI
* @param string|null $namespacePrefix
* @param string $attrName
* @param string $attrValue
* @param \SimpleSAML\XML\Type\ValueTypeInterface $attrValue
*/
public function __construct(
protected ?string $namespaceURI,
protected ?string $namespacePrefix,
protected string $attrName,
protected string $attrValue,
protected ValueTypeInterface $attrValue,
) {
Assert::nullOrStringNotEmpty($namespaceURI);
Assert::nullOrValidAnyURI($namespaceURI);
if ($namespaceURI !== null) {
Assert::stringNotEmpty($namespacePrefix);
Assert::nullOrValidNCName($namespacePrefix);
}
Assert::stringNotEmpty($attrName);
Assert::string($attrValue);
Assert::validNCName($attrName);
}


Expand Down Expand Up @@ -76,9 +77,9 @@ public function getAttrName(): string
/**
* Collect the value of the value-property
*
* @return string
* @return \SimpleSAML\XML\Type\ValueTypeInterface
*/
public function getAttrValue(): string
public function getAttrValue(): ValueTypeInterface
{
return $this->attrValue;
}
Expand All @@ -92,7 +93,7 @@ public function getAttrValue(): string
*/
public static function fromXML(DOMAttr $attr): static
{
return new static($attr->namespaceURI, $attr->prefix, $attr->localName, $attr->value);
return new static($attr->namespaceURI, $attr->prefix, $attr->localName, StringValue::fromString($attr->value));
}


Expand All @@ -110,7 +111,7 @@ public function toXML(DOMElement $parent): DOMElement
!in_array($this->getNamespacePrefix(), ['', null])
? ($this->getNamespacePrefix() . ':' . $this->getAttrName())
: $this->getAttrName(),
$this->getAttrValue(),
strval($this->getAttrValue()),
);

return $parent;
Expand All @@ -120,7 +121,12 @@ public function toXML(DOMElement $parent): DOMElement
/**
* Create a class from an array
*
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
* @param array{
* namespaceURI: string,
* namespacePrefix: string|null,
* attrName: string,
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface,
* } $data
* @return static
*/
public static function fromArray(array $data): static
Expand Down Expand Up @@ -157,10 +163,10 @@ private static function processArrayContents(array $data): array
Assert::keyExists($data, 'attrname');
Assert::keyExists($data, 'attrvalue');

Assert::nullOrStringNotEmpty($data['namespaceuri']);
Assert::string($data['namespaceprefix']);
Assert::stringNotEmpty($data['attrname']);
Assert::string($data['attrvalue']);
Assert::nullOrValidAnyURI($data['namespaceuri']);
Assert::nullOrValidNCName($data['namespaceprefix']);
Assert::nullOrValidNCName($data['attrname']);
Assert::isAOf($data['attrvalue'], ValueTypeInterface::class);

return [
'namespaceURI' => $data['namespaceuri'],
Expand All @@ -174,7 +180,12 @@ private static function processArrayContents(array $data): array
/**
* Create an array from this class
*
* @return array{attrName: string, attrValue: string, namespacePrefix: string, namespaceURI: null|string}
* @return array{
* attrName: string,
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface,
* namespacePrefix: string,
* namespaceURI: null|string,
* }
*/
public function toArray(): array
{
Expand Down
15 changes: 13 additions & 2 deletions src/ExtendableAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\Constants as C;
use SimpleSAML\XML\Type\StringValue;
use SimpleSAML\XML\XsNamespace as NS;

use function array_diff;
Expand Down Expand Up @@ -115,7 +116,12 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array|null
continue;
}

$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
$attributes[] = new Attribute(
$a->namespaceURI,
$a->prefix,
$a->localName,
StringValue::fromString($a->nodeValue),
);
}
} else {
// Array must be non-empty and cannot contain ##any or ##other
Expand All @@ -141,7 +147,12 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array|null
continue;
}

$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
$attributes[] = new Attribute(
$a->namespaceURI,
$a->prefix,
$a->localName,
StringValue::fromString($a->nodeValue),
);
}
}

Expand Down
18 changes: 14 additions & 4 deletions tests/XML/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
use SimpleSAML\XML\Type\StringValue;

use function strval;

/**
* Class \SimpleSAML\XML\AttributeTest
Expand Down Expand Up @@ -40,7 +43,7 @@ public static function setUpBeforeClass(): void
'namespaceURI' => 'urn:x-simplesamlphp:phpunit',
'namespacePrefix' => 'ssp',
'attrName' => 'test1',
'attrValue' => 'testvalue1',
'attrValue' => StringValue::fromString('testvalue1'),
];
}

Expand All @@ -53,7 +56,7 @@ public function testMarshallingArray(): void
'urn:x-simplesamlphp:phpunit',
'ssp',
'test1',
'testvalue1',
StringValue::fromString('testvalue1'),
);

$this->assertEquals(
Expand All @@ -67,7 +70,14 @@ public function testMarshallingArray(): void
*/
public function testUnmarshalling(): void
{
/** @var array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $arrayRepresentation */
/**
* @var array{
* namespaceURI: string,
* namespacePrefix: string|null,
* attrName: string,
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface
* } $arrayRepresentation
*/
$arrayRepresentation = self::$arrayRepresentation;
$extendableAttribute = Attribute::fromArray($arrayRepresentation);
$this->assertEquals(
Expand All @@ -85,7 +95,7 @@ public function testMarshallingXML(): void
'urn:x-simplesamlphp:phpunit',
'ssp',
'test1',
'testvalue1',
StringValue::fromString('testvalue1'),
);

$doc = DOMDocumentFactory::fromString('<root />');
Expand Down
12 changes: 7 additions & 5 deletions tests/XML/ExtendableAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\Type\StringValue;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XML\ExtendableAttributesTest
Expand Down Expand Up @@ -45,9 +47,9 @@ public function testMarshalling(): void
{
$extendableElement = new ExtendableAttributesElement(
[
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1'),
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', 'testval2'),
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', 'testval3'),
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', StringValue::fromString('testval1')),
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', StringValue::fromString('testval2')),
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', StringValue::fromString('testval3')),
],
);

Expand All @@ -72,11 +74,11 @@ public function testGetAttributesNSFromXML(): void
$this->assertEquals($attributes[0]->getNamespaceURI(), 'urn:x-simplesamlphp:namespace');
$this->assertEquals($attributes[0]->getNamespacePrefix(), 'ssp');
$this->assertEquals($attributes[0]->getAttrName(), 'attr1');
$this->assertEquals($attributes[0]->getAttrValue(), 'testval1');
$this->assertEquals(strval($attributes[0]->getAttrValue()), 'testval1');

$this->assertEquals($attributes[1]->getNamespaceURI(), 'urn:x-simplesamlphp:namespace');
$this->assertEquals($attributes[1]->getNamespacePrefix(), 'ssp');
$this->assertEquals($attributes[1]->getAttrName(), 'attr2');
$this->assertEquals($attributes[1]->getAttrValue(), 'testval2');
$this->assertEquals(strval($attributes[1]->getAttrValue()), 'testval2');
}
}
12 changes: 9 additions & 3 deletions tests/XML/ExtendableAttributesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XML\Type\StringValue;
use SimpleSAML\XML\XsNamespace as NS;

/**
Expand All @@ -38,11 +39,16 @@ final class ExtendableAttributesTraitTest extends TestCase
*/
public static function setUpBeforeClass(): void
{
self::$local = new Attribute(null, '', 'some', 'localValue');
self::$local = new Attribute(null, '', 'some', StringValue::fromString('localValue'));

self::$target = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'some', 'targetValue');
self::$target = new Attribute(
'urn:x-simplesamlphp:namespace',
'ssp',
'some',
StringValue::fromString('targetValue'),
);

self::$other = new Attribute('urn:custom:dummy', 'dummy', 'some', 'dummyValue');
self::$other = new Attribute('urn:custom:dummy', 'dummy', 'some', StringValue::fromString('dummyValue'));
}


Expand Down

0 comments on commit 9edf3c6

Please sign in to comment.