AbstractAdviceType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Constants as C;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\ExtendableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
13
use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum;
14
15
/**
16
 * SAML AdviceType abstract data type.
17
 *
18
 * @package simplesamlphp/saml11
19
 */
20
abstract class AbstractAdviceType extends AbstractSamlElement
21
{
22
    use ExtendableElementTrait;
23
24
    /** The namespace-attribute for the xs:any element */
25
    public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Other;
26
27
28
    /**
29
     * Initialize a saml:AdviceType from scratch
30
     *
31
     * @param array<\SimpleSAML\SAML11\XML\saml\AssertionIDReference> $assertionIDReference
32
     * @param array<\SimpleSAML\SAML11\XML\saml\Assertion> $assertion
33
     * @param \SimpleSAML\XML\SerializableElementInterface[] $elements
34
     */
35
    final public function __construct(
36
        protected array $assertionIDReference = [],
37
        protected array $assertion = [],
38
        array $elements = [],
39
    ) {
40
        Assert::maxCount($assertionIDReference, C::UNBOUNDED_LIMIT);
41
        Assert::maxCount($assertion, C::UNBOUNDED_LIMIT);
42
        Assert::allIsInstanceOf($assertionIDReference, AssertionIDReference::class, SchemaViolationException::class);
43
        Assert::allIsInstanceOf($assertion, Assertion::class, SchemaViolationException::class);
44
45
        $this->setElements($elements);
46
    }
47
48
49
    /**
50
     * Collect the value of the assertionIDReference-property
51
     *
52
     * @return array<\SimpleSAML\SAML11\XML\saml\AssertionIDReference>
53
     */
54
    public function getAssertionIDReference(): array
55
    {
56
        return $this->assertionIDReference;
57
    }
58
59
60
    /**
61
     * Collect the value of the assertion-property
62
     *
63
     * @return array<\SimpleSAML\SAML11\XML\saml\Assertion>
64
     */
65
    public function getAssertion(): array
66
    {
67
        return $this->assertion;
68
    }
69
70
71
    /**
72
     * Test if an object, at the state it's in, would produce an empty XML-element
73
     *
74
     * @return bool
75
     */
76
    public function isEmptyElement(): bool
77
    {
78
        return empty($this->assertionIDReference)
79
            && empty($this->assertion)
80
            && empty($this->getElements());
81
    }
82
83
84
    /**
85
     * Convert XML into an AdviceType
86
     *
87
     * @param \DOMElement $xml The XML element we should load
88
     * @return static
89
     *
90
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
91
     *   if the qualified name of the supplied element is wrong
92
     */
93
    public static function fromXML(DOMElement $xml): static
94
    {
95
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
96
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
97
98
        $elements = [];
99
        foreach ($xml->childNodes as $element) {
100
            if ($element->namespaceURI === C::NS_SAML) {
101
                continue;
102
            } elseif (!($element instanceof DOMElement)) {
103
                continue;
104
            }
105
106
            $elements[] = new Chunk($element);
107
        }
108
109
        return new static(
110
            AssertionIDReference::getChildrenOfClass($xml),
111
            Assertion::getChildrenOfClass($xml),
112
            $elements,
113
        );
114
    }
115
116
117
    /**
118
     * Convert this EvidenceType to XML.
119
     *
120
     * @param \DOMElement $parent The element we are converting to XML.
121
     * @return \DOMElement The XML element after adding the data corresponding to this EvidenceType.
122
     */
123
    public function toXML(?DOMElement $parent = null): DOMElement
124
    {
125
        $e = $this->instantiateParentElement($parent);
126
127
        foreach ($this->getAssertionIDReference() as $assertionIDRef) {
128
            $assertionIDRef->toXML($e);
129
        }
130
131
        foreach ($this->getAssertion() as $assertion) {
132
            $assertion->toXML($e);
133
        }
134
135
        foreach ($this->getElements() as $element) {
136
            $element->toXML($e);
137
        }
138
139
        return $e;
140
    }
141
}
142