Passed
Pull Request — master (#19)
by Tim
01:53
created

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