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