AbstractHeaderType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
c 0
b 0
f 0
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200702;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\XsNamespace as NS;
12
13
use function sprintf;
14
15
/**
16
 * Class representing WS security policy HeaderType.
17
 *
18
 * @package simplesamlphp/ws-security
19
 */
20
abstract class AbstractHeaderType extends AbstractSpElement
21
{
22
    use ExtendableAttributesTrait;
23
24
    /** The namespace-attribute for the xs:anyAttribute element */
25
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
26
27
28
    /**
29
     * AbstractHeaderType constructor.
30
     *
31
     * @param string $namespace
32
     * @param string|null $name
33
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\sp_200702\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...
34
     */
35
    final public function __construct(
36
        protected string $namespace,
37
        protected ?string $name = null,
38
        array $namespacedAttributes = [],
39
    ) {
40
        Assert::validURI($namespace);
41
        Assert::nullOrValidQName($name);
42
43
        $this->setAttributesNS($namespacedAttributes);
44
    }
45
46
47
    /**
48
     * Collect the value of the Name property.
49
     *
50
     * @return string|null
51
     */
52
    public function getName(): ?string
53
    {
54
        return $this->name;
55
    }
56
57
58
    /**
59
     * Collect the value of the Namespace property.
60
     *
61
     * @return string
62
     */
63
    public function getNamespace(): string
64
    {
65
        return $this->namespace;
66
    }
67
68
69
    /**
70
     * Initialize an HeaderType.
71
     *
72
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
73
     *
74
     * @param \DOMElement $xml The XML element we should load.
75
     * @return static
76
     *
77
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
78
     *   if the qualified name of the supplied element is wrong
79
     */
80
    public static function fromXML(DOMElement $xml): static
81
    {
82
        $qualifiedName = static::getClassName(static::class);
83
        Assert::eq(
84
            $xml->localName,
85
            $qualifiedName,
86
            sprintf('Unexpected name for HeaderType: %s. Expected: %s.', $xml->localName, $qualifiedName),
87
            InvalidDOMElementException::class,
88
        );
89
90
        $namespacedAttributes = self::getAttributesNSFromXML($xml);
91
        foreach ($namespacedAttributes as $i => $attr) {
92
            if ($attr->getNamespaceURI() === null) {
93
                if ($attr->getAttrName() === 'Name' || $attr->getAttrName() === 'Namespace') {
94
                    unset($namespacedAttributes[$i]);
95
                }
96
            }
97
        }
98
99
        return new static(
100
            self::getAttribute($xml, 'Namespace'),
101
            self::getOptionalAttribute($xml, '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());
119
        }
120
121
        $e->setAttribute('Namespace', $this->getNamespace());
122
123
        foreach ($this->getAttributesNS() as $attr) {
124
            $attr->toXML($e);
125
        }
126
127
        return $e;
128
    }
129
}
130