Passed
Pull Request — master (#19)
by Tim
03:46 queued 01:44
created

DsObject::getMimeType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Constants;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
14
15
/**
16
 * Class representing a ds:Object element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class DsObject extends AbstractDsElement
21
{
22
    use ExtendableElementTrait;
23
24
    /** @var string */
25
    public const LOCALNAME = 'Object';
26
27
    /** @var string */
28
    public const NAMESPACE = Constants::XS_ANY_NS_ANY;
29
30
    /**
31
     * The Id.
32
     *
33
     * @var string|null
34
     */
35
    protected ?string $Id;
36
37
    /**
38
     * The MIME type.
39
     *
40
     * @var string|null
41
     */
42
    protected ?string $MimeType;
43
44
    /**
45
     * The encoding.
46
     *
47
     * @var string|null
48
     */
49
    protected ?string $Encoding;
50
51
52
    /**
53
     * Initialize a ds:Object element.
54
     *
55
     * @param string|null $Id
56
     * @param string|null $MimeType
57
     * @param string|null $Encoding
58
     * @param \SimpleSAML\XML\XMLElementInterface[] $elements
59
     */
60
    public function __construct(
61
        ?string $Id = null,
62
        ?string $MimeType = null,
63
        ?string $Encoding = null,
64
        array $elements = []
65
    ) {
66
        $this->setId($Id);
67
        $this->setMimeType($MimeType);
68
        $this->setEncoding($Encoding);
69
        $this->setElements($elements);
70
    }
71
72
73
    /**
74
     * Collect the value of the Id-property
75
     *
76
     * @return string|null
77
     */
78
    public function getId(): ?string
79
    {
80
        return $this->Id;
81
    }
82
83
84
    /**
85
     * Set the value of the Id-property
86
     *
87
     * @param string $Id
88
     */
89
    private function setId(?string $Id): void
90
    {
91
        $this->Id = $Id;
92
    }
93
94
95
    /**
96
     * Collect the value of the MimeType-property
97
     *
98
     * @return string|null
99
     */
100
    public function getMimeType(): ?string
101
    {
102
        return $this->MimeType;
103
    }
104
105
106
    /**
107
     * Set the value of the MimeType-property
108
     *
109
     * @param string|null $MimeType
110
     */
111
    private function setMimeType(?string $MimeType): void
112
    {
113
        $this->MimeType = $MimeType;
114
    }
115
116
117
    /**
118
     * Collect the value of the Encoding-property
119
     *
120
     * @return string|null
121
     */
122
    public function getEncoding(): ?string
123
    {
124
        return $this->Encoding;
125
    }
126
127
128
    /**
129
     * Set the value of the Encoding-property
130
     *
131
     * @param string|null $Encoding
132
     */
133
    private function setEncoding(?string $Encoding): void
134
    {
135
        Assert::nullOrValidURI($Encoding);
136
        $this->Encoding = $Encoding;
137
    }
138
139
140
    /**
141
     * Test if an object, at the state it's in, would produce an empty XML-element
142
     *
143
     * @return bool
144
     */
145
    public function isEmptyElement(): bool
146
    {
147
        return (
148
            empty($this->elements)
149
            && empty($this->Id)
150
            && empty($this->MimeType)
151
            && empty($this->Encoding)
152
        );
153
    }
154
155
156
    /**
157
     * Convert XML into a ds:Object
158
     *
159
     * @param \DOMElement $xml The XML element we should load
160
     * @return self
161
     *
162
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
163
     *   If the qualified name of the supplied element is wrong
164
     */
165
    public static function fromXML(DOMElement $xml): object
166
    {
167
        Assert::same($xml->localName, 'Object', InvalidDOMElementException::class);
168
        Assert::same($xml->namespaceURI, DsObject::NS, InvalidDOMElementException::class);
169
170
        $Id = DsObject::getAttribute($xml, 'Id');
171
        $MimeType = DsObject::getAttribute($xml, 'MimeType');
172
        $Encoding = DsObject::getAttribute($xml, 'Encoding');
173
174
        $elements = [];
175
        foreach ($xml->childNodes as $elt) {
176
            if (!($elt instanceof DOMElement)) {
177
                continue;
178
            }
179
180
            $elements[] = new Chunk($elt);
181
        }
182
183
        return new self($Id, $MimeType, $Encoding, $elements);
184
    }
185
186
187
    /**
188
     * Convert this ds:Object element to XML.
189
     *
190
     * @param \DOMElement|null $parent The element we should append this ds:Object element to.
191
     * @return \DOMElement
192
     */
193
    public function toXML(DOMElement $parent = null): DOMElement
194
    {
195
        $e = $this->instantiateParentElement($parent);
196
197
        if ($this->Id !== null) {
198
            $e->setAttribute('Id', $this->Id);
199
        }
200
201
        if ($this->MimeType !== null) {
202
            $e->setAttribute('MimeType', $this->MimeType);
203
        }
204
205
        if ($this->Encoding !== null) {
206
            $e->setAttribute('Encoding', $this->Encoding);
207
        }
208
209
        foreach ($this->elements as $elt) {
210
            $elt->toXML($e);
211
        }
212
213
        return $e;
214
    }
215
}
216