Skip to content

Commit 9edf3c6

Browse files
committed
Migrate Attribute to use value type
1 parent e3d8613 commit 9edf3c6

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

src/Attribute.php

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

77
use DOMAttr;
88
use DOMElement;
9-
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Assert\Assert;
10+
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};
1011

1112
use function array_keys;
13+
use function strval;
1214

1315
/**
1416
* Class to represent an arbitrary namespaced attribute.
@@ -23,20 +25,19 @@ final class Attribute implements ArrayizableElementInterface
2325
* @param string|null $namespaceURI
2426
* @param string|null $namespacePrefix
2527
* @param string $attrName
26-
* @param string $attrValue
28+
* @param \SimpleSAML\XML\Type\ValueTypeInterface $attrValue
2729
*/
2830
public function __construct(
2931
protected ?string $namespaceURI,
3032
protected ?string $namespacePrefix,
3133
protected string $attrName,
32-
protected string $attrValue,
34+
protected ValueTypeInterface $attrValue,
3335
) {
34-
Assert::nullOrStringNotEmpty($namespaceURI);
36+
Assert::nullOrValidAnyURI($namespaceURI);
3537
if ($namespaceURI !== null) {
36-
Assert::stringNotEmpty($namespacePrefix);
38+
Assert::nullOrValidNCName($namespacePrefix);
3739
}
38-
Assert::stringNotEmpty($attrName);
39-
Assert::string($attrValue);
40+
Assert::validNCName($attrName);
4041
}
4142

4243

@@ -76,9 +77,9 @@ public function getAttrName(): string
7677
/**
7778
* Collect the value of the value-property
7879
*
79-
* @return string
80+
* @return \SimpleSAML\XML\Type\ValueTypeInterface
8081
*/
81-
public function getAttrValue(): string
82+
public function getAttrValue(): ValueTypeInterface
8283
{
8384
return $this->attrValue;
8485
}
@@ -92,7 +93,7 @@ public function getAttrValue(): string
9293
*/
9394
public static function fromXML(DOMAttr $attr): static
9495
{
95-
return new static($attr->namespaceURI, $attr->prefix, $attr->localName, $attr->value);
96+
return new static($attr->namespaceURI, $attr->prefix, $attr->localName, StringValue::fromString($attr->value));
9697
}
9798

9899

@@ -110,7 +111,7 @@ public function toXML(DOMElement $parent): DOMElement
110111
!in_array($this->getNamespacePrefix(), ['', null])
111112
? ($this->getNamespacePrefix() . ':' . $this->getAttrName())
112113
: $this->getAttrName(),
113-
$this->getAttrValue(),
114+
strval($this->getAttrValue()),
114115
);
115116

116117
return $parent;
@@ -120,7 +121,12 @@ public function toXML(DOMElement $parent): DOMElement
120121
/**
121122
* Create a class from an array
122123
*
123-
* @param array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $data
124+
* @param array{
125+
* namespaceURI: string,
126+
* namespacePrefix: string|null,
127+
* attrName: string,
128+
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface,
129+
* } $data
124130
* @return static
125131
*/
126132
public static function fromArray(array $data): static
@@ -157,10 +163,10 @@ private static function processArrayContents(array $data): array
157163
Assert::keyExists($data, 'attrname');
158164
Assert::keyExists($data, 'attrvalue');
159165

160-
Assert::nullOrStringNotEmpty($data['namespaceuri']);
161-
Assert::string($data['namespaceprefix']);
162-
Assert::stringNotEmpty($data['attrname']);
163-
Assert::string($data['attrvalue']);
166+
Assert::nullOrValidAnyURI($data['namespaceuri']);
167+
Assert::nullOrValidNCName($data['namespaceprefix']);
168+
Assert::nullOrValidNCName($data['attrname']);
169+
Assert::isAOf($data['attrvalue'], ValueTypeInterface::class);
164170

165171
return [
166172
'namespaceURI' => $data['namespaceuri'],
@@ -174,7 +180,12 @@ private static function processArrayContents(array $data): array
174180
/**
175181
* Create an array from this class
176182
*
177-
* @return array{attrName: string, attrValue: string, namespacePrefix: string, namespaceURI: null|string}
183+
* @return array{
184+
* attrName: string,
185+
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface,
186+
* namespacePrefix: string,
187+
* namespaceURI: null|string,
188+
* }
178189
*/
179190
public function toArray(): array
180191
{

src/ExtendableAttributesTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SimpleSAML\Assert\Assert;
1010
use SimpleSAML\XML\Attribute;
1111
use SimpleSAML\XML\Constants as C;
12+
use SimpleSAML\XML\Type\StringValue;
1213
use SimpleSAML\XML\XsNamespace as NS;
1314

1415
use function array_diff;
@@ -115,7 +116,12 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array|null
115116
continue;
116117
}
117118

118-
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
119+
$attributes[] = new Attribute(
120+
$a->namespaceURI,
121+
$a->prefix,
122+
$a->localName,
123+
StringValue::fromString($a->nodeValue),
124+
);
119125
}
120126
} else {
121127
// Array must be non-empty and cannot contain ##any or ##other
@@ -141,7 +147,12 @@ protected static function getAttributesNSFromXML(DOMElement $xml, NS|array|null
141147
continue;
142148
}
143149

144-
$attributes[] = new Attribute($a->namespaceURI, $a->prefix, $a->localName, $a->nodeValue);
150+
$attributes[] = new Attribute(
151+
$a->namespaceURI,
152+
$a->prefix,
153+
$a->localName,
154+
StringValue::fromString($a->nodeValue),
155+
);
145156
}
146157
}
147158

