Passed
Push — master ( 2d3781...f48916 )
by Tim
11:07 queued 06:44
created

AbstractDerivedKeyType::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\XML\xenc\ReferenceList;
13
14
use function array_pop;
15
16
/**
17
 * Class representing <xenc11:DerivedKeyType>.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractDerivedKeyType extends AbstractXenc11Element
22
{
23
    /**
24
     * DerivedKey constructor.
25
     *
26
     * @param string|null $recipient
27
     * @param string|null $id
28
     * @param string|null $type
29
     * @param \SimpleSAML\XMLSecurity\XML\xenc11\KeyDerivationMethod|null $keyDerivationMethod
30
     * @param \SimpleSAML\XMLSecurity\XML\xenc\ReferenceList|null $referenceList
31
     * @param \SimpleSAML\XMLSecurity\XML\xenc11\DerivedKeyName|null $derivedKeyName
32
     * @param \SimpleSAML\XMLSecurity\XML\xenc11\MasterKeyName|null $masterKeyName
33
     */
34
    final public function __construct(
35
        protected ?string $recipient = null,
36
        protected ?string $id = null,
37
        protected ?string $type = null,
38
        protected ?KeyDerivationMethod $keyDerivationMethod = null,
39
        protected ?ReferenceList $referenceList = null,
40
        protected ?DerivedKeyName $derivedKeyName = null,
41
        protected ?MasterKeyName $masterKeyName = null,
42
    ) {
43
        Assert::nullOrValidNCName($id, SchemaViolationException::class);
44
        Assert::nullOrValidURI($type, SchemaViolationException::class);
45
    }
46
47
48
    /**
49
     * Get the value of the $recipient property.
50
     *
51
     * @return string|null
52
     */
53
    public function getRecipient(): ?string
54
    {
55
        return $this->recipient;
56
    }
57
58
59
    /**
60
     * Get the value of the $id property.
61
     *
62
     * @return string|null
63
     */
64
    public function getId(): ?string
65
    {
66
        return $this->id;
67
    }
68
69
70
    /**
71
     * Get the value of the $type property.
72
     *
73
     * @return string|null
74
     */
75
    public function getType(): ?string
76
    {
77
        return $this->type;
78
    }
79
80
81
    /**
82
     * Get the value of the $keyDerivationMethod property.
83
     *
84
     * @return \SimpleSAML\XMLSecurity\XML\xenc11\KeyDerivationMethod|null
85
     */
86
    public function getKeyDerivationMethod(): ?KeyDerivationMethod
87
    {
88
        return $this->keyDerivationMethod;
89
    }
90
91
92
    /**
93
     * Get the value of the $referenceList property.
94
     *
95
     * @return \SimpleSAML\XMLSecurity\XML\xenc\ReferenceList|null
96
     */
97
    public function getReferenceList(): ?ReferenceList
98
    {
99
        return $this->referenceList;
100
    }
101
102
103
    /**
104
     * Get the value of the $derivedKeyName property.
105
     *
106
     * @return \SimpleSAML\XMLSecurity\XML\xenc11\DerivedKeyName|null
107
     */
108
    public function getDerivedKeyName(): ?DerivedKeyName
109
    {
110
        return $this->derivedKeyName;
111
    }
112
113
114
    /**
115
     * Get the value of the $masterKeyName property.
116
     *
117
     * @return \SimpleSAML\XMLSecurity\XML\xenc11\MasterKeyName|null
118
     */
119
    public function getMasterKeyName(): ?MasterKeyName
120
    {
121
        return $this->masterKeyName;
122
    }
123
124
125
    /**
126
     * Test if an object, at the state it's in, would produce an empty XML-element
127
     *
128
     * @return bool
129
     */
130
    public function isEmptyElement(): bool
131
    {
132
        return empty($this->getKeyDerivationMethod())
133
            && empty($this->getReferenceList())
134
            && empty($this->getDerivedKeyName())
135
            && empty($this->getMasterKeyName())
136
            && empty($this->getRecipient())
137
            && empty($this->getId())
138
            && empty($this->getType());
139
    }
140
141
142
    /**
143
     * @inheritDoc
144
     *
145
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
146
     *   If the qualified name of the supplied element is wrong
147
     */
148
    public static function fromXML(DOMElement $xml): static
149
    {
150
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
151
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
152
153
        $keyDerivationMethod = KeyDerivationMethod::getChildrenOfClass($xml);
154
        Assert::maxCount($keyDerivationMethod, 1, TooManyElementsException::class);
155
156
        $referenceList = ReferenceList::getChildrenOfClass($xml);
157
        Assert::maxCount($referenceList, 1, TooManyElementsException::class);
158
159
        $derivedKeyName = DerivedKeyName::getChildrenOfClass($xml);
160
        Assert::maxCount($derivedKeyName, 1, TooManyElementsException::class);
161
162
        $masterKeyName = MasterKeyName::getChildrenOfClass($xml);
163
        Assert::maxCount($masterKeyName, 1, TooManyElementsException::class);
164
165
        return new static(
166
            self::getOptionalAttribute($xml, 'Recipient', null),
167
            self::getOptionalAttribute($xml, 'Id', null),
168
            self::getOptionalAttribute($xml, 'Type', null),
169
            array_pop($keyDerivationMethod),
170
            array_pop($referenceList),
171
            array_pop($derivedKeyName),
172
            array_pop($masterKeyName),
173
        );
174
    }
175
176
177
    /**
178
     * @inheritDoc
179
     */
180
    public function toXML(?DOMElement $parent = null): DOMElement
181
    {
182
        $e = $this->instantiateParentElement($parent);
183
184
        if ($this->getRecipient() !== null) {
185
            $e->setAttribute('Recipient', $this->getRecipient());
186
        }
187
188
        if ($this->getId() !== null) {
189
            $e->setAttribute('Id', $this->getId());
190
        }
191
192
        if ($this->getType() !== null) {
193
            $e->setAttribute('Type', $this->getType());
194
        }
195
196
        $this->getKeyDerivationMethod()?->toXML($e);
197
        $this->getReferenceList()?->toXML($e);
198
        $this->getDerivedKeyName()?->toXML($e);
199
        $this->getMasterKeyName()?->toXML($e);
200
201
        return $e;
202
    }
203
}
204