Advice   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 17

8 Methods

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