66
77use DOMAttr ;
88use DOMElement ;
9- use SimpleSAML \Assert \Assert ;
9+ use SimpleSAML \XML \Assert \Assert ;
10+ use SimpleSAML \XML \Type \{StringValue , ValueTypeInterface };
1011
1112use 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 {
0 commit comments