AbstractClaimsType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 26
c 1
b 0
f 0
dl 0
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDialect() 0 3 1
A isEmptyElement() 0 5 3
A toXML() 0 19 5
A fromXML() 0 9 1
A __construct() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200502;
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\AnyURIValue;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
/**
16
 * Class defining the ClaimsType element
17
 *
18
 * @package simplesamlphp/ws-security
19
 */
20
abstract class AbstractClaimsType extends AbstractWstElement
21
{
22
    use ExtendableAttributesTrait;
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...0502\AbstractClaimsType: $namespaceURI, $localName, $childNodes
Loading history...
24
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
28
29
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
33
    /**
34
     * AbstractClaimsType constructor
35
     *
36
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $dialect
37
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
38
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39
     */
40
    final public function __construct(
41
        protected ?AnyURIValue $dialect = null,
42
        array $children = [],
43
        array $namespacedAttributes = [],
44
    ) {
45
        $this->setElements($children);
46
        $this->setAttributesNS($namespacedAttributes);
47
    }
48
49
50
    /**
51
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
52
     */
53
    public function getDialect(): ?AnyURIValue
54
    {
55
        return $this->dialect;
56
    }
57
58
59
    /**
60
     * Test if an object, at the state it's in, would produce an empty XML-element
61
     *
62
     * @return bool
63
     */
64
    public function isEmptyElement(): bool
65
    {
66
        return empty($this->getDialect())
67
            && empty($this->getElements())
68
            && empty($this->getAttributesNS());
69
    }
70
71
72
    /**
73
     * Create an instance of this object from its XML representation.
74
     *
75
     * @param \DOMElement $xml
76
     * @return static
77
     *
78
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
79
     *   if the qualified name of the supplied element is wrong
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
85
86
        return new static(
87
            self::getOptionalAttribute($xml, 'Dialect', AnyURIValue::class, null),
88
            self::getChildElementsFromXML($xml),
89
            self::getAttributesNSFromXML($xml),
90
        );
91
    }
92
93
94
    /**
95
     * Add this ClaimsType to an XML element.
96
     *
97
     * @param \DOMElement|null $parent The element we should append this element to.
98
     * @return \DOMElement
99
     */
100
    public function toXML(?DOMElement $parent = null): DOMElement
101
    {
102
        $e = parent::instantiateParentElement($parent);
103
104
        if ($this->getDialect() !== null) {
105
            $e->setAttribute('Dialect', $this->getDialect()->getValue());
106
        }
107
108
        foreach ($this->getElements() as $child) {
109
            if (!$child->isEmptyElement()) {
110
                $child->toXML($e);
111
            }
112
        }
113
114
        foreach ($this->getAttributesNS() as $attr) {
115
            $attr->toXML($e);
116
        }
117
118
        return $e;
119
    }
120
}
121