AbstractUseKeyType::isEmptyElement()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200512;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\SerializableElementInterface;
14
use SimpleSAML\XML\XsNamespace as NS;
15
16
/**
17
 * Class defining the UseKeyType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractUseKeyType extends AbstractWstElement
22
{
23
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...0512\AbstractUseKeyType: $namespaceURI, $localName, $childNodes
Loading history...
24
25
    /** The namespace-attribute for the xs:any element */
26
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
27
28
29
    /**
30
     * AbstractUseKeyType constructor
31
     *
32
     * @param \SimpleSAML\XML\SerializableElementInterface|null $child
33
     * @param string|null $Sig
34
     */
35
    final public function __construct(
36
        ?SerializableElementInterface $child = null,
37
        protected ?string $Sig = null,
38
    ) {
39
        Assert::nullOrValidURI($Sig, SchemaViolationException::class);
40
41
        if ($child !== null) {
42
            $this->setElements([$child]);
43
        }
44
    }
45
46
47
    /**
48
     * @return string|null
49
     */
50
    public function getSig(): ?string
51
    {
52
        return $this->Sig;
53
    }
54
55
56
    /**
57
     * Test if an object, at the state it's in, would produce an empty XML-element
58
     *
59
     * @return bool
60
     */
61
    public function isEmptyElement(): bool
62
    {
63
        return empty($this->getSig())
64
            && empty($this->getElements());
65
    }
66
67
68
    /**
69
     * Create an instance of this object from its XML representation.
70
     *
71
     * @param \DOMElement $xml
72
     * @return static
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
75
     *   if the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
81
82
        $children = self::getChildElementsFromXML($xml);
83
        Assert::maxCount($children, 1, TooManyElementsException::class);
84
85
        return new static(
86
            array_pop($children),
87
            self::getOptionalAttribute($xml, 'Sig', null),
88
        );
89
    }
90
91
92
    /**
93
     * Add this UseKeyType to an XML element.
94
     *
95
     * @param \DOMElement|null $parent The element we should append this username token to.
96
     * @return \DOMElement
97
     */
98
    public function toXML(?DOMElement $parent = null): DOMElement
99
    {
100
        $e = parent::instantiateParentElement($parent);
101
102
        if ($this->getSig() !== null) {
103
            $e->setAttribute('Sig', $this->getSig());
104
        }
105
106
        foreach ($this->getElements() as $child) {
107
            if (!$child->isEmptyElement()) {
108
                $child->toXML($e);
109
            }
110
        }
111
112
        return $e;
113
    }
114
}
115