Appinfo::toXML()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use DOMNodeList;
9
use SimpleSAML\XML\Assert\Assert;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Type\AnyURIValue;
15
use SimpleSAML\XMLSchema\XML\Constants\NS;
16
17
use function strval;
18
19
/**
20
 * Class representing the appinfo element
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
final class Appinfo extends AbstractXsElement implements SchemaValidatableElementInterface
25
{
26
    use ExtendableAttributesTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\Appinfo: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
27
    use SchemaValidatableElementTrait;
28
29
30
    /** @var string */
31
    public const LOCALNAME = 'appinfo';
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
35
36
37
    /**
38
     * Appinfo constructor
39
     *
40
     * @param \DOMNodeList<\DOMNode> $content
41
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $source
42
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
43
     */
44
    final public function __construct(
45
        protected DOMNodeList $content,
46
        protected ?AnyURIValue $source = null,
47
        array $namespacedAttributes = [],
48
    ) {
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * Get the content property.
55
     *
56
     * @return \DOMNodeList<\DOMNode>
57
     */
58
    public function getContent(): DOMNodeList
59
    {
60
        return $this->content;
61
    }
62
63
64
    /**
65
     * Get the source property.
66
     *
67
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
68
     */
69
    public function getSource(): ?AnyURIValue
70
    {
71
        return $this->source;
72
    }
73
74
75
    /**
76
     * Test if an object, at the state it's in, would produce an empty XML-element
77
     *
78
     * @return bool
79
     */
80
    public function isEmptyElement(): bool
81
    {
82
        return $this->getContent()->count() === 0
83
            && empty($this->getSource())
84
            && empty($this->getAttributesNS());
85
    }
86
87
88
    /**
89
     * Create an instance of this object from its XML representation.
90
     *
91
     * @param \DOMElement $xml
92
     * @return static
93
     *
94
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
95
     *   if the qualified name of the supplied element is wrong
96
     */
97
    public static function fromXML(DOMElement $xml): static
98
    {
99
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
100
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
101
102
        return new static(
103
            $xml->childNodes,
104
            self::getOptionalAttribute($xml, 'source', AnyURIValue::class, null),
105
            self::getAttributesNSFromXML($xml),
106
        );
107
    }
108
109
110
    /**
111
     * Add this Appinfo to an XML element.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this Appinfo to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(?DOMElement $parent = null): DOMElement
117
    {
118
        $e = parent::instantiateParentElement($parent);
119
120
        if ($this->getSource() !== null) {
121
            $e->setAttribute('source', strval($this->getSource()));
122
        }
123
124
        foreach ($this->getAttributesNS() as $attr) {
125
            $attr->toXML($e);
126
        }
127
128
        foreach ($this->getContent() as $i) {
129
            $e->appendChild($e->ownerDocument->importNode($i, true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($i, true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
        }
131
132
        return $e;
133
    }
134
}
135