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