Passed
Pull Request — master (#6)
by Tim
12:22
created

Attributes::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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