Attributes::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML;
6
7
use DOMElement;
8
use SimpleSAML\CAS\Assert\Assert;
9
use SimpleSAML\CAS\Constants as C;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException};
12
use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum;
13
14
/**
15
 * Class for CAS attributes
16
 *
17
 * @package simplesamlphp/cas
18
 */
19
final class Attributes extends AbstractCasElement
20
{
21
    use ExtendableElementTrait;
22
23
    /** @var string */
24
    final public const LOCALNAME = 'attributes';
25
26
    /** The namespace-attribute for the xs:any element */
27
    final public const XS_ANY_ELT_NAMESPACE = NamespaceEnum::Any;
28
29
    /** The exclusions for the xs:any element */
30
    final public const XS_ANY_ELT_EXCLUSIONS = [
31
        [C::NS_CAS, 'authenticationDate'],
32
        [C::NS_CAS, 'longTermAuthenticationRequestTokenUsed'],
33
        [C::NS_CAS, 'isFromNewLogin'],
34
    ];
35
36
37
    /**
38
     * Initialize a cas:attributes element
39
     *
40
     * @param \SimpleSAML\CAS\XML\AuthenticationDate $authenticationDate
41
     * @param \SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed
42
     * @param \SimpleSAML\CAS\XML\IsFromNewLogin $isFromNewLogin
43
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $elts
44
     */
45
    final public function __construct(
46
        protected AuthenticationDate $authenticationDate,
47
        protected LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed,
48
        protected IsFromNewLogin $isFromNewLogin,
49
        array $elts = [],
50
    ) {
51
        $this->setElements($elts);
52
    }
53
54
55
    /**
56
     * @return \SimpleSAML\CAS\XML\AuthenticationDate
57
     */
58
    public function getAuthenticationDate(): AuthenticationDate
59
    {
60
        return $this->authenticationDate;
61
    }
62
63
64
    /**
65
     * @return \SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed
66
     */
67
    public function getLongTermAuthenticationRequestTokenUsed(): LongTermAuthenticationRequestTokenUsed
68
    {
69
        return $this->longTermAuthenticationRequestTokenUsed;
70
    }
71
72
73
    /**
74
     * @return \SimpleSAML\CAS\XML\IsFromNewLogin
75
     */
76
    public function getIsFromNewLogin(): IsFromNewLogin
77
    {
78
        return $this->isFromNewLogin;
79
    }
80
81
82
    /**
83
     * Convert XML into a cas:attributes-element
84
     *
85
     * @param \DOMElement $xml The XML element we should load
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
89
     *   if the qualified name of the supplied element is wrong
90
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
91
     *   if the supplied element is missing one of the mandatory attributes
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::getNamespaceURI(), InvalidDOMElementException::class);
97
98
        $authenticationDate = AuthenticationDate::getChildrenOfClass($xml);
99
        Assert::count(
100
            $authenticationDate,
101
            1,
102
            'Exactly one <cas:authenticationDate> must be specified.',
103
            MissingElementException::class,
104
        );
105
106
        $longTermAuthenticationRequestTokenUsed = LongTermAuthenticationRequestTokenUsed::getChildrenOfClass($xml);
107
        Assert::count(
108
            $longTermAuthenticationRequestTokenUsed,
109
            1,
110
            'Exactly one <cas:longTermAuthenticationRequestTokenUsed> must be specified.',
111
            MissingElementException::class,
112
        );
113
114
        $isFromNewLogin = IsFromNewLogin::getChildrenOfClass($xml);
115
        Assert::count(
116
            $isFromNewLogin,
117
            1,
118
            'Exactly least one <cas:isFromNewLogin> must be specified.',
119
            MissingElementException::class,
120
        );
121
122
        return new static(
123
            $authenticationDate[0],
124
            $longTermAuthenticationRequestTokenUsed[0],
125
            $isFromNewLogin[0],
126
            self::getChildElementsFromXML($xml),
127
        );
128
    }
129
130
131
    /**
132
     * Convert this Attributes to XML.
133
     *
134
     * @param \DOMElement|null $parent The element we should append this Attributes to.
135
     * @return \DOMElement
136
     */
137
    public function toXML(?DOMElement $parent = null): DOMElement
138
    {
139
        $e = $this->instantiateParentElement($parent);
140
141
        $this->getAuthenticationDate()->toXML($e);
142
        $this->getLongTermAuthenticationRequestTokenUsed()->toXML($e);
143
        $this->getIsFromNewLogin()->toXML($e);
144
145
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
146
        foreach ($this->elements as $elt) {
147
            if (!$elt->isEmptyElement()) {
148
                $elt->toXML($e);
149
            }
150
        }
151
152
        return $e;
153
    }
154
}
155