AbstractBinarySecurityTokenType::toXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsse;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\Constants as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\XsNamespace as NS;
13
14
/**
15
 * Class defining the BinarySecurityTokenType element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractBinarySecurityTokenType extends AbstractEncodedString
20
{
21
    /** The namespace-attribute for the xs:anyAttribute element */
22
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
23
24
25
    /**
26
     * AbstractBinarySecurityTokenType constructor
27
     *
28
     * @param string $content
29
     * @param string|null $valueType
30
     * @param string|null $Id
31
     * @param string|null $EncodingType
32
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
33
     */
34
    final public function __construct(
35
        string $content,
36
        protected ?string $valueType = null,
37
        ?string $Id = null,
38
        ?string $EncodingType = null,
39
        array $namespacedAttributes = [],
40
    ) {
41
        Assert::nullOrValidURI($valueType, SchemaViolationException::class);
42
43
        parent::__construct($content, $Id, $EncodingType, $namespacedAttributes);
44
    }
45
46
47
    /**
48
     * @return string|null
49
     */
50
    public function getValueType(): ?string
51
    {
52
        return $this->valueType;
53
    }
54
55
56
    /**
57
     * Create an instance of this object from its XML representation.
58
     *
59
     * @param \DOMElement $xml
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     */
65
    public static function fromXML(DOMElement $xml): static
66
    {
67
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
69
70
        $nsAttributes = self::getAttributesNSFromXML($xml);
71
72
        $Id = null;
73
        foreach ($nsAttributes as $i => $attr) {
74
            if ($attr->getNamespaceURI() === C::NS_SEC_UTIL && $attr->getAttrName() === 'Id') {
75
                $Id = $attr->getAttrValue();
76
                unset($nsAttributes[$i]);
77
                break;
78
            }
79
        }
80
81
        return new static(
82
            $xml->textContent,
83
            self::getOptionalAttribute($xml, 'ValueType', null),
84
            $Id,
85
            self::getOptionalAttribute($xml, 'EncodingType', null),
86
            $nsAttributes,
87
        );
88
    }
89
90
91
    /**
92
     * Add this username token to an XML element.
93
     *
94
     * @param \DOMElement $parent The element we should append this username token to.
95
     * @return \DOMElement
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = parent::toXML($parent);
100
101
        if ($this->getValueType() !== null) {
102
            $e->setAttribute('ValueType', $this->getValueType());
103
        }
104
105
        return $e;
106
    }
107
}
108