Passed
Push — master ( 566f03...c7e878 )
by Tim
02:14
created

AuthnContextDecl::toXML()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
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\Attribute as XMLAttribute;
11
use SimpleSAML\XML\Chunk;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
16
/**
17
 * Class representing SAML2 AuthnContextDecl
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class AuthnContextDecl extends AbstractSamlElement
22
{
23
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\AuthnContextDecl: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
24
    use ExtendableElementTrait;
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_ANY;
28
29
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_ANY;
31
32
33
    /**
34
     * Initialize an AuthnContextDecl.
35
     *
36
     * @param \SimpleSAML\XML\Chunk[] $elements
37
     * @param \SimpleSAML\XML\Attribute[] $attributes
38
     */
39
    public function __construct(array $elements = [], array $attributes = [])
40
    {
41
        $this->setElements($elements);
42
        $this->setAttributesNS($attributes);
43
    }
44
45
46
47
    /**
48
     * Test if an object, at the state it's in, would produce an empty XML-element
49
     *
50
     * @return bool
51
     */
52
    public function isEmptyElement(): bool
53
    {
54
        return empty($this->getAttributesNS())
55
            && empty($this->getElements());
56
    }
57
58
59
    /**
60
     * Convert XML into a AuthnContextDecl
61
     *
62
     * @param \DOMElement $xml The XML element we should load
63
     * @return static
64
     *
65
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
66
     *   if the qualified name of the supplied element is wrong
67
     */
68
    public static function fromXML(DOMElement $xml): static
69
    {
70
        Assert::same($xml->localName, 'AuthnContextDecl', InvalidDOMElementException::class);
71
        Assert::same($xml->namespaceURI, AuthnContextDecl::NS, InvalidDOMElementException::class);
72
73
        $elements = [];
74
        foreach ($xml->childNodes as $element) {
75
            if ($element->namespaceURI === C::NS_SAML) {
76
                continue;
77
            } elseif (!($element instanceof DOMElement)) {
78
                continue;
79
            }
80
81
            $elements[] = new Chunk($element);
82
        }
83
84
        return new static($elements, self::getAttributesNSFromXML($xml));
85
    }
86
87
88
    /**
89
     * Convert this AuthContextDecl to XML.
90
     *
91
     * @param \DOMElement|null $parent The element we should append this AuthnContextDecl to.
92
     * @return \DOMElement
93
     */
94
    public function toXML(DOMElement $parent = null): DOMElement
95
    {
96
        $e = $this->instantiateParentElement($parent);
97
98
        foreach ($this->getAttributesNS() as $attr) {
99
            $attr->toXML($e);
100
        }
101
102
        foreach ($this->getElements() as $element) {
103
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $element */
104
            $element->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110