AbstractBinarySecretType::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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\Base64ElementTrait;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_map;
16
use function explode;
17
use function implode;
18
19
/**
20
 * A BinarySecertType element
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractBinarySecretType extends AbstractWstElement
25
{
26
    use Base64ElementTrait;
27
    use ExtendableAttributesTrait;
28
29
    /** @var string|\SimpleSAML\XML\XsNamespace */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
    /** @var string[]|null */
33
    protected ?array $Type;
34
35
36
    /**
37
     * @param string $content
38
     * @param (\SimpleSAML\WSSecurity\XML\wst_200512\BinarySecretTypeEnum|string)[]|null $Type
39
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
40
     */
41
    final public function __construct(
42
        string $content,
43
        ?array $Type = null,
44
        array $namespacedAttributes = [],
45
    ) {
46
        if ($Type !== null) {
47
            $Type = array_map(
48
                function (BinarySecretTypeEnum|string $v): string {
49
                    return ($v instanceof BinarySecretTypeEnum) ? $v->value : $v;
50
                },
51
                $Type,
52
            );
53
            Assert::allValidURI($Type, SchemaViolationException::class);
54
            $this->Type = $Type;
55
        }
56
57
        $this->setContent($content);
58
        $this->setAttributesNS($namespacedAttributes);
59
    }
60
61
62
    /**
63
     * Get the Type property.
64
     *
65
     * @return string[]|null
66
     */
67
    public function getType(): ?array
68
    {
69
        return $this->Type;
70
    }
71
72
73
    /**
74
     * Convert XML into a class instance
75
     *
76
     * @param \DOMElement $xml The XML element we should load
77
     * @return static
78
     *
79
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
80
     *   If the qualified name of the supplied element is wrong
81
     */
82
    public static function fromXML(DOMElement $xml): static
83
    {
84
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
85
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
86
87
        return new static(
88
            $xml->textContent,
89
            explode(' ', self::getAttribute($xml, 'Type')),
90
            self::getAttributesNSFromXML($xml),
91
        );
92
    }
93
94
95
    /**
96
     * Convert this element to XML.
97
     *
98
     * @param \DOMElement|null $parent The element we should append this element to.
99
     * @return \DOMElement
100
     */
101
    public function toXML(?DOMElement $parent = null): DOMElement
102
    {
103
        $e = $this->instantiateParentElement($parent);
104
        $e->textContent = $this->getContent();
105
106
        if ($this->getType() !== null) {
107
            $e->setAttribute('Type', implode(' ', $this->getType()));
108
        }
109
110
        foreach ($this->getAttributesNS() as $attr) {
111
            $attr->toXML($e);
112
        }
113
114
        return $e;
115
    }
116
}
117