AbstractSePartsType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 23
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
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\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\XML\Constants\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
29
    /** The namespace-attribute for the xs:any element */
30
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
31
32
    /** The namespace-attribute for the xs:anyAttribute element */
33
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
34
35
36
    /**
37
     * AbstractSePartsType constructor.
38
     *
39
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Body|null $body
40
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Header[] $header
41
     * @param \SimpleSAML\WSSecurity\XML\sp_200702\Attachments|null $attachments
42
     * @param \SimpleSAML\XML\SerializableElementInterface[] $details
43
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
44
     */
45
    final public function __construct(
46
        protected ?Body $body = null,
47
        protected array $header = [],
48
        protected ?Attachments $attachments = null,
49
        array $details = [],
50
        array $namespacedAttributes = [],
51
    ) {
52
        $this->setElements($details);
53
        $this->setAttributesNS($namespacedAttributes);
54
    }
55
56
57
    /**
58
     * Collect the Body
59
     *
60
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Body|null
61
     */
62
    public function getBody(): ?Body
63
    {
64
        return $this->body;
65
    }
66
67
68
    /**
69
     * Collect the Header
70
     *
71
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Header[]
72
     */
73
    public function getHeader(): array
74
    {
75
        return $this->header;
76
    }
77
78
79
    /**
80
     * Collect the Attachments
81
     *
82
     * @return \SimpleSAML\WSSecurity\XML\sp_200702\Attachments|null
83
     */
84
    public function getAttachments(): ?Attachments
85
    {
86
        return $this->attachments;
87
    }
88
89
90
    /**
91
     * Test if an object, at the state it's in, would produce an empty XML-element
92
     *
93
     * @return bool
94
     */
95
    public function isEmptyElement(): bool
96
    {
97
        return empty($this->getElements())
98
            && empty($this->getBody())
99
            && empty($this->getHeader())
100
            && empty($this->getAttachments())
101
            && empty($this->getAttributesNS());
102
    }
103
104
105
    /**
106
     * Initialize an SePartsType.
107
     *
108
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
109
     *
110
     * @param \DOMElement $xml The XML element we should load.
111
     * @return static
112
     *
113
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
114
     *   if the qualified name of the supplied element is wrong
115
     */
116
    public static function fromXML(DOMElement $xml): static
117
    {
118
        $qualifiedName = static::getClassName(static::class);
119
        Assert::eq(
120
            $xml->localName,
121
            $qualifiedName,
122
            sprintf('Unexpected name for Empty: %s. Expected: %s.', $xml->localName, $qualifiedName),
123
            InvalidDOMElementException::class,
124
        );
125
126
        $body = Body::getChildrenOfClass($xml);
127
128
        $header = Header::getChildrenOfClass($xml);
129
        Assert::maxCount($header, 1, TooManyElementsException::class);
130
131
        $attachments = Attachments::getChildrenOfClass($xml);
132
133
        return new static(
134
            array_pop($body),
135
            $header,
136
            array_pop($attachments),
137
            self::getChildElementsFromXML($xml),
138
            self::getAttributesNSFromXML($xml),
139
        );
140
    }
141
142
143
    /**
144
     * Convert this element to XML.
145
     *
146
     * @param \DOMElement|null $parent The element we should append this element to.
147
     * @return \DOMElement
148
     */
149
    public function toXML(?DOMElement $parent = null): DOMElement
150
    {
151
        $e = $this->instantiateParentElement($parent);
152
153
        if ($this->getBody() !== null) {
154
            $this->getBody()->toXML($e);
155
        }
156
157
        foreach ($this->getHeader() as $header) {
158
            $header->toXML($e);
159
        }
160
161
        if ($this->getAttachments() !== null) {
162
            $this->getAttachments()->toXML($e);
163
        }
164
165
        foreach ($this->getElements() as $elt) {
166
            if (!$elt->isEmptyElement()) {
167
                $elt->toXML($e);
168
            }
169
        }
170
171
        foreach ($this->getAttributesNS() as $attr) {
172
            $attr->toXML($e);
173
        }
174
175
        return $e;
176
    }
177
}
178