AbstractEncodedString::fromXML()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 21
rs 9.7998
cc 4
nc 3
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
12
/**
13
 * Abstract class defining the EncodedString type
14
 *
15
 * @package simplesamlphp/ws-security
16
 *
17
 * @phpstan-consistent-constructor
18
 */
19
abstract class AbstractEncodedString extends AbstractAttributedString
20
{
21
    /**
22
     * AbstractEncodedString constructor
23
     *
24
     * @param string $content
25
     * @param string|null $Id
26
     * @param string|null $EncodingType
27
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
28
     */
29
    public function __construct(
30
        string $content,
31
        ?string $Id = null,
32
        protected ?string $EncodingType = null,
33
        array $namespacedAttributes = [],
34
    ) {
35
        Assert::nullOrValidURI($EncodingType);
36
37
        parent::__construct($content, $Id, $namespacedAttributes);
38
    }
39
40
41
    /**
42
     * @return string|null
43
     */
44
    public function getEncodingType(): ?string
45
    {
46
        return $this->EncodingType;
47
    }
48
49
50
    /**
51
     * Create an instance of this object from its XML representation.
52
     *
53
     * @param \DOMElement $xml
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
63
64
        $nsAttributes = self::getAttributesNSFromXML($xml);
65
66
        $Id = null;
67
        foreach ($nsAttributes as $i => $attr) {
68
            if ($attr->getNamespaceURI() === C::NS_SEC_UTIL && $attr->getAttrName() === 'Id') {
69
                $Id = $attr->getAttrValue();
70
                unset($nsAttributes[$i]);
71
                break;
72
            }
73
        }
74
75
        return new static(
76
            $xml->textContent,
77
            $Id,
78
            self::getOptionalAttribute($xml, 'EncodingType', null),
79
            $nsAttributes,
80
        );
81
    }
82
83
84
    /**
85
     * @param \DOMElement|null $parent
86
     * @return \DOMElement
87
     */
88
    public function toXML(?DOMElement $parent = null): DOMElement
89
    {
90
        $e = parent::toXML($parent);
91
92
        if ($this->getEncodingType() !== null) {
93
            $e->setAttribute('EncodingType', $this->getEncodingType());
94
        }
95
96
        return $e;
97
    }
98
}
99