DsObject::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 11
c 1
b 0
f 1
nc 24
nop 1
dl 0
loc 23
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\Exception\InvalidDOMElementException;
9
use SimpleSAML\XML\ExtendableElementTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XML\XsNamespace as NS;
13
use SimpleSAML\XMLSecurity\Assert\Assert;
14
15
/**
16
 * Class representing a ds:Object element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class DsObject extends AbstractDsElement implements SchemaValidatableElementInterface
21
{
22
    use ExtendableElementTrait;
23
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\DsObject: $message, $line
Loading history...
24
25
    /** @var string */
26
    public const LOCALNAME = 'Object';
27
28
    /** @var \SimpleSAML\XML\XsNamespace */
29
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
30
31
32
    /**
33
     * Initialize a ds:Object element.
34
     *
35
     * @param string|null $Id
36
     * @param string|null $MimeType
37
     * @param string|null $Encoding
38
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
39
     */
40
    public function __construct(
41
        protected ?string $Id = null,
42
        protected ?string $MimeType = null,
43
        protected ?string $Encoding = null,
44
        array $elements = [],
45
    ) {
46
        Assert::nullOrValidNCName($Id);
47
        Assert::nullOrValidURI($Encoding);
48
49
        $this->setElements($elements);
50
    }
51
52
53
    /**
54
     * Collect the value of the Id-property
55
     *
56
     * @return string|null
57
     */
58
    public function getId(): ?string
59
    {
60
        return $this->Id;
61
    }
62
63
64
    /**
65
     * Collect the value of the MimeType-property
66
     *
67
     * @return string|null
68
     */
69
    public function getMimeType(): ?string
70
    {
71
        return $this->MimeType;
72
    }
73
74
75
    /**
76
     * Collect the value of the Encoding-property
77
     *
78
     * @return string|null
79
     */
80
    public function getEncoding(): ?string
81
    {
82
        return $this->Encoding;
83
    }
84
85
86
    /**
87
     * Test if an object, at the state it's in, would produce an empty XML-element
88
     *
89
     * @return bool
90
     */
91
    public function isEmptyElement(): bool
92
    {
93
        return empty($this->elements)
94
            && empty($this->Id)
95
            && empty($this->MimeType)
96
            && empty($this->Encoding);
97
    }
98
99
100
    /**
101
     * Convert XML into a ds:Object
102
     *
103
     * @param \DOMElement $xml The XML element we should load
104
     * @return static
105
     *
106
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
107
     *   If the qualified name of the supplied element is wrong
108
     */
109
    public static function fromXML(DOMElement $xml): static
110
    {
111
        Assert::same($xml->localName, 'Object', InvalidDOMElementException::class);
112
        Assert::same($xml->namespaceURI, DsObject::NS, InvalidDOMElementException::class);
113
114
        $Id = DsObject::getOptionalAttribute($xml, 'Id', null);
115
        $MimeType = DsObject::getOptionalAttribute($xml, 'MimeType', null);
116
        $Encoding = DsObject::getOptionalAttribute($xml, 'Encoding', null);
117
        $elements = self::getChildElementsFromXML($xml);
118
119
        return new static($Id, $MimeType, $Encoding, $elements);
120
    }
121
122
123
    /**
124
     * Convert this ds:Object element to XML.
125
     *
126
     * @param \DOMElement|null $parent The element we should append this ds:Object element to.
127
     * @return \DOMElement
128
     */
129
    public function toXML(?DOMElement $parent = null): DOMElement
130
    {
131
        $e = $this->instantiateParentElement($parent);
132
133
        if ($this->getId() !== null) {
134
            $e->setAttribute('Id', $this->getId());
135
        }
136
137
        if ($this->getMimeType() !== null) {
138
            $e->setAttribute('MimeType', $this->getMimeType());
139
        }
140
141
        if ($this->getEncoding() !== null) {
142
            $e->setAttribute('Encoding', $this->getEncoding());
143
        }
144
145
        foreach ($this->getElements() as $elt) {
146
            if (!$elt->isEmptyElement()) {
147
                $elt->toXML($e);
148
            }
149
        }
150
151
        return $e;
152
    }
153
}
154