AbstractPseudonymBasisType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\SerializableElementInterface;
15
use SimpleSAML\XML\XsNamespace as 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
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
    /** The namespace-attribute for the xs:any element */
33
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
34
35
36
    /**
37
     * AbstractPseudonymBasis constructor
38
     *
39
     * @param \SimpleSAML\XML\SerializableElementInterface $child
40
     * @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
41
     */
42
    final public function __construct(
43
        SerializableElementInterface $child,
44
        array $namespacedAttributes = [],
45
    ) {
46
        $this->setElements([$child]);
47
        $this->setAttributesNS($namespacedAttributes);
48
    }
49
50
51
    /**
52
     * Create an instance of this object from its XML representation.
53
     *
54
     * @param \DOMElement $xml
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
64
65
        $children = self::getChildElementsFromXML($xml);
66
        Assert::minCount($children, 1, MissingElementException::class);
67
        Assert::maxCount($children, 1, TooManyElementsException::class);
68
69
        return new static(
70
            array_pop($children),
71
            self::getAttributesNSFromXML($xml),
72
        );
73
    }
74
75
76
    /**
77
     * Add this AbstractPseudonymBasisType to an XML element.
78
     *
79
     * @param \DOMElement|null $parent The element we should append this username token to.
80
     * @return \DOMElement
81
     */
82
    public function toXML(?DOMElement $parent = null): DOMElement
83
    {
84
        $e = parent::instantiateParentElement($parent);
85
86
        foreach ($this->getAttributesNS() as $attr) {
87
            $attr->toXML($e);
88
        }
89
90
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
91
        foreach ($this->getElements() as $child) {
92
            if (!$child->isEmptyElement()) {
93
                $child->toXML($e);
94
            }
95
        }
96
97
        return $e;
98
    }
99
}
100