88use  SimpleSAML \Assert \Assert ;
99use  SimpleSAML \XML \Chunk ;
1010use  SimpleSAML \XML \Exception \InvalidDOMElementException ;
11+ use  SimpleSAML \XML \ExtendableElementTrait ;
12+ use  SimpleSAML \XML \SerializableElementInterface ;
13+ use  SimpleSAML \XML \XsNamespace  as  NS ;
1114use  SimpleSAML \XMLSecurity \Constants  as  C ;
1215use  SimpleSAML \XMLSecurity \Exception \InvalidArgumentException ;
1316use  SimpleSAML \XMLSecurity \XML \dsig11 \KeyInfoReference ;
2124 */ 
2225final  class  KeyInfo extends  AbstractDsElement
2326{
27+     use  ExtendableElementTrait;
28+ 
29+     /** @var \SimpleSAML\XML\XsNamespace */ 
30+     public  const  XS_ANY_ELT_NAMESPACE  = NS ::OTHER ;
31+ 
32+ 
2433    /** 
2534     * Initialize a KeyInfo element. 
2635     * 
2736     * @param ( 
28-      *     \SimpleSAML\XML\SerializableElementInterface| 
2937     *     \SimpleSAML\XMLSecurity\XML\ds\KeyName| 
3038     *     \SimpleSAML\XMLSecurity\XML\ds\KeyValue| 
3139     *     \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod| 
@@ -34,29 +42,26 @@ final class KeyInfo extends AbstractDsElement
3442     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData| 
3543     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey 
3644     * )[] $info 
45+      * @param \SimpleSAML\XML\SerializableElementInterface[] $children 
3746     * @param string|null $Id 
3847     */ 
3948    public  function  __construct (
4049        protected  array  $ info ,
50+         array  $ children  = [],
4151        protected  ?string  $ Id  = null ,
4252    ) {
43-         Assert::notEmpty ($ info , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
44-         Assert::maxCount ($ info , C::UNBOUNDED_LIMIT );
45-         Assert::allIsInstanceOfAny (
46-             $ info ,
47-             [
48-                 Chunk::class,
49-                 KeyName::class,
50-                 KeyValue::class,
51-                 RetrievalMethod::class,
52-                 X509Data::class,
53-                 EncryptedData::class,
54-                 EncryptedKey::class,
55-             ],
56-             'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk. ' ,
53+         $ combi  = array_merge ($ info , $ children );
54+ 
55+         Assert::notEmpty ($ combi , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
56+         Assert::maxCount ($ combi , C::UNBOUNDED_LIMIT );
57+         Assert::allIsInstanceOf (
58+             $ combi ,
59+             SerializableElementInterface::class,
5760            InvalidArgumentException::class,
5861        );
5962        Assert::nullOrValidNCName ($ Id );
63+ 
64+         $ this  ->setElements ($ children );
6065    }
6166
6267
@@ -87,7 +92,7 @@ public function getId(): ?string
8792     */ 
8893    public  function  getInfo (): array 
8994    {
90-         return  $ this  ->info ;
95+         return  array_merge ( $ this  ->info ,  $ this -> getElements ()); ;
9196    }
9297
9398
@@ -106,36 +111,27 @@ public static function fromXML(DOMElement $xml): static
106111        Assert::same ($ xml ->namespaceURI , KeyInfo::NS , InvalidDOMElementException::class);
107112
108113        $ Id  = self ::getOptionalAttribute ($ xml , 'Id ' , null );
109-         $ info  = [];
110- 
111-         foreach  ($ xml ->childNodes  as  $ n ) {
112-             if  (!($ n  instanceof  DOMElement)) {
113-                 continue ;
114-             } elseif  ($ n ->namespaceURI  === C::NS_XDSIG ) {
115-                 $ info [] = match  ($ n ->localName ) {
116-                     'KeyName '  => KeyName::fromXML ($ n ),
117-                     'KeyValue '  => KeyValue::fromXML ($ n ),
118-                     'RetrievalMethod '  => RetrievalMethod::fromXML ($ n ),
119-                     'X509Data '  => X509Data::fromXML ($ n ),
120-                     default  => new  Chunk ($ n ),
121-                 };
122-             } elseif  ($ n ->namespaceURI  === C::NS_XDSIG11 ) {
123-                 $ info [] = match  ($ n ->localName ) {
124-                     'KeyInfoReference '  => KeyInfoReference::fromXML ($ n ),
125-                     default  => new  Chunk ($ n ),
126-                 };
127-             } elseif  ($ n ->namespaceURI  === C::NS_XENC ) {
128-                 $ info [] = match  ($ n ->localName ) {
129-                     'EncryptedData '  => EncryptedData::fromXML ($ n ),
130-                     'EncryptedKey '  => EncryptedKey::fromXML ($ n ),
131-                     default  => new  Chunk ($ n ),
132-                 };
133-             } else  {
134-                 $ info [] = new  Chunk ($ n );
135-             }
136-         }
137114
138-         return  new  static ($ info , $ Id );
115+         $ keyName  = KeyName::getChildrenOfClass ($ xml );
116+         $ keyValue  = KeyValue::getChildrenOfClass ($ xml );
117+         $ retrievalMethod  = RetrievalMethod::getChildrenOfClass ($ xml );
118+         $ x509Data  = X509Data::getChildrenOfClass ($ xml );
119+         //$pgpData = PGPData::getChildrenOfClass($xml); 
120+         //$spkiData = SPKIData::getChildrenOfClass($xml); 
121+         //$mgmtData = MgmtData::getChildrenOfClass($xml); 
122+ 
123+         $ info  = array_merge (
124+             $ keyName ,
125+             $ keyValue ,
126+             $ retrievalMethod ,
127+             $ x509Data ,
128+             //$pgpdata, 
129+             //$spkidata, 
130+             //$mgmtdata, 
131+         );
132+ 
133+         $ children  = self ::getChildElementsFromXML ($ xml );
134+         return  new  static ($ info , $ children , $ Id );
139135    }
140136
141137
@@ -153,8 +149,8 @@ public function toXML(DOMElement $parent = null): DOMElement
153149            $ e ->setAttribute ('Id ' , $ this  ->getId ());
154150        }
155151
156-         foreach  ($ this  ->getInfo () as  $ n  ) {
157-             $ n  ->toXML ($ e );
152+         foreach  ($ this  ->getInfo () as  $ elt  ) {
153+             $ elt  ->toXML ($ e );
158154        }
159155
160156        return  $ e ;
0 commit comments