Passed
Pull Request — master (#19)
by Tim
02:42
created

AbstractRequestPseudonymType::toXML()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 9.2222
cc 6
nc 24
nop 1
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
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
34
    /**
35
     * AbstractRequestPseudonymType constructor
36
     *
37
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $SingleUse
38
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $Lookup
39
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
40
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
41
     */
42
    final public function __construct(
43
        protected ?BooleanValue $SingleUse = null,
44
        protected ?BooleanValue $Lookup = null,
45
        array $children = [],
46
        array $namespacedAttributes = [],
47
    ) {
48
        $this->setElements($children);
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
55
     */
56
    public function getSingleUse(): ?BooleanValue
57
    {
58
        return $this->SingleUse;
59
    }
60
61
62
    /**
63
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
64
     */
65
    public function getLookup(): ?BooleanValue
66
    {
67
        return $this->Lookup;
68
    }
69
70
71
    /**
72
     * Test if an object, at the state it's in, would produce an empty XML-element
73
     *
74
     * @return bool
75
     */
76
    public function isEmptyElement(): bool
77
    {
78
        return empty($this->getSingleUse())
79
            && empty($this->getLookup())
80
            && empty($this->getElements())
81
            && empty($this->getAttributesNS());
82
    }
83
84
85
    /**
86
     * Create an instance of this object from its XML representation.
87
     *
88
     * @param \DOMElement $xml
89
     * @return static
90
     *
91
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
92
     *   if the qualified name of the supplied element is wrong
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
97
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
98
99
        return new static(
100
            self::getOptionalAttribute($xml, 'SingleUse', BooleanValue::class, null),
101
            self::getOptionalAttribute($xml, 'Lookup', BooleanValue::class, null),
102
            self::getChildElementsFromXML($xml),
103
            self::getAttributesNSFromXML($xml),
104
        );
105
    }
106
107
108
    /**
109
     * Add this AbstractRequestPseudonymType to an XML element.
110
     *
111
     * @param \DOMElement|null $parent The element we should append this username token to.
112
     * @return \DOMElement
113
     */
114
    public function toXML(?DOMElement $parent = null): DOMElement
115
    {
116
        $e = parent::instantiateParentElement($parent);
117
118
        $singleUse = $this->getSingleUse();
119
        if ($singleUse !== null) {
120
            $e->setAttribute('SingleUse', var_export($singleUse->toBoolean(), true));
121
        }
122
123
        $lookup = $this->getLookup();
124
        if ($lookup !== null) {
125
            $e->setAttribute('Lookup', var_export($lookup->toBoolean(), true));
126
        }
127
128
        foreach ($this->getAttributesNS() as $attr) {
129
            $attr->toXML($e);
130
        }
131
132
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
133
        foreach ($this->getElements() as $child) {
134
            if (!$child->isEmptyElement()) {
135
                $child->toXML($e);
136
            }
137
        }
138
139
        return $e;
140
    }
141
}
142