Passed
Pull Request — master (#55)
by Tim
02:16
created

KeyInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 17
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\SerializableElementInterface;
13
use SimpleSAML\XML\XsNamespace as NS;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
use SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
19
20
/**
21
 * Class representing a ds:KeyInfo element.
22
 *
23
 * @package simplesamlphp/xml-security
24
 */
25
final class KeyInfo extends AbstractDsElement
26
{
27
    use ExtendableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\KeyInfo: $namespaceURI, $localName, $childNodes
Loading history...
28
29
    /** @var \SimpleSAML\XML\XsNamespace */
30
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
31
32
33
    /**
34
     * Initialize a KeyInfo element.
35
     *
36
     * @param (
37
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyName|
38
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
39
     *     \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
40
     *     \SimpleSAML\XMLSecurity\XML\ds\X509Data|
41
     *     \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference|
42
     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
43
     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
44
     * )[] $info
45
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
46
     * @param string|null $Id
47
     */
48
    public function __construct(
49
        protected array $info,
50
        array $children = [],
51
        protected ?string $Id = null,
52
    ) {
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,
60
            InvalidArgumentException::class,
61
        );
62
        Assert::nullOrValidNCName($Id);
63
64
        $this->setElements($children);
65
    }
66
67
68
    /**
69
     * Collect the value of the Id-property
70
     *
71
     * @return string|null
72
     */
73
    public function getId(): ?string
74
    {
75
        return $this->Id;
76
    }
77
78
79
    /**
80
     * Collect the value of the info-property
81
     *
82
     * @return (
0 ignored issues
show
Documentation Bug introduced by
The doc comment ( at position 1 could not be parsed: the token is null at position 1.
Loading history...
83
     *     \SimpleSAML\XML\SerializableElementInterface|
84
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyName|
85
     *     \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
86
     *     \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
87
     *     \SimpleSAML\XMLSecurity\XML\ds\X509Data|
88
     *     \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference|
89
     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
90
     *     \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
91
     * )[]
92
     */
93
    public function getInfo(): array
94
    {
95
        return array_merge($this->info, $this->getElements());;
96
    }
97
98
99
    /**
100
     * Convert XML into a KeyInfo
101
     *
102
     * @param \DOMElement $xml The XML element we should load
103
     * @return static
104
     *
105
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
106
     *   If the qualified name of the supplied element is wrong
107
     */
108
    public static function fromXML(DOMElement $xml): static
109
    {
110
        Assert::same($xml->localName, 'KeyInfo', InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class);
112
113
        $Id = self::getOptionalAttribute($xml, 'Id', null);
114
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);
135
    }
136
137
138
    /**
139
     * Convert this KeyInfo to XML.
140
     *
141
     * @param \DOMElement|null $parent The element we should append this KeyInfo to.
142
     * @return \DOMElement
143
     */
144
    public function toXML(DOMElement $parent = null): DOMElement
145
    {
146
        $e = $this->instantiateParentElement($parent);
147
148
        if ($this->getId() !== null) {
149
            $e->setAttribute('Id', $this->getId());
150
        }
151
152
        foreach ($this->getInfo() as $elt) {
153
            $elt->toXML($e);
154
        }
155
156
        return $e;
157
    }
158
}
159