AbstractStructuredValueType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
dl 0
loc 73
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A fromXML() 0 8 1
A toXML() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\auth;
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
/**
15
 * Class defining the StructuredValueType element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractStructuredValueType extends AbstractAuthElement
20
{
21
    use ExtendableAttributesTrait;
22
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...ractStructuredValueType: $namespaceURI, $localName, $childNodes
Loading history...
23
24
25
    /** The namespace-attribute for the xs:anyAttribute element */
26
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * AbstractStructuredValueType constructor
34
     *
35
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
36
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
37
     */
38
    final public function __construct(
39
        array $children = [],
40
        array $namespacedAttributes = [],
41
    ) {
42
        // Next one is debatable since the schema allows an empty element, but that makes zero sense
43
        Assert::allNotEmpty([$children, $namespacedAttributes]);
44
45
        $this->setElements($children);
46
        $this->setAttributesNS($namespacedAttributes);
47
    }
48
49
50
    /**
51
     * Create an instance of this object from its XML representation.
52
     *
53
     * @param \DOMElement $xml
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
63
64
        return new static(
65
            self::getChildElementsFromXML($xml),
66
            self::getAttributesNSFromXML($xml),
67
        );
68
    }
69
70
71
    /**
72
     * Add this StructuredValueType to an XML element.
73
     *
74
     * @param \DOMElement $parent The element we should append this username token to.
75
     * @return \DOMElement
76
     */
77
    public function toXML(?DOMElement $parent = null): DOMElement
78
    {
79
        $e = parent::instantiateParentElement($parent);
80
81
        foreach ($this->getAttributesNS() as $attr) {
82
            $attr->toXML($e);
83
        }
84
85
        foreach ($this->getElements() as $child) {
86
            if (!$child->isEmptyElement()) {
87
                $child->toXML($e);
88
            }
89
        }
90
91
        return $e;
92
    }
93
}
94