AbstractEntropyType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 21
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 15 4
A isEmptyElement() 0 4 2
A fromXML() 0 8 1
A __construct() 0 6 1
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\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 EntropyType element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractEntropyType extends AbstractWstElement
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...512\AbstractEntropyType: $namespaceURI, $localName, $childNodes
Loading history...
23
24
25
    /** The namespace-attribute for the xs:any element */
26
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
27
28
    /** The namespace-attribute for the xs:anyAttribute element */
29
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * AbstractEntropyType 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
        $this->setElements($children);
43
        $this->setAttributesNS($namespacedAttributes);
44
    }
45
46
47
    /**
48
     * Test if an object, at the state it's in, would produce an empty XML-element
49
     *
50
     * @return bool
51
     */
52
    public function isEmptyElement(): bool
53
    {
54
        return empty($this->getElements())
55
            && empty($this->getAttributesNS());
56
    }
57
58
59
    /**
60
     * Create an instance of this object from its XML representation.
61
     *
62
     * @param \DOMElement $xml
63
     * @return static
64
     *
65
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
66
     *   if the qualified name of the supplied element is wrong
67
     */
68
    public static function fromXML(DOMElement $xml): static
69
    {
70
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
71
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
72
73
        return new static(
74
            self::getChildElementsFromXML($xml),
75
            self::getAttributesNSFromXML($xml),
76
        );
77
    }
78
79
80
    /**
81
     * Add this EntropyType to an XML element.
82
     *
83
     * @param \DOMElement|null $parent The element we should append this element to.
84
     * @return \DOMElement
85
     */
86
    public function toXML(?DOMElement $parent = null): DOMElement
87
    {
88
        $e = parent::instantiateParentElement($parent);
89
90
        foreach ($this->getElements() as $child) {
91
            if (!$child->isEmptyElement()) {
92
                $child->toXML($e);
93
            }
94
        }
95
96
        foreach ($this->getAttributesNS() as $attr) {
97
            $attr->toXML($e);
98
        }
99
100
        return $e;
101
    }
102
}
103