Passed
Branch feature/php83 (e52173)
by Tim
33:00 queued 16:50
created

DsObject::toXML()   A

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