AbstractSerElementsType::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 17
rs 9.8333
cc 2
nc 1
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\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Type\AnyURIValue;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
use function sprintf;
17
18
/**
19
 * Class representing WS security policy SetElementsType.
20
 *
21
 * @package simplesamlphp/ws-security
22
 */
23
abstract class AbstractSerElementsType extends AbstractSpElement
24
{
25
    use ExtendableAttributesTrait;
26
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractSerElementsType: $namespaceURI, $localName, $childNodes
Loading history...
27
28
29
    /** The namespace-attribute for the xs:any element */
30
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
31
32
    /** The namespace-attribute for the xs:anyAttribute element */
33
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
34
35
    /** The exclusions for the xs:anyAttribute element */
36
    public const XS_ANY_ATTR_EXCLUSIONS = [
37
        ['http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702', 'XPathVersion'],
38
    ];
39
40
41
    /**
42
     * AbstractSerElementsType constructor.
43
     *
44
     * @param list<\SimpleSAML\WSSecurity\XML\sp_200702\XPath> $xpath
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...
45
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $xpathVersion
46
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     */
49
    final public function __construct(
50
        protected array $xpath,
51
        protected ?AnyURIValue $xpathVersion = null,
52
        array $elts = [],
53
        array $namespacedAttributes = [],
54
    ) {
55
        Assert::minCount($xpath, 1, SchemaViolationException::class);
56
57
        $this->setElements($elts);
58
        $this->setAttributesNS($namespacedAttributes);
59
    }
60
61
62
    /**
63
     * Collect the value of the XPath property.
64
     *
65
     * @return list<\SimpleSAML\WSSecurity\XML\sp_200702\XPath>
66
     */
67
    public function getXPath(): array
68
    {
69
        return $this->xpath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xpath returns the type array which is incompatible with the documented return type SimpleSAML\WSSecurity\XML\sp_200702\list.
Loading history...
70
    }
71
72
73
    /**
74
     * Collect the value of the XPathVersion property.
75
     *
76
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
77
     */
78
    public function getXPathVersion(): ?AnyURIValue
79
    {
80
        return $this->xpathVersion;
81
    }
82
83
84
    /**
85
     * Initialize an SerElementsType.
86
     *
87
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
88
     *
89
     * @param \DOMElement $xml The XML element we should load.
90
     * @return static
91
     *
92
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
93
     *   if the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): static
96
    {
97
        $qualifiedName = static::getClassName(static::class);
98
        Assert::eq(
99
            $xml->localName,
100
            $qualifiedName,
101
            sprintf('Unexpected name for SerElementsType: %s. Expected: %s.', $xml->localName, $qualifiedName),
102
            InvalidDOMElementException::class,
103
        );
104
105
        return new static(
106
            XPath::getChildrenOfClass($xml),
107
            $xml->hasAttributeNS(self::NS, 'XPathVersion')
108
                ? AnyURIValue::fromString($xml->getAttributeNS(self::NS, 'XPathVersion'))
109
                : null,
110
            self::getChildElementsFromXML($xml),
111
            self::getAttributesNSFromXML($xml),
112
        );
113
    }
114
115
116
    /**
117
     * Convert this element to XML.
118
     *
119
     * @param \DOMElement|null $parent The element we should append this element to.
120
     * @return \DOMElement
121
     */
122
    public function toXML(?DOMElement $parent = null): DOMElement
123
    {
124
        $e = $this->instantiateParentElement($parent);
125
126
        if ($this->getXPathVersion() !== null) {
127
            $e->setAttributeNS(self::NS, 'sp:XPathVersion', $this->getXPathVersion()->getValue());
128
        }
129
130
        foreach ($this->getXPath() as $xpath) {
131
            $xpath->toXML($e);
132
        }
133
134
        foreach ($this->getElements() as $elt) {
135
            if (!$elt->isEmptyElement()) {
136
                $elt->toXML($e);
137
            }
138
        }
139
140
        foreach ($this->getAttributesNS() as $attr) {
141
            $attr->toXML($e);
142
        }
143
144
        return $e;
145
    }
146
}
147