tests/XML/AttributeTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use SimpleSAML\XML\Attribute;
1111
use SimpleSAML\XML\DOMDocumentFactory;
1212
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
13+
use SimpleSAML\XML\Type\StringValue;
14+
15+
use function strval;
1316

1417
/**
1518
* Class \SimpleSAML\XML\AttributeTest
@@ -40,7 +43,7 @@ public static function setUpBeforeClass(): void
4043
'namespaceURI' => 'urn:x-simplesamlphp:phpunit',
4144
'namespacePrefix' => 'ssp',
4245
'attrName' => 'test1',
43-
'attrValue' => 'testvalue1',
46+
'attrValue' => StringValue::fromString('testvalue1'),
4447
];
4548
}
4649

@@ -53,7 +56,7 @@ public function testMarshallingArray(): void
5356
'urn:x-simplesamlphp:phpunit',
5457
'ssp',
5558
'test1',
56-
'testvalue1',
59+
StringValue::fromString('testvalue1'),
5760
);
5861

5962
$this->assertEquals(
@@ -67,7 +70,14 @@ public function testMarshallingArray(): void
6770
*/
6871
public function testUnmarshalling(): void
6972
{
70-
/** @var array{namespaceURI: string, namespacePrefix: string|null, attrName: string, attrValue: mixed} $arrayRepresentation */
73+
/**
74+
* @var array{
75+
* namespaceURI: string,
76+
* namespacePrefix: string|null,
77+
* attrName: string,
78+
* attrValue: \SimpleSAML\XML\Type\ValueTypeInterface
79+
* } $arrayRepresentation
80+
*/
7181
$arrayRepresentation = self::$arrayRepresentation;
7282
$extendableAttribute = Attribute::fromArray($arrayRepresentation);
7383
$this->assertEquals(
@@ -85,7 +95,7 @@ public function testMarshallingXML(): void
8595
'urn:x-simplesamlphp:phpunit',
8696
'ssp',
8797
'test1',
88-
'testvalue1',
98+
StringValue::fromString('testvalue1'),
8999
);
90100

91101
$doc = DOMDocumentFactory::fromString('<root />');

tests/XML/ExtendableAttributesTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use SimpleSAML\XML\DOMDocumentFactory;
1212
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1313
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
14+
use SimpleSAML\XML\Type\StringValue;
1415

1516
use function dirname;
17+
use function strval;
1618

1719
/**
1820
* Class \SimpleSAML\XML\ExtendableAttributesTest
@@ -45,9 +47,9 @@ public function testMarshalling(): void
4547
{
4648
$extendableElement = new ExtendableAttributesElement(
4749
[
48-
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1'),
49-
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', 'testval2'),
50-
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', 'testval3'),
50+
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', StringValue::fromString('testval1')),
51+
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr2', StringValue::fromString('testval2')),
52+
new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr3', StringValue::fromString('testval3')),
5153
],
5254
);
5355

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

7779
$this->assertEquals($attributes[1]->getNamespaceURI(), 'urn:x-simplesamlphp:namespace');
7880
$this->assertEquals($attributes[1]->getNamespacePrefix(), 'ssp');
7981
$this->assertEquals($attributes[1]->getAttrName(), 'attr2');
80-
$this->assertEquals($attributes[1]->getAttrValue(), 'testval2');
82+
$this->assertEquals(strval($attributes[1]->getAttrValue()), 'testval2');
8183
}
8284
}

tests/XML/ExtendableAttributesTraitTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SimpleSAML\XML\ExtendableAttributesTrait;
1313
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1414
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
15+
use SimpleSAML\XML\Type\StringValue;
1516
use SimpleSAML\XML\XsNamespace as NS;
1617

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

43-
self::$target = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'some', 'targetValue');
44+
self::$target = new Attribute(
45+
'urn:x-simplesamlphp:namespace',
46+
'ssp',
47+
'some',
48+
StringValue::fromString('targetValue'),
49+
);
4450

45-
self::$other = new Attribute('urn:custom:dummy', 'dummy', 'some', 'dummyValue');
51+
self::$other = new Attribute('urn:custom:dummy', 'dummy', 'some', StringValue::fromString('dummyValue'));
4652
}
4753

4854

0 commit comments

Comments
 (0)