Passed
Push — master ( 6e5077...34d57b )
by Tim
02:29
created

Advice::getAssertionIDRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\SchemaViolationException;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
15
/**
16
 * Class representing a saml:Advice element.
17
 *
18
 * @package simplesaml/saml2
19
 */
20
final class Advice extends AbstractSamlElement
21
{
22
    use ExtendableElementTrait;
23
24
    /** The namespace-attribute for the xs:any element */
25
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
26
27
28
    /**
29
     * @param \SimpleSAML\SAML2\XML\saml\AssertionIDRef[] $assertionIDRef
30
     * @param \SimpleSAML\SAML2\XML\saml\AssertionURIRef[] $assertionURIRef
31
     * @param \SimpleSAML\SAML2\XML\saml\Assertion[] $assertion
32
     * @param \SimpleSAML\SAML2\XML\saml\EncryptedAssertion[] $encryptedAssertion
33
     * @param \SimpleSAML\XML\Chunk[] $elements
34
     */
35
    public function __construct(
36
        protected array $assertionIDRef = [],
37
        protected array $assertionURIRef = [],
38
        protected array $assertion = [],
39
        protected array $encryptedAssertion = [],
40
        array $elements = [],
41
    ) {
42
        Assert::allIsInstanceOf($assertionIDRef, AssertionIDRef::class, SchemaViolationException::class);
43
        Assert::allIsInstanceOf($assertionURIRef, AssertionURIRef::class, SchemaViolationException::class);
44
        Assert::allIsInstanceOf($assertion, Assertion::class, SchemaViolationException::class);
45
        Assert::allIsInstanceOf($encryptedAssertion, EncryptedAssertion::class, SchemaViolationException::class);
46
47
        $this->setElements($elements);
48
    }
49
50
51
    /**
52
     * Test if an object, at the state it's in, would produce an empty XML-element
53
     *
54
     * @return bool
55
     */
56
    public function isEmptyElement(): bool
57
    {
58
        return (
59
            empty($this->assertionIDRef)
60
            && empty($this->assertionURIRef)
61
            && empty($this->assertion)
62
            && empty($this->encryptedAssertion)
63
            && empty($this->getElements())
64
        );
65
    }
66
67
68
    /**
69
     * @return \SimpleSAML\SAML2\XML\saml\AssertionIDRef[]
70
     */
71
    public function getAssertionIDRef(): array
72
    {
73
        return $this->assertionIDRef;
74
    }
75
76
77
    /**
78
     * @return \SimpleSAML\SAML2\XML\saml\AssertionURIRef[]
79
     */
80
    public function getAssertionURIRef(): array
81
    {
82
        return $this->assertionURIRef;
83
    }
84
85
86
    /**
87
     * @return \SimpleSAML\SAML2\XML\saml\Assertion[]
88
     */
89
    public function getAssertion(): array
90
    {
91
        return $this->assertion;
92
    }
93
94
95
    /**
96
     * @return \SimpleSAML\SAML2\XML\saml\EncryptedAssertion[]
97
     */
98
    public function getEncryptedAssertion(): array
99
    {
100
        return $this->encryptedAssertion;
101
    }
102
103
104
    /**
105
     * Convert XML into an Advice
106
     *
107
     * @param \DOMElement $xml The XML element we should load
108
     * @return static
109
     *
110
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
111
     *   If the qualified name of the supplied element is wrong
112
     */
113
    public static function fromXML(DOMElement $xml): static
114
    {
115
        $qualifiedName = static::getClassName(static::class);
116
        Assert::eq(
117
            $xml->localName,
118
            $qualifiedName,
119
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.',
120
            InvalidDOMElementException::class,
121
        );
122
123
        $assertionIDRef = AssertionIDRef::getChildrenOfClass($xml);
124
        $assertionURIRef = AssertionURIRef::getChildrenOfClass($xml);
125
        $assertion = Assertion::getChildrenOfClass($xml);
126
        $encryptedAssertion = EncryptedAssertion::getChildrenOfClass($xml);
127
128
        $elements = [];
129
        foreach ($xml->childNodes as $element) {
130
            if ($element->namespaceURI === C::NS_SAML) {
131
                continue;
132
            } elseif (!($element instanceof DOMElement)) {
133
                continue;
134
            }
135
136
            $elements[] = new Chunk($element);
137
        }
138
139
        return new static(
140
            $assertionIDRef,
141
            $assertionURIRef,
142
            $assertion,
143
            $encryptedAssertion,
144
            $elements,
145
        );
146
    }
147
148
149
    /**
150
     * Convert this Advince to XML.
151
     *
152
     * @param \DOMElement $parent The element we are converting to XML.
153
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
154
     */
155
    public function toXML(DOMElement $parent = null): DOMElement
156
    {
157
        $e = $this->instantiateParentElement($parent);
158
159
        foreach ($this->getAssertionIDRef() as $assertionIDRef) {
160
            $assertionIDRef->toXML($e);
161
        }
162
163
        foreach ($this->getAssertionURIRef() as $assertionURIRef) {
164
            $assertionURIRef->toXML($e);
165
        }
166
167
        foreach ($this->getAssertion() as $assertion) {
168
            $assertion->toXML($e);
169
        }
170
171
        foreach ($this->getEncryptedAssertion() as $encryptedAssertion) {
172
            $encryptedAssertion->toXML($e);
173
        }
174
175
        foreach ($this->getElements() as $element) {
176
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $element */
177
            $element->toXML($e);
178
        }
179
180
        return $e;
181
    }
182
}
183