AbstractTokenAssertionType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 23
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isEmptyElement() 0 4 2
A fromXML() 0 13 1
A toXML() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200507;
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\XML\Constants\NS;
13
14
use function sprintf;
15
16
/**
17
 * Class representing WS security policy TokenAssertionType.
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractTokenAssertionType extends AbstractSpElement
22
{
23
    use ExtendableAttributesTrait;
24
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...tractTokenAssertionType: $namespaceURI, $localName, $childNodes
Loading history...
25
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
32
33
34
    /**
35
     * TokenAssertionType constructor.
36
     *
37
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
38
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39
     */
40
    final public function __construct(
41
        array $elts = [],
42
        array $namespacedAttributes = [],
43
    ) {
44
        $this->setElements($elts);
45
        $this->setAttributesNS($namespacedAttributes);
46
    }
47
48
49
    /**
50
     * Test if an object, at the state it's in, would produce an empty XML-element
51
     *
52
     * @return bool
53
     */
54
    public function isEmptyElement(): bool
55
    {
56
        return empty($this->getAttributesNS())
57
            && empty($this->getElements());
58
    }
59
60
61
    /**
62
     * Initialize an TokenAssertionType.
63
     *
64
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
65
     *
66
     * @param \DOMElement $xml The XML element we should load.
67
     * @return static
68
     *
69
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
70
     *   if the qualified name of the supplied element is wrong
71
     */
72
    public static function fromXML(DOMElement $xml): static
73
    {
74
        $qualifiedName = static::getClassName(static::class);
75
        Assert::eq(
76
            $xml->localName,
77
            $qualifiedName,
78
            sprintf('Unexpected name for TokenAssertionType: %s. Expected: %s.', $xml->localName, $qualifiedName),
79
            InvalidDOMElementException::class,
80
        );
81
82
        return new static(
83
            self::getChildElementsFromXML($xml),
84
            self::getAttributesNSFromXML($xml),
85
        );
86
    }
87
88
89
    /**
90
     * Convert this element to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this element to.
93
     * @return \DOMElement
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        foreach ($this->getElements() as $elt) {
100
            $elt->toXML($e);
101
        }
102
103
        foreach ($this->getAttributesNS() as $attr) {
104
            $attr->toXML($e);
105
        }
106
107
        return $e;
108
    }
109
}
110