AbstractEncodedString   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 21 4
A getEncodingType() 0 3 1
A __construct() 0 7 1
A toXML() 0 9 2
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\WSSecurity\XML\wsu\Type\IDValue;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSchema\Type\StringValue;
14
15
/**
16
 * Abstract class defining the EncodedString type
17
 *
18
 * @package simplesamlphp/ws-security
19
 *
20
 * @phpstan-consistent-constructor
21
 */
22
abstract class AbstractEncodedString extends AbstractAttributedString
23
{
24
    /**
25
     * AbstractEncodedString constructor
26
     *
27
     * @param \SimpleSAML\XMLSchema\Type\StringValue $content
28
     * @param \SimpleSAML\WSSecurity\XML\wsu\Type\IDValue|null $Id
29
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
30
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $EncodingType
31
     */
32
    public function __construct(
33
        StringValue $content,
34
        ?IDValue $Id = null,
35
        array $namespacedAttributes = [],
36
        protected ?AnyURIValue $EncodingType = null,
37
    ) {
38
        parent::__construct($content, $Id, $namespacedAttributes);
39
    }
40
41
42
    /**
43
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
44
     */
45
    public function getEncodingType(): ?AnyURIValue
46
    {
47
        return $this->EncodingType;
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\XMLSchema\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
        $nsAttributes = self::getAttributesNSFromXML($xml);
66
67
        $Id = null;
68
        foreach ($nsAttributes as $i => $attr) {
69
            if ($attr->getNamespaceURI() === C::NS_SEC_UTIL && $attr->getAttrName() === 'Id') {
70
                $Id = IDValue::fromString($attr->getAttrValue()->getValue());
71
                unset($nsAttributes[$i]);
72
                break;
73
            }
74
        }
75
76
        return new static(
77
            StringValue::fromString($xml->textContent),
78
            $Id,
79
            $nsAttributes,
80
            self::getOptionalAttribute($xml, 'EncodingType', AnyURIValue::class, null),
81
        );
82
    }
83
84
85
    /**
86
     * @param \DOMElement|null $parent
87
     * @return \DOMElement
88
     */
89
    public function toXML(?DOMElement $parent = null): DOMElement
90
    {
91
        $e = parent::toXML($parent);
92
93
        if ($this->getEncodingType() !== null) {
94
            $e->setAttribute('EncodingType', $this->getEncodingType()->getValue());
95
        }
96
97
        return $e;
98
    }
99
}
100