AbstractHeaderType::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 17
rs 9.8666
c 1
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200507;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSchema\Type\AnyURIValue;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
use function sprintf;
16
17
/**
18
 * Class representing WS security policy HeaderType.
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractHeaderType extends AbstractSpElement
23
{
24
    use ExtendableAttributesTrait;
25
26
27
    /** The namespace-attribute for the xs:anyAttribute element */
28
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
29
30
    /** The exclusions for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_EXCLUSIONS = [
32
        [null, 'Name'],
33
        [null, 'Namespace'],
34
    ];
35
36
37
    /**
38
     * AbstractHeaderType constructor.
39
     *
40
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $namespace
41
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $name
42
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\sp_200507\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    final public function __construct(
45
        protected AnyURIValue $namespace,
46
        protected ?QNameValue $name = null,
47
        array $namespacedAttributes = [],
48
    ) {
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * Collect the value of the Name property.
55
     *
56
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
57
     */
58
    public function getName(): ?QNameValue
59
    {
60
        return $this->name;
61
    }
62
63
64
    /**
65
     * Collect the value of the Namespace property.
66
     *
67
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
68
     */
69
    public function getNamespace(): AnyURIValue
70
    {
71
        return $this->namespace;
72
    }
73
74
75
    /**
76
     * Initialize an HeaderType.
77
     *
78
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
79
     *
80
     * @param \DOMElement $xml The XML element we should load.
81
     * @return static
82
     *
83
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
84
     *   if the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): static
87
    {
88
        $qualifiedName = static::getClassName(static::class);
89
        Assert::eq(
90
            $xml->localName,
91
            $qualifiedName,
92
            sprintf('Unexpected name for HeaderType: %s. Expected: %s.', $xml->localName, $qualifiedName),
93
            InvalidDOMElementException::class,
94
        );
95
96
        $namespacedAttributes = self::getAttributesNSFromXML($xml);
97
        $namespace = self::getAttribute($xml, 'Namespace', AnyURIValue::class);
98
99
        return new static(
100
            $namespace,
101
            $xml->hasAttribute('Name') ? QNameValue::fromString($xml->getAttribute('Name')) : null,
102
            $namespacedAttributes,
103
        );
104
    }
105
106
107
    /**
108
     * Convert this element to XML.
109
     *
110
     * @param \DOMElement|null $parent The element we should append this element to.
111
     * @return \DOMElement
112
     */
113
    public function toXML(?DOMElement $parent = null): DOMElement
114
    {
115
        $e = $this->instantiateParentElement($parent);
116
117
        if ($this->getName() !== null) {
118
            $e->setAttribute('Name', $this->getName()->getValue());
119
        }
120
121
        $e->setAttribute('Namespace', $this->getNamespace()->getValue());
122
123
        foreach ($this->getAttributesNS() as $attr) {
124
            $attr->toXML($e);
125
        }
126
127
        return $e;
128
    }
129
}
130