Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

AbstractEncryptedType::setEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\SchemaViolationException;
10
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
11
12
use function count;
13
14
/**
15
 * Abstract class representing encrypted data.
16
 *
17
 * Note: <xenc:EncryptionProperties> elements are not supported.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractEncryptedType extends AbstractXencElement
22
{
23
    /**
24
     * EncryptedData constructor.
25
     *
26
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherData $cipherData The CipherData object of this EncryptedData.
27
     * @param string|null $id The Id attribute of this object. Optional.
28
     * @param string|null $type The Type attribute of this object. Optional.
29
     * @param string|null $mimeType The MimeType attribute of this object. Optional.
30
     * @param string|null $encoding The Encoding attribute of this object. Optional.
31
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null $encryptionMethod
32
     *   The EncryptionMethod object of this EncryptedData. Optional.
33
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo The KeyInfo object of this EncryptedData. Optional.
34
     */
35
    public function __construct(
36
        protected CipherData $cipherData,
37
        protected ?string $id = null,
38
        protected ?string $type = null,
39
        protected ?string $mimeType = null,
40
        protected ?string $encoding = null,
41
        protected ?EncryptionMethod $encryptionMethod = null,
42
        protected ?KeyInfo $keyInfo = null,
43
    ) {
44
        Assert::nullOrValidNCName($id, SchemaViolationException::class); // Covers the empty string
45
        Assert::nullOrValidURI($type, SchemaViolationException::class); // Covers the empty string
46
        Assert::nullOrValidURI($encoding, SchemaViolationException::class); // Covers the empty string
47
    }
48
49
50
    /**
51
     * Get the CipherData object.
52
     *
53
     * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherData
54
     */
55
    public function getCipherData(): CipherData
56
    {
57
        return $this->cipherData;
58
    }
59
60
61
    /**
62
     * Get the value of the Encoding attribute.
63
     *
64
     * @return string|null
65
     */
66
    public function getEncoding(): ?string
67
    {
68
        return $this->encoding;
69
    }
70
71
72
    /**
73
     * Get the EncryptionMethod object.
74
     *
75
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null
76
     */
77
    public function getEncryptionMethod(): ?EncryptionMethod
78
    {
79
        return $this->encryptionMethod;
80
    }
81
82
83
    /**
84
     * Get the value of the Id attribute.
85
     *
86
     * @return string
87
     */
88
    public function getID(): ?string
89
    {
90
        return $this->id;
91
    }
92
93
94
    /**
95
     * Get the KeyInfo object.
96
     *
97
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
98
     */
99
    public function getKeyInfo(): ?KeyInfo
100
    {
101
        return $this->keyInfo;
102
    }
103
104
105
    /**
106
     * Get the value of the MimeType attribute.
107
     *
108
     * @return string
109
     */
110
    public function getMimeType(): ?string
111
    {
112
        return $this->mimeType;
113
    }
114
115
116
    /**
117
     * Get the value of the Type attribute.
118
     *
119
     * @return string|null
120
     */
121
    public function getType(): ?string
122
    {
123
        return $this->type;
124
    }
125
126
127
    /**
128
     * @inheritDoc
129
     */
130
    public function toXML(DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
134
        $id = $this->getId();
135
        if ($id !== null) {
136
            $e->setAttribute('Id', $id);
137
        }
138
139
        $type = $this->getType();
140
        if ($type !== null) {
141
            $e->setAttribute('Type', $type);
142
        }
143
144
        $mimeType = $this->getMimeType();
145
        if ($mimeType !== null) {
146
            $e->setAttribute('MimeType', $mimeType);
147
        }
148
149
        $encoding = $this->getEncoding();
150
        if ($encoding !== null) {
151
            $e->setAttribute('Encoding', $encoding);
152
        }
153
154
        $this->getEncryptionMethod()?->toXML($e);
155
        $this->getKeyInfo()?->toXML($e);
156
        $this->getCipherData()->toXML($e);
157
158
        return $e;
159
    }
160
}
161