Issues (341)

src/XMLSchema/XML/Documentation.php (1 issue)

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