Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

Documentation   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 142
rs 10
c 1
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 3 1
A getLang() 0 3 1
A fromXML() 0 20 2
A getSource() 0 3 1
A __construct() 0 7 1
A isEmptyElement() 0 6 4
A toXML() 0 21 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML\xs;
6
7
use DOMElement;
8
use DOMNodeList;
9
use SimpleSAML\XML\Assert\Assert;
10
use SimpleSAML\XML\Attribute as XMLAttribute;
11
use SimpleSAML\XML\Constants as C;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
use SimpleSAML\XMLSchema\Type\Builtin\{AnyURIValue, StringValue};
16
use SimpleSAML\XMLSchema\XML\xs\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
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\xs\Documentation: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
28
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\xs\Documentation: $message, $line
Loading history...
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
        ['http://www.w3.org/XML/1998/namespace', 'lang'],
39
    ];
40
41
42
    /**
43
     * Documentation constructor
44
     *
45
     * @param \DOMNodeList<\DOMNode> $content
46
     * @param \SimpleSAML\XML\Attribute|null $lang
47
     * @param \SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue|null $source
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     */
50
    final public function __construct(
51
        protected DOMNodeList $content,
52
        protected ?XMLAttribute $lang = null,
53
        protected ?AnyURIValue $source = null,
54
        array $namespacedAttributes = [],
55
    ) {
56
        $this->setAttributesNS($namespacedAttributes);
57
    }
58
59
60
    /**
61
     * Get the content property.
62
     *
63
     * @return \DOMNodeList<\DOMNode>
64
     */
65
    public function getContent(): DOMNodeList
66
    {
67
        return $this->content;
68
    }
69
70
71
    /**
72
     * Get the lang property.
73
     *
74
     * @return \SimpleSAML\XML\Attribute|null
75
     */
76
    public function getLang(): ?XMLAttribute
77
    {
78
        return $this->lang;
79
    }
80
81
82
    /**
83
     * Get the source property.
84
     *
85
     * @return \SimpleSAML\XMLSchema\Type\Builtin\AnyURIValue|null
86
     */
87
    public function getSource(): ?AnyURIValue
88
    {
89
        return $this->source;
90
    }
91
92
93
    /**
94
     * Test if an object, at the state it's in, would produce an empty XML-element
95
     *
96
     * @return bool
97
     */
98
    public function isEmptyElement(): bool
99
    {
100
        return $this->getContent()->count() === 0
101
            && empty($this->getLang())
102
            && empty($this->getSource())
103
            && empty($this->getAttributesNS());
104
    }
105
106
107
    /**
108
     * Create an instance of this object from its XML representation.
109
     *
110
     * @param \DOMElement $xml
111
     * @return static
112
     *
113
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
114
     *   if the qualified name of the supplied element is wrong
115
     */
116
    public static function fromXML(DOMElement $xml): static
117
    {
118
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
119
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
120
121
        $lang = null;
122
        if ($xml->hasAttributeNS(C::NS_XML, 'lang')) {
123
            $lang = new XMLAttribute(
124
                C::NS_XML,
125
                'xml',
126
                'lang',
127
                StringValue::fromString($xml->getAttributeNS(C::NS_XML, 'lang')),
128
            );
129
        }
130
131
        return new static(
132
            $xml->childNodes,
133
            $lang,
134
            self::getOptionalAttribute($xml, 'source', AnyURIValue::class, null),
135
            self::getAttributesNSFromXML($xml),
136
        );
137
    }
138
139
140
    /**
141
     * Add this Documentation to an XML element.
142
     *
143
     * @param \DOMElement|null $parent The element we should append this Documentation to.
144
     * @return \DOMElement
145
     */
146
    public function toXML(?DOMElement $parent = null): DOMElement
147
    {
148
        $e = parent::instantiateParentElement($parent);
149
150
        if ($this->getSource() !== null) {
151
            $e->setAttribute('source', strval($this->getSource()));
152
        }
153
154
        if ($this->getLang() !== null) {
155
            $this->getLang()->toXML($e);
156
        }
157
158
        foreach ($this->getAttributesNS() as $attr) {
159
            $attr->toXML($e);
160
        }
161
162
        foreach ($this->getContent() as $i) {
163
            $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

163
            $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...
164
        }
165
166
        return $e;
167
    }
168
}
169