AbstractPseudonymBasisType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

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

3 Methods

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