DsObject::getId()   A
last analyzed

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\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;
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...
29
30
31
    /** @var string */
32
    public const LOCALNAME = 'Object';
33
34
    /** @var string */
35
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
36
37
38
    /**
39
     * Initialize a ds:Object element.
40
     *
41
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
42
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $MimeType
43
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Encoding
44
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
45
     */
46
    public function __construct(
47
        protected ?IDValue $Id = null,
48
        protected ?StringValue $MimeType = null,
49
        protected ?AnyURIValue $Encoding = null,
50
        array $elements = [],
51
    ) {
52
        $this->setElements($elements);
53
    }
54
55
56
    /**
57
     * Collect the value of the Id-property
58
     *
59
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
60
     */
61
    public function getId(): ?IDValue
62
    {
63
        return $this->Id;
64
    }
65
66
67
    /**
68
     * Collect the value of the MimeType-property
69
     *
70
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
71
     */
72
    public function getMimeType(): ?StringValue
73
    {
74
        return $this->MimeType;
75
    }
76
77
78
    /**
79
     * Collect the value of the Encoding-property
80
     *
81
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
82
     */
83
    public function getEncoding(): ?AnyURIValue
84
    {
85
        return $this->Encoding;
86
    }
87
88
89
    /**
90
     * Test if an object, at the state it's in, would produce an empty XML-element
91
     *
92
     * @return bool
93
     */
94
    public function isEmptyElement(): bool
95
    {
96
        return empty($this->getElements())
97
            && empty($this->getId())
98
            && empty($this->getMimeType())
99
            && empty($this->getEncoding());
100
    }
101
102
103
    /**
104
     * Convert XML into a ds:Object
105
     *
106
     * @param \DOMElement $xml The XML element we should load
107
     * @return static
108
     *
109
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
110
     *   If the qualified name of the supplied element is wrong
111
     */
112
    public static function fromXML(DOMElement $xml): static
113
    {
114
        Assert::same($xml->localName, 'Object', InvalidDOMElementException::class);
115
        Assert::same($xml->namespaceURI, DsObject::NS, InvalidDOMElementException::class);
116
117
        $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null);
118
        $MimeType = self::getOptionalAttribute($xml, 'MimeType', StringValue::class, null);
119
        $Encoding = self::getOptionalAttribute($xml, 'Encoding', AnyURIValue::class, null);
120
        $elements = self::getChildElementsFromXML($xml);
121
122
        return new static($Id, $MimeType, $Encoding, $elements);
123
    }
124
125
126
    /**
127
     * Convert this ds:Object element to XML.
128
     *
129
     * @param \DOMElement|null $parent The element we should append this ds:Object element to.
130
     * @return \DOMElement
131
     */
132
    public function toXML(?DOMElement $parent = null): DOMElement
133
    {
134
        $e = $this->instantiateParentElement($parent);
135
136
        if ($this->getId() !== null) {
137
            $e->setAttribute('Id', strval($this->getId()));
138
        }
139
140
        if ($this->getMimeType() !== null) {
141
            $e->setAttribute('MimeType', strval($this->getMimeType()));
142
        }
143
144
        if ($this->getEncoding() !== null) {
145
            $e->setAttribute('Encoding', strval($this->getEncoding()));
146
        }
147
148
        foreach ($this->getElements() as $elt) {
149
            if (!$elt->isEmptyElement()) {
150
                $elt->toXML($e);
151
            }
152
        }
153
154
        return $e;
155
    }
156
}
157