Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

Import::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue, NCNameValue};
12
13
use function strval;
14
15
/**
16
 * Class representing the import-element.
17
 *
18
 * @package simplesamlphp/xml-common
19
 */
20
final class Import extends AbstractAnnotated implements SchemaValidatableElementInterface
21
{
22
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\Import: $message, $line
Loading history...
23
24
    /** @var string */
25
    public const LOCALNAME = 'import';
26
27
28
    /**
29
     * Import constructor
30
     *
31
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $namespace
32
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $schemaLocation
33
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
34
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
35
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
36
     */
37
    public function __construct(
38
        protected ?AnyURIValue $namespace = null,
39
        protected ?AnyURIValue $schemaLocation = null,
40
        ?Annotation $annotation = null,
41
        ?IDValue $id = null,
42
        array $namespacedAttributes = [],
43
    ) {
44
        parent::__construct($annotation, $id, $namespacedAttributes);
45
    }
46
47
48
    /**
49
     * Collect the value of the namespace-property
50
     *
51
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
52
     */
53
    public function getNamespace(): ?AnyURIValue
54
    {
55
        return $this->namespace;
56
    }
57
58
59
    /**
60
     * Collect the value of the schemaLocation-property
61
     *
62
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
63
     */
64
    public function getSchemaLocation(): ?AnyURIValue
65
    {
66
        return $this->schemaLocation;
67
    }
68
69
70
    /**
71
     * Add this Import to an XML element.
72
     *
73
     * @param \DOMElement|null $parent The element we should append this import to.
74
     * @return \DOMElement
75
     */
76
    public function toXML(?DOMElement $parent = null): DOMElement
77
    {
78
        $e = parent::toXML($parent);
79
80
        if ($this->getNamespace() !== null) {
81
            $e->setAttribute('namespace', strval($this->getNamespace()));
82
        }
83
84
        if ($this->getSchemaLocation() !== null) {
85
            $e->setAttribute('schemaLocation', strval($this->getSchemaLocation()));
86
        }
87
88
        return $e;
89
    }
90
91
92
    /**
93
     * Create an instance of this object from its XML representation.
94
     *
95
     * @param \DOMElement $xml
96
     * @return static
97
     *
98
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
99
     *   if the qualified name of the supplied element is wrong
100
     */
101
    public static function fromXML(DOMElement $xml): static
102
    {
103
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
104
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
105
106
        $annotation = Annotation::getChildrenOfClass($xml);
107
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
108
109
        return new static(
110
            self::getOptionalAttribute($xml, 'namespace', AnyURIValue::class, null),
111
            self::getOptionalAttribute($xml, 'schemaLocation', AnyURIValue::class, null),
112
            array_pop($annotation),
113
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
114
            self::getAttributesNSFromXML($xml),
115
        );
116
    }
117
}
118