AbstractDerivedKeyType::isEmptyElement()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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