Issues (33)

src/XMLSchema/XML/Documentation.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\Constants as C;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
13
use SimpleSAML\XML\Type\LangValue;
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
16
use SimpleSAML\XMLSchema\XML\Enumeration\NamespaceEnum;
17
18
use function strval;
19
20
/**
21
 * Class representing the documentation element
22
 *
23
 * @package simplesamlphp/xml-common
24
 */
25
final class Documentation extends AbstractXsElement implements SchemaValidatableElementInterface
26
{
27
    use ExtendableAttributesTrait;
0 ignored issues
show
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\Documentation: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
28
    use SchemaValidatableElementTrait;
29
30
    /** @var string */
31
    public const LOCALNAME = 'documentation';
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_NAMESPACE = NamespaceEnum::Other;
35
36
    /** The exclusions for the xs:anyAttribute element */
37
    public const XS_ANY_ATTR_EXCLUSIONS = [
38
        [C::NS_XML, 'lang'],
39
    ];
40
41
42
    /**
43
     * Documentation constructor
44
     *
45
     * @param \DOMNodeList<\DOMNode> $content
46
     * @param \SimpleSAML\XML\Type\LangValue|null $lang
47
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $source
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     */
50
    final public function __construct(
51
        protected DOMNodeList $content,
52
        protected ?LangValue $lang = null,
53
        protected ?AnyURIValue $source = null,
54
        array $namespacedAttributes = [],
55
    ) {
56
        Assert::lessThanEq($content->count(), C::UNBOUNDED_LIMIT);
57
        $this->setAttributesNS($namespacedAttributes);
58
    }
59
60
61
    /**
62
     * Get the content property.
63
     *
64
     * @return \DOMNodeList<\DOMNode>
65
     */
66
    public function getContent(): DOMNodeList
67
    {
68
        return $this->content;
69
    }
70
71
72
    /**
73
     * Get the lang property.
74
     *
75
     * @return \SimpleSAML\XML\Type\LangValue|null
76
     */
77
    public function getLang(): ?LangValue
78
    {
79
        return $this->lang;
80
    }
81
82
83
    /**
84
     * Get the source property.
85
     *
86
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
87
     */
88
    public function getSource(): ?AnyURIValue
89
    {
90
        return $this->source;
91
    }
92
93
94
    /**
95
     * Test if an object, at the state it's in, would produce an empty XML-element
96
     *
97
     * @return bool
98
     */
99
    public function isEmptyElement(): bool
100
    {
101
        return $this->getContent()->count() === 0
102
            && empty($this->getLang())
103
            && empty($this->getSource())
104
            && empty($this->getAttributesNS());
105
    }
106
107
108
    /**
109
     * Create an instance of this object from its XML representation.
110
     *
111
     * @param \DOMElement $xml
112
     * @return static
113
     *
114
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
115
     *   if the qualified name of the supplied element is wrong
116
     */
117
    public static function fromXML(DOMElement $xml): static
118
    {
119
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
120
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
121
122
        $lang = $xml->hasAttributeNS(C::NS_XML, 'lang')
123
            ? LangValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang'))
124
            : null;
125
126
        return new static(
127
            $xml->childNodes,
128
            $lang,
129
            self::getOptionalAttribute($xml, 'source', AnyURIValue::class, null),
130
            self::getAttributesNSFromXML($xml),
131
        );
132
    }
133
134
135
    /**
136
     * Add this Documentation to an XML element.
137
     *
138
     * @param \DOMElement|null $parent The element we should append this Documentation to.
139
     * @return \DOMElement
140
     */
141
    public function toXML(?DOMElement $parent = null): DOMElement
142
    {
143
        $e = parent::instantiateParentElement($parent);
144
145
        if ($this->getSource() !== null) {
146
            $e->setAttribute('source', strval($this->getSource()));
147
        }
148
149
        $this->getLang()?->toAttribute()->toXML($e);
150
151
        foreach ($this->getAttributesNS() as $attr) {
152
            $attr->toXML($e);
153
        }
154
155
        foreach ($this->getContent() as $i) {
156
            $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

156
            $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...
157
        }
158
159
        return $e;
160
    }
161
}
162