Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

RequestedAuthnContext::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\SAML2\Utils;
11
use SimpleSAML\SAML2\XML\saml\AuthnContextClassRef;
12
use SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef;
13
14
use function array_merge;
15
16
/**
17
 * Class representing SAML2 RequestedAuthnContext
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class RequestedAuthnContext extends AbstractSamlpElement
22
{
23
    /**
24
     * Initialize a RequestedAuthnContext.
25
     *
26
     * @param (
27
     *    \SimpleSAML\SAML2\XML\saml\AuthnContextClassRef|
28
     *    \SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef
29
     * )[] $requestedAuthnContexts
30
     * @param string $Comparison
31
     */
32
    public function __construct(
33
        protected array $requestedAuthnContexts = [],
34
        protected ?string $Comparison = null,
35
    ) {
36
        Assert::minCount($requestedAuthnContexts, 1);
37
        Assert::allIsInstanceOfAny($requestedAuthnContexts, [AuthnContextClassRef::class, AuthnContextDeclRef::class]);
38
39
        if ($requestedAuthnContexts[0] instanceof AuthnContextClassRef) {
40
            Assert::allIsInstanceOf(
41
                $requestedAuthnContexts,
42
                AuthnContextClassRef::class,
43
                'You need either AuthnContextClassRef or AuthnContextDeclRef, not both.',
44
            );
45
        } else { // Can only be AuthnContextDeclRef
46
            Assert::allIsInstanceOf(
47
                $requestedAuthnContexts,
48
                AuthnContextDeclRef::class,
49
                'You need either AuthnContextClassRef or AuthnContextDeclRef, not both.',
50
            );
51
        }
52
        Assert::nullOrOneOf($Comparison, ['exact', 'minimum', 'maximum', 'better']);
53
    }
54
55
56
    /**
57
     * Collect the value of the requestedAuthnContexts-property
58
     *
59
     * @return (\SimpleSAML\SAML2\XML\saml\AuthnContextClassRef|\SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef)[]
60
     */
61
    public function getRequestedAuthnContexts(): array
62
    {
63
        return $this->requestedAuthnContexts;
64
    }
65
66
67
    /**
68
     * Collect the value of the Comparison-property
69
     *
70
     * @return string|null
71
     */
72
    public function getComparison(): ?string
73
    {
74
        return $this->Comparison;
75
    }
76
77
78
    /**
79
     * Convert XML into a RequestedAuthnContext
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     * @return \SimpleSAML\SAML2\XML\samlp\RequestedAuthnContext
83
     *
84
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
85
     *   if the qualified name of the supplied element is wrong
86
     */
87
    public static function fromXML(DOMElement $xml): static
88
    {
89
        Assert::same($xml->localName, 'RequestedAuthnContext', InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, RequestedAuthnContext::NS, InvalidDOMElementException::class);
91
92
        return new static(
93
            array_merge(
94
                AuthnContextClassRef::getChildrenOfClass($xml),
95
                AuthnContextDeclRef::getChildrenOfClass($xml),
96
            ),
97
            self::getAttribute($xml, 'Comparison', null),
98
        );
99
    }
100
101
102
    /**
103
     * Convert this RequestedAuthnContext to XML.
104
     *
105
     * @param \DOMElement|null $parent The element we should append this RequestedAuthnContext to.
106
     * @return \DOMElement
107
     */
108
    public function toXML(DOMElement $parent = null): DOMElement
109
    {
110
        /** @psalm-var \DOMDocument $e->ownerDocument */
111
        $e = $this->instantiateParentElement($parent);
112
113
        foreach ($this->getRequestedAuthnContexts() as $context) {
114
            $context->toXML($e);
115
        }
116
117
        if ($this->getComparison() !== null) {
118
            $e->setAttribute('Comparison', $this->getComparison());
119
        }
120
121
        return $e;
122
    }
123
}
124