AbstractSePartsType::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
c 0
b 0
f 0
rs 9.2222
cc 6
nc 32
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200702;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_pop;
16
use function sprintf;
17
18
/**
19
 * Class representing WS security policy AbstractSePartsType.
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractSePartsType extends AbstractSpElement
24
{
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...702\AbstractSePartsType: $namespaceURI, $localName, $childNodes
Loading history...
26
    use ExtendableAttributesTrait;
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
30
31
    /** The namespace-attribute for the xs:anyAttribute element */
32
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
33
34
35
    /**
36
     * AbstractSePartsType constructor.
37
     *
38
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Body|null $body
39
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Header[] $header
40
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Attachments|null $attachments
41
     * @param \SimpleSAML\XML\SerializableElementInterface[] $details
42
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
43
     */
44
    final public function __construct(
45
        protected ?Body $body = null,
46
        protected array $header = [],
47
        protected ?Attachments $attachments = null,
48
        array $details = [],
49
        array $namespacedAttributes = [],
50
    ) {
51
        $this->setElements($details);
52
        $this->setAttributesNS($namespacedAttributes);
53
    }
54
55
56
    /**
57
     * Collect the Body
58
     *
59
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Body|null
60
     */
61
    public function getBody(): ?Body
62
    {
63
        return $this->body;
64
    }
65
66
67
    /**
68
     * Collect the Header
69
     *
70
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Header[]
71
     */
72
    public function getHeader(): array
73
    {
74
        return $this->header;
75
    }
76
77
78
    /**
79
     * Collect the Attachments
80
     *
81
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Attachments|null
82
     */
83
    public function getAttachments(): ?Attachments
84
    {
85
        return $this->attachments;
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->getBody())
98
            && empty($this->getHeader())
99
            && empty($this->getAttachments())
100
            && empty($this->getAttributesNS());
101
    }
102
103
104
    /**
105
     * Initialize an SePartsType.
106
     *
107
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
108
     *
109
     * @param \DOMElement $xml The XML element we should load.
110
     * @return static
111
     *
112
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
113
     *   if the qualified name of the supplied element is wrong
114
     */
115
    public static function fromXML(DOMElement $xml): static
116
    {
117
        $qualifiedName = static::getClassName(static::class);
118
        Assert::eq(
119
            $xml->localName,
120
            $qualifiedName,
121
            sprintf('Unexpected name for Empty: %s. Expected: %s.', $xml->localName, $qualifiedName),
122
            InvalidDOMElementException::class,
123
        );
124
125
        $body = Body::getChildrenOfClass($xml);
126
127
        $header = Header::getChildrenOfClass($xml);
128
        Assert::maxCount($header, 1, TooManyElementsException::class);
129
130
        $attachments = Attachments::getChildrenOfClass($xml);
131
132
        return new static(
133
            array_pop($body),
134
            $header,
135
            array_pop($attachments),
136
            self::getChildElementsFromXML($xml),
137
            self::getAttributesNSFromXML($xml),
138
        );
139
    }
140
141
142
    /**
143
     * Convert this element to XML.
144
     *
145
     * @param \DOMElement|null $parent The element we should append this element to.
146
     * @return \DOMElement
147
     */
148
    public function toXML(?DOMElement $parent = null): DOMElement
149
    {
150
        $e = $this->instantiateParentElement($parent);
151
152
        if ($this->getBody() !== null) {
153
            $this->getBody()->toXML($e);
154
        }
155
156
        foreach ($this->getHeader() as $header) {
157
            $header->toXML($e);
158
        }
159
160
        if ($this->getAttachments() !== null) {
161
            $this->getAttachments()->toXML($e);
162
        }
163
164
        foreach ($this->getElements() as $elt) {
165
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
166
            $elt->toXML($e);
167
        }
168
169
        foreach ($this->getAttributesNS() as $attr) {
170
            $attr->toXML($e);
171
        }
172
173
        return $e;
174
    }
175
}
176