Issues (33)

src/XMLSchema/XML/Appinfo.php (2 issues)

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

127
            $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...
128
        }
129
130
        return $e;
131
    }
132
}
133