AbstractRequestPseudonymType   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 32
c 1
b 0
f 0
dl 0
loc 118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSingleUse() 0 3 1
A __construct() 0 8 1
A fromXML() 0 10 1
A isEmptyElement() 0 6 4
A getLookup() 0 3 1
A toXML() 0 25 6
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\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\BooleanValue;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
use function var_export;
16
17
/**
18
 * Class defining the RequestPseudonymType element
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractRequestPseudonymType extends AbstractFedElement
23
{
24
    use ExtendableAttributesTrait;
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...actRequestPseudonymType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * AbstractRequestPseudonymType constructor
37
     *
38
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $SingleUse
39
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $Lookup
40
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
41
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
42
     */
43
    final public function __construct(
44
        protected ?BooleanValue $SingleUse = null,
45
        protected ?BooleanValue $Lookup = null,
46
        array $children = [],
47
        array $namespacedAttributes = [],
48
    ) {
49
        $this->setElements($children);
50
        $this->setAttributesNS($namespacedAttributes);
51
    }
52
53
54
    /**
55
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
56
     */
57
    public function getSingleUse(): ?BooleanValue
58
    {
59
        return $this->SingleUse;
60
    }
61
62
63
    /**
64
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
65
     */
66
    public function getLookup(): ?BooleanValue
67
    {
68
        return $this->Lookup;
69
    }
70
71
72
    /**
73
     * Test if an object, at the state it's in, would produce an empty XML-element
74
     *
75
     * @return bool
76
     */
77
    public function isEmptyElement(): bool
78
    {
79
        return empty($this->getSingleUse())
80
            && empty($this->getLookup())
81
            && empty($this->getElements())
82
            && empty($this->getAttributesNS());
83
    }
84
85
86
    /**
87
     * Create an instance of this object from its XML representation.
88
     *
89
     * @param \DOMElement $xml
90
     * @return static
91
     *
92
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
93
     *   if the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): static
96
    {
97
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
99
100
        return new static(
101
            self::getOptionalAttribute($xml, 'SingleUse', BooleanValue::class, null),
102
            self::getOptionalAttribute($xml, 'Lookup', BooleanValue::class, null),
103
            self::getChildElementsFromXML($xml),
104
            self::getAttributesNSFromXML($xml),
105
        );
106
    }
107
108
109
    /**
110
     * Add this AbstractRequestPseudonymType to an XML element.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this username token to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(?DOMElement $parent = null): DOMElement
116
    {
117
        $e = parent::instantiateParentElement($parent);
118
119
        $singleUse = $this->getSingleUse();
120
        if ($singleUse !== null) {
121
            $e->setAttribute('SingleUse', var_export($singleUse->toBoolean(), true));
122
        }
123
124
        $lookup = $this->getLookup();
125
        if ($lookup !== null) {
126
            $e->setAttribute('Lookup', var_export($lookup->toBoolean(), true));
127
        }
128
129
        foreach ($this->getAttributesNS() as $attr) {
130
            $attr->toXML($e);
131
        }
132
133
        foreach ($this->getElements() as $child) {
134
            if (!$child->isEmptyElement()) {
135
                $child->toXML($e);
136
            }
137
        }
138
139
        return $e;
140
    }
141
}
142