Passed
Push — master ( 22d70f...de2862 )
by Tim
02:15
created

Object::isEmptyElement()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 5
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 7
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
13
/**
14
 * Class representing a ds:Object element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class Object extends AbstractDsElement
19
{
20
    use ExtendableElementTrait;
21
22
    /**
23
     * The Id.
24
     *
25
     * @var string|null
26
     */
27
    protected ?string $Id;
28
29
    /**
30
     * The MIME type.
31
     *
32
     * @var string|null
33
     */
34
    protected ?string $MimeType;
35
36
    /**
37
     * The encoding.
38
     *
39
     * @var string|null
40
     */
41
    protected ?string $Encoding;
42
43
44
    /**
45
     * Initialize a ds:Object element.
46
     *
47
     * @param string|null $Id
48
     * @param string|null $MimeType
49
     * @param string|null $Encoding
50
     * @param \SimpleSAML\XML\XMLElementInterface[] $elements
51
     */
52
    public function __construct(
53
        ?string $Id = null,
54
        ?string $MimeType = null,
55
        ?string $Encoding = null,
56
        array $elements = []
57
    ) {
58
        $this->setId($Id);
59
        $this->setMimeType($MimeType);
60
        $this->setEncoding($Encoding);
61
        $this->setElements($elements);
62
    }
63
64
65
    /**
66
     * Collect the value of the Id-property
67
     *
68
     * @return string|null
69
     */
70
    public function getId(): ?string
71
    {
72
        return $this->Id;
73
    }
74
75
76
    /**
77
     * Set the value of the Id-property
78
     *
79
     * @param string $Id
80
     */
81
    private function setId(?string $Id): void
82
    {
83
        $this->Id = $Id;
84
    }
85
86
87
    /**
88
     * Collect the value of the MimeType-property
89
     *
90
     * @return string|null
91
     */
92
    public function getMimeType(): ?string
93
    {
94
        return $this->MimeType;
95
    }
96
97
98
    /**
99
     * Set the value of the MimeType-property
100
     *
101
     * @param string|null $MimeType
102
     */
103
    private function setMimeType(?string $MimeType): void
104
    {
105
        $this->MimeType = $MimeType;
106
    }
107
108
109
    /**
110
     * Collect the value of the Encoding-property
111
     *
112
     * @return string|null
113
     */
114
    public function getEncoding(): ?string
115
    {
116
        return $this->Encoding;
117
    }
118
119
120
    /**
121
     * Set the value of the Encoding-property
122
     *
123
     * @param string|null $Encoding
124
     */
125
    private function setEncoding(?string $Encoding): void
126
    {
127
        $this->Encoding = $Encoding;
128
    }
129
130
131
    /**
132
     * Test if an object, at the state it's in, would produce an empty XML-element
133
     *
134
     * @return bool
135
     */
136
    public function isEmptyElement(): bool
137
    {
138
        return (
139
            empty($this->elements)
140
            && empty($this->Id)
141
            && empty($this->MimeType)
142
            && empty($this->Encoding)
143
        );
144
    }
145
146
147
    /**
148
     * Convert XML into a ds:Object
149
     *
150
     * @param \DOMElement $xml The XML element we should load
151
     * @return self
152
     *
153
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
154
     *   If the qualified name of the supplied element is wrong
155
     */
156
    public static function fromXML(DOMElement $xml): object
157
    {
158
        Assert::same($xml->localName, 'Object', InvalidDOMElementException::class);
159
        Assert::same($xml->namespaceURI, Object::NS, InvalidDOMElementException::class);
160
161
        $Id = Object::getAttribute($xml, 'Id');
162
        $MimeType = Object::getAttribute($xml, 'MimeType');
163
        $Encoding = Object::getAttribute($xml, 'Encoding');
164
165
        $elements = [];
166
        foreach ($xml->childNodes as $elt) {
167
            if (!($elt instanceof DOMElement)) {
168
                continue;
169
            }
170
171
            $elements[] = new Chunk($elt);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\Chunk was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
172
        }
173
174
        return new self($Id, $MimeType, $Encoding, $elements);
175
    }
176
177
178
    /**
179
     * Convert this ds:Object element to XML.
180
     *
181
     * @param \DOMElement|null $parent The element we should append this ds:Object element to.
182
     * @return \DOMElement
183
     */
184
    public function toXML(DOMElement $parent = null): DOMElement
185
    {
186
        $e = $this->instantiateParentElement($parent);
187
        $e->setAttribute('Id', $this->Id);
188
        $e->setAttribute('MimeType', $this->MimeType);
189
        $e->setAttribute('Encoding', $this->Encoding);
190
191
        foreach ($this->elements as $elt) {
192
            $elt->toXML($e);
193
        }
194
195
        return $e;
196
    }
197
}
198