AbstractSignOutType::getRealm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\Constants as C;
10
use SimpleSAML\XML\Attribute as XMLAttribute;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Exception\MissingElementException;
15
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
16
use SimpleSAML\XMLSchema\Type\NCNameValue;
17
use SimpleSAML\XMLSchema\XML\Constants\NS;
18
19
/**
20
 * A SignOutType
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractSignOutType extends AbstractFedElement
25
{
26
    use ExtendableAttributesTrait;
27
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\fed\AbstractSignOutType: $namespaceURI, $localName, $childNodes
Loading history...
28
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
32
33
    /** The namespace-attribute for the xs:any element */
34
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
35
36
    /** The exclusions for the xs:any element */
37
    public const XS_ANY_ELT_EXCLUSIONS = [
38
        ['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id'],
39
        ['http://docs.oasis-open.org/wsfed/federation/200706', 'Realm'],
40
        ['http://docs.oasis-open.org/wsfed/federation/200706', 'SignOutBasis'],
41
    ];
42
43
44
    /**
45
     * SignOutType constructor.
46
     *
47
     * @param \SimpleSAML\WSSecurity\XML\fed\SignOutBasis $signOutBasis
48
     * @param \SimpleSAML\WSSecurity\XML\fed\Realm|null $realm
49
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $Id
50
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
51
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
52
     */
53
    final public function __construct(
54
        protected SignOutBasis $signOutBasis,
55
        protected ?Realm $realm = null,
56
        protected ?NCNameValue $Id = null,
57
        array $children = [],
58
        array $namespacedAttributes = [],
59
    ) {
60
        $this->setElements($children);
61
        $this->setAttributesNS($namespacedAttributes);
62
    }
63
64
65
    /**
66
     * Collect the value of the signOutBasis-property
67
     *
68
     * @return \SimpleSAML\WSSecurity\XML\fed\SignOutBasis
69
     */
70
    public function getSignOutBasis(): SignOutBasis
71
    {
72
        return $this->signOutBasis;
73
    }
74
75
76
    /**
77
     * Collect the value of the realm-property
78
     *
79
     * @return \SimpleSAML\WSSecurity\XML\fed\Realm|null
80
     */
81
    public function getRealm(): ?Realm
82
    {
83
        return $this->realm;
84
    }
85
86
87
    /**
88
     * Collect the value of the Id-property
89
     *
90
     * @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
91
     */
92
    public function getId(): ?NCNameValue
93
    {
94
        return $this->Id;
95
    }
96
97
98
    /**
99
     * Create an instance of this object from its XML representation.
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
105
     *   if the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
111
112
        $signOutBasis = SignOutBasis::getChildrenOfClass($xml);
113
        Assert::minCount($signOutBasis, 1, MissingElementException::class);
114
        Assert::maxCount($signOutBasis, 1, TooManyElementsException::class);
115
116
        $realm = Realm::getChildrenOfClass($xml);
117
        Assert::maxCount($realm, 1, TooManyElementsException::class);
118
119
        return new static(
120
            array_pop($signOutBasis),
121
            array_pop($realm),
122
            $xml->hasAttributeNS(C::NS_SEC_UTIL, 'Id')
123
                ? NCNameValue::fromString($xml->getAttributeNS(C::NS_SEC_UTIL, 'Id'))
124
                : null,
125
            self::getChildElementsFromXML($xml),
126
            self::getAttributesNSFromXML($xml),
127
        );
128
    }
129
130
131
    /**
132
     * Add this SignOutType to an XML element.
133
     *
134
     * @param \DOMElement|null $parent The element we should append this username token to.
135
     * @return \DOMElement
136
     */
137
    public function toXML(?DOMElement $parent = null): DOMElement
138
    {
139
        $e = parent::instantiateParentElement($parent);
140
141
        $attributes = $this->getAttributesNS();
142
        if ($this->getId() !== null) {
143
            $idAttr = new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', $this->getId());
144
            array_unshift($attributes, $idAttr);
145
        }
146
147
        foreach ($attributes as $attr) {
148
            $attr->toXML($e);
149
        }
150
151
        $this->getRealm()?->toXML($e);
152
        $this->getSignOutBasis()->toXML($e);
153
154
        foreach ($this->getElements() as $child) {
155
            if (!$child->isEmptyElement()) {
156
                $child->toXML($e);
157
            }
158
        }
159
160
        return $e;
161
    }
162
}
